We've retired the forum in favor of GitHub Discussions.

New conversations can be started on GitHub. Existing conversations will remain for a while longer.

Head over to GitHub →

Filtering all listing pages through one template

Erin Dalzell September 28, 2014 by Erin Dalzell

Not sure if this is useful for anyone, but thought I'd share just in case.

I have a fairly standard blog, although it has over 800 entries, so loading it was initially slow and caching is, umm, temperamental. So to speed it up, I have broken out each year into its own folder (blog/2014, blog/2013, etc).

However, I wanted the root page to show entries from the current year (site.com = site.com/blog = site.com/blog/2014). I also wanted monthly archives (blog/2014/08) and taxonomy pages (site.com/blog/tags/tag1, site.com/blog/categories/cat1). I hate, hate to repeat myself so I wanted to run all of those through the same template so I didn't have to repeat anything.

Here's how I did it:

  • 1 "router" template, default
  • it calls the correct partial and passes in the correct variables.
  • I use Named Routes to handle the archive

default template: use the year if it's there, otherwise grab the current year (requires this plugin.

{{ if segment_3 }}
    {{ if segment_3|numeric }} 
        {{ theme:partial src="default" folder="blog/{segment_2}" since="{segment_2}-{segment_3}" until="{segment_2}-{segment_3} +1 month" }}
    {{ elseif segment_2 == "tags" or segment_2 == "categories"}}
        {{ theme:partial src="default" folder="blog*" taxonomy="true" }}
    {{ else }}
        {{ theme:partial src="post" }}
    {{ /if }}
{{ elseif segment_2 }}
    {{ theme:partial src="default" folder="blog/{segment_2}" taxonomy="false" since=null until=null }}
{{ else }}
    {{var:year is={current_date format='Y'} }}
    {{ theme:partial src="default" folder="blog/{var:year}" taxonomy="false" since=null until=null }}
{{ /if }}           

archives: requires a Named Route in routes.yaml:

    template: default
    layout: default

And finally, the magic default partial (the ONLY place I need to go to make changes):

<div class="container">
    <div class="row">
        <div class="col-sm-12 blog-main">
            {{ entries:listing folder="{folder}" limit="4" taxonomy="{taxonomy}" since=since until=until}}
                <div class="blog-post">
                    <h2 class="blog-post-title">
                        {{ if link }}
                            <a href="{{ link }}">{{ title }} ⇒</a>
                        {{ else }}
                            <a href="{{ url }}">{{ title }}</a>
                        {{ endif }}
                    <p class="blog-post-meta"><a href="{{ url }}">{{ datestamp format="F jS, Y" }}</a> by <a href="#">{{author|ucfirst}}</a> in {{categories_url_list}} {{ if tags }} with tags {{ tags_url_list }}{{ endif }}</p>
                    {{ content }}       
                    <p><a href="{{ url }}#comments">Come join the conversation!</a></p>
            {{ entries:pagination folder="{folder}" limit="4" taxonomy="{taxonomy}"}}
                <ul class="pager">
                    <li class="previous {{ if !previous_page }} disabled{{endif}}"><a href="{{ previous_page }}">Newer</a></li>
                    <li class="next {{ if !next_page }} disabled{{endif}}"><a href="{{ next_page }}">Older</a></li>
            {{ /entries:pagination }}
        </div> <!-- blog-main -->
    </div> <!-- row -->

You will need to move your post template into the partials directory.

>>>>>>> Unanswered <<<<<<<
2 Replies