Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Template Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Issues when trying iterate over a DataList in a template


Go to End


7 Posts   2320 Views

Avatar
mjvezzani

Community Member, 5 Posts

23 May 2017 at 8:34am

Hi,

I'm currently experiencing issues trying to iterate and display content from a DataList in a template. I've managed to find a way to get it to work, but it doesn't feel like the right way to do it. Here is what I've got:

Feature.php
public static function GetNumFeaturesBySection($arguments, $content = null, $parser = null, $tagName) {
    $numberOfFeatures = (int)$arguments['num'];
    $section = $arguments['section'];

    $listOfFeatures = Feature::get()->filter(array('Section.Title' => $section))->limit(4);

    return $listOfFeatures->renderWith('MultipleFeatureTemplate');
  }

-------

MultipleFeatureTemplate.ss

<div class='row'>
  <% loop $Me %>
  <div class="col-md-4">
    <h3>$Title</h3>
    <p>$Description</h3>
  <% end_loop %>
</div>

This code results in each Feature items getting rendered to the webpage on load, but I really don't like that

<% loop $Me %>
bit at all. I can't find any documentation on it and I only randomly stumbled upon it in this silverstripe forum question. I tried the other suggestion from the answerer in that post, but it doesn't work. Anyone out there have a better understanding on the proper way to iterate over $listOfFeatures in my template? Thanks!

Avatar
martimiz

Forum Moderator, 1391 Posts

24 May 2017 at 4:46am

Edited: 24/05/2017 4:48am

I would do something like this:

$this->customise(array('ListOfFeatures' => $listOfFeatures))->renderWith('MultipleFeatureTemplate');

That will provide your DataList with a name that you can then iterate:

<% loop $ListOfFeatures %>
...

Avatar
mjvezzani

Community Member, 5 Posts

24 May 2017 at 6:02am

Edited: 24/05/2017 6:03am

Thanks for the response.

As mentioned in my previous post, the link that I referenced also suggested doing that. I attempted to implement by changing my code as follows:

public static function GetFeatures($arguments, $content = null, $parser = null, $tagName) {
    $limit = isset($arguments['limit']) ? (int)$arguments['limit'] : '';
    $filter = isset($arguments['section']) ? $arguments['section'] : '';
    $sort = isset($arguments['sort']) ? $arguments['sort'] : '';

    if ($filter == '') {
      $listOfFeatures = Feature::get()->sort($sort)->limit($limit);
    } else {
      $listOfFeatures = Feature::get()->filter(array('Section.Title' => $filter))->sort($sort)->limit($limit);
    }
    // original code
    // return $listOfFeatures->renderWith('MultipleFeatureTemplate');
    // suggested changes
    $this->customize(array('ListOfFeatures' => $listOfFeatures))->renderWith('MultipleFeatureTemplate');
  }

Trying what I have above results in a 500 server error. Am I missing something? I also tried:

return $this->customize(array('ListOfFeatures' => $listOfFeatures))->renderWith('MultipleFeatureTemplate');

and ended up with the save server error.

Avatar
martimiz

Forum Moderator, 1391 Posts

24 May 2017 at 6:11am

Edited: 24/05/2017 6:14am

I think tat should be customise, not customize. The z was replaced at some point...

And sure, should add 'return' :)

[edit] https://docs.silverstripe.org/en/3/developer_guides/templates/rendering_templates/

Avatar
mjvezzani

Community Member, 5 Posts

25 May 2017 at 12:52am

Thanks again for the response.

Alright, so I changed the 'z' to an 's' and added the return statement. The result was:

 Fatal error: Using $this when not in object context in /Applications/MAMP/cms/www/mysite/code/program/Feature.php

I read the documentation that you referenced. Is using the `$this->customise` syntax something that is limited to controllers only? What I'm trying to do is create a short code called GetFeatures that retrieves multiple features and then renders the collection of features into my `MultipleFeaturesTemplate.ss` file. I've followed the documentation here to create the GetFeatures shortcode. However the documentation doesn't at all address how to define a short code that is responsible for rendering collections of data. Based on the error I'm getting, it would seem that using the `$this->customise` syntax does not work within a short code. What syntax would be appropriate within my Feature class in the short code definition?

Avatar
martimiz

Forum Moderator, 1391 Posts

25 May 2017 at 4:22am

Sorry, should obviously have noticed the static context :(

singleton('Controller')->customise(array('ListOfFeatures' => $listOfFeatures))->renderWith('MultipleFeatureTemplate');

Or Controller::curr()->customise... (not 100% sure)

Avatar
mjvezzani

Community Member, 5 Posts

25 May 2017 at 4:30am

That is fantastic! Works like a charm.

Thank you so much for working with me through that. Much appreciated!