Retrieving some DataObjects in template without php getter
When trying to get e.g. the lastest blog posts in your template you normally create a getter for theese in php like:
function getLastestBlogPosts($limit=3, $offset=0) { return BlogPost::get()->limit($limit, $offset); }
and in template something like
<% loop $LatestBlogPosts %> $Title <% end_loop %>
While this works very good, you always need to touch your php codebase when you want to grab a DataList. But SilverStripe has a nice feature, to get any (simple) DataList directly in your template, the $List helper:
<% loop $List('BlogPost').Limit(3) %> $Title <% end_loop %>
which will get you the latest 3 blog posts (assuming that BlogPost's $default_sort is set properly).
See also:
- GenericTemplateProvider API docs: where the magic happens
- docs about modifying lists in templates
- Colin Richardson's great presentation about useful frontend modules (slide 17 showed me this idea)(video of the presentation)
Post your comment
Comments
No one has commented on this page yet.
RSS feed for comments on this page | RSS feed for all comments