2

I have several Listing channels that all use the same templates:

E.g.

Templates:

  • /templates/view/overview.html
  • /templates/view/detail.html

Channels

  • News
  • Research Updates
  • Upcoming Events

How can I only list the entries for each channel at their respective URL without having to duplicate the templates?

E.g.

"http://mysite.com/news" should only list the the latest 20 news stories with links to the detail page.

{% for entry in craft.entries.section('news', 'research', 'events').limit(20).find() %}
<div class="col-sm-6">
    <h2 class="Heading"><a href="{{ entry.url }}" title="{{ entry.title }}">{{ entry.title }}</a></h2>
    <p class="Date">{{entry.postDate|date('M j, Y')}}</p>
    <p>{{ excerpt|length > 250 ? excerpt|slice(0, 200) ~ '...' : excerpt  }}</p>
    <p><a href="{{ entry.url }}" title="{{ entry.title }}" class="ReadMore">Read More</a></p>
</div>                          

{% endfor %}

3

A good option here is to make use of craft.request. As long as the URL is the section handle, you can do something like this:

{% current_section = craft.request.firstSegment %}

{% for entry in craft.entries.section(current_section).limit(20).find() %}
...
{% endfor %}

If the url is mysite.com/news, firstSegment will return 'news', which you can use in your craft.entries call.

UPDATE:

If your URLs need to be different than the section handles, make use of Twig's include with tag. For each section, you will need an index.html template with the following:

{% set current_section = 'thisSectionHanle' %}
{% include listPage.html with current_section %}

listPage.html can be the template that has the actual fetch and display logic. Using include with, listPage.html will have access to the current_section variable.

So not perfectly dry, but the main logic chunk will not have to be repeated.

| improve this answer | |
  • Thank you for your reply. This will work for the 'news' section but it does not work when I have sections with handles like 'reasearchUpdates'. Craft does not allow dashes in section names. My url for these entries would like something like mysite.com/research-updates/this-is-the-title – SteveO Aug 19 '15 at 20:49
  • Just updated with another option! – Aaron Berkowitz Aug 19 '15 at 21:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.