Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get helper to fetch featured posts, else block not executing #8747

Closed
astab opened this issue Jul 24, 2017 · 5 comments
Closed

Get helper to fetch featured posts, else block not executing #8747

astab opened this issue Jul 24, 2017 · 5 comments
Assignees

Comments

@astab
Copy link

astab commented Jul 24, 2017

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
@ErisDS ErisDS self-assigned this Jul 25, 2017
@ErisDS ErisDS added the themes label Jul 25, 2017
@ErisDS
Copy link
Member

ErisDS commented 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

@ErisDS ErisDS closed this as completed Jul 25, 2017
@astab
Copy link
Author

astab commented Jul 25, 2017

Hi @ErisDS,
thank you for your reply. In my opinion, {{else}} should be the part of the {{get}} helper. It is very limited now. If I add any other elements inside {{get}} helper before {{#foreach}} or wrap the {{#foreach}} helper, these elements will be displayed even if there are no results. For instance, in this simple example the heading and empty ul tag will be displayed even if there are now featured posts.

{{#get "posts" filter="featured:true" limit="5"}}
  <h2>Featured posts</h2>
    <ul>
      {{#foreach posts}}
        <li>
          <a href="{{url}}">{{title}}</a>
	</li>
      {{/foreach}}
  </ul>
{{/get}}

@kevinansfield
Copy link
Member

kevinansfield commented 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}}

@astab
Copy link
Author

astab commented Jul 26, 2017

Thank you, @kevinansfield. That solves my problem.

@ErisDS
Copy link
Member

ErisDS commented Jul 26, 2017

@astab totally get where you're coming from. @kevinansfield's answer is correct and it is also possible to do things like:

{{#foreach featured_posts}}
   {{#if @first}}... something here{{/if}}
   {{#if @last}}... something here {{/if}}
{{/foreach}}

So, whilst the old way this worked was more convenient in a certain sense, I don't think we have added any limitations here, only improved consistency as these same patterns are required when you're not using the {{#get}} helper.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants