Search code examples
handlebars.jsghost

Ghost handlebars.js {{#has tag="Top"}} doesn't work


In my loop.hbs template I'm trying to get Ghost to pin posts that have the tag "Top" to the top. I'm using the {{#has}} helper combined with a {{#foreach posts}}.

See code below. The behaviour I'm getting though is that only the {{^has tag="Top"}} (i.e. the second foreach loop) is working.

Any clues as to what I might be doing wrong?

{{#foreach posts}}
    {{#has tag="Top"}}
        <article class="{{post_class}} top-post" style="background-color:lightgray">
            <header class="post-header">
                <h2 class="post-title"><a href="{{url}}">{{{title}}}</a></h2>
            </header>
            <section class="post-excerpt">
                <p>{{excerpt words="26"}} <a class="read-more" href="{{url}}">&raquo;</a></p>
            </section>
            <footer class="post-meta">
                {{#if author.image}}<img class="author-thumb" src="{{author.image}}" alt="{{author.name}}" nopin="nopin" />{{/if}}
                {{author}}
                {{tags prefix=" on "}}
                <time class="post-date" datetime="{{date format='YYYY-MM-DD'}}">{{date format="DD MMMM YYYY"}}</time>
            </footer>
        </article>
    {{/has}}
{{/foreach}}

{{! All the code above doesn't seem to be working.  Only the below code outputs posts to the blog homepage }}
{{#foreach posts}}
     {{^has tag="Top"}}
        <article class="{{post_class}}">
            <header class="post-header">
                <h2 class="post-title"><a href="{{url}}">{{{title}}}</a></h2>
            </header>
            <section class="post-excerpt">
                <p>{{excerpt words="26"}} <a class="read-more" href="{{url}}">&raquo;</a></p>
            </section>
            <footer class="post-meta">
                {{#if author.image}}<img class="author-thumb" src="{{author.image}}" alt="{{author.name}}" nopin="nopin" />{{/if}}
                {{author}}
                {{tags prefix=" on "}}
                <time class="post-date" datetime="{{date format='YYYY-MM-DD'}}">{{date format="DD MMMM YYYY"}}</time>
            </footer>
        </article>
    {{/has}}
{{/foreach}}

Solution

  • Solution was to wrap the first block of code in {{#get}} wrappers, i.e.

    {{#get "posts" include="tags,author" filter="featured:true" limit="all" as |featured|}}{{/get}}
    

    The below gist pins posts with "top" tag to the top of the Ghost blog (on page 1 only) and formats posts marked as featured (but with no "top" tag) and includes them with other posts in default chronological order.

    https://gist.github.com/anonymous/386c7eb445cc97a45a1ea0ff56898ec6