question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get helper to fetch featured posts, else block not executing

See original GitHub issue

I’m using the get helper to output a featured posts carousel. If there are no featured posts, blog cover image should be displayed instead. It was working fine with Ghost 0.11.x version, but with Ghost 1.0 the else block is not executing even though there are no posts marked as featured.

Here is my code:

{{#get "posts" filter="featured:true" limit="5" include="author,tags" as |featured|}}
    <div id="featured-slider" class="featured-slider">
        {{#foreach featured}}
            <div>
                {{#if feature_image}}
                    <div class="slide-bg" style="background-image: url({{feature_image}})"></div>
		{{/if}}
		<div class="slide-content">
		    {{#if tags}}
		        <div class="post-tags">
			    {{tags separator=""}}
		        </div>
		    {{/if}}
		    <h2 class="post-title"><a href="{{{url}}}">{{title}}</a></h2>
	        </div>
	    </div>
	{{/foreach}}
    </div><!-- .featured-slider -->
{{else}}
    <div class="cover">
	{{#if @blog.cover_image}}
            <div class="cover-bg" style="background-image: url({{@blog.cover_image}})"></div>
	{{/if}}
	<div class="cover-content">
	    <h2 class="hero-title">{{@blog.title}}</h2>
	    <p class="hero-text">{{@blog.description}}</p>
	</div><!-- .cover-content -->
    </div><!-- .cover -->
{{/get}}

Technical details:

  • Ghost Version: 1.0
  • Node Version: 6.11.1

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kevinansfieldcommented, Jul 26, 2017

@astab in this case you should be able to use an explicit {{#if}} conditional:

{{#get "posts" filter="featured:true" limit="5" as |featured_posts|}}
  {{#if featured_posts}}
    <h2>Featured posts</h2>
    <ul>
      {{#foreach featured_posts}}
        <li>
          <a href="{{url}}">{{title}}</a>
	</li>
      {{/foreach}}
    </ul>
  {{/if}}
{{/get}}
1reaction
ErisDScommented, Jul 25, 2017

Hi @astab, this behaviour deliberately changed between 0.11.x and 1.0.

The way this worked in 0.11.0 was confusing, because the {{else}} needed to belong to get instead of foreach, and yet it was the foreach helper that most people are used to and which had the else block documented. Following a discussion it was decided to change this.

Unfortunately, this has been missed in our changelog for 1.0.0 and other documentation. I’m in the process of updating that now and have added a new section to the docs.

Hopefully this gets you on your way!

Closing this as we’re tracking outstanding documentation tasks to wrap up 1.0.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

Ghost Handlebars Theme Helpers: get
Make custom queries to the Ghost API using the get helper and fetch publicly available data. Read more about Ghost themes!
Read more >
Trying to retrieve a document by _id directly in template helper not ...
unfortunately I can not use iron router. So I need to get the id manually and grab it through the url. Is there...
Read more >
Helpers - Zendesk Developer Docs
Use the isnt helper to execute a template block conditionally, given an expression result. If the comparison evaluates to true (the two values...
Read more >
Handlebars Helpers Reference | BigCommerce Dev Center
Differs from the get helper in that this helper will return the actual object including the given property key. This helper does not...
Read more >
Top 10 Most Common Node.js Developer Mistakes - Toptal
// Trying to fetch an user object from the database. Node.js is free to run other parts of the code from the moment...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found