English 中文(简体)
BigCommerce get featured blog based on blog post tag
原标题:

I am trying to get the featured blog post based on the "featured" tag assigned to it. Below are my code logic. Is there any issue on the code below? I only want to display one featured blog-post in blog page. Using below code I am getting error in frontend "Server Error".

Here is how I assign "featured" tag to featured blog post:

Here is how I assign "featured" tag to featured blog post

Code in "Templates >> Page >> blog.html"

{{#each blog.posts}}
        {{ assignVar "found" "no" }}
        {{#if post.tags}}
            {{#each post.tags}}
                {{#if name "==" "featured"}}
                    {{ assignVar "found" "yes" }}
                    {{break}}
                {{/if}}
            {{/each}}
        {{/if}}
        
        {{#if getVar "found"   ==   "yes"}}
             <div class="sushil-featured-blog">
                <div class="sushil-featured-blog-image">
                    {{#if thumbnail}}
                        <figure class="blog-thumbnail">
                            <a href="{{url}}">
                                {{> components/common/responsive-img
                                    image=thumbnail
                                    fallback_size=theme_settings.blog_size
                                    lazyload=theme_settings.lazyload_mode
                                }}
                            </a>
                        </figure>
                    {{/if}}
                </div>
            </div>
            {{break}}
        {{/if}}
    {{/each}}
问题回答

There are a few issues with your code. The first is that in order to get a variable inside an if statement, you must use parentheses. The second is post.tags (should just be tags). The third potential issue is that I don t think "break" does anything.

Here is the revised code with items 1 and 2 resolved.

{{#each blog.posts}}
{{ assignVar "found" "no" }}
{{#if tags}}
    {{#each tags}}
        {{#if name "==" "featured"}}
            {{ assignVar "found" "yes" }}
            {{break}}
        {{/if}}
    {{/each}}
{{/if}}

{{#if (getVar "found")   ==   "yes"}}
        <div class="sushil-featured-blog">
        <div class="sushil-featured-blog-image">
            {{#if thumbnail}}
                <figure class="blog-thumbnail">
                    <a href="{{url}}">
                        {{> components/common/responsive-img
                            image=thumbnail
                            fallback_size=theme_settings.blog_size
                            lazyload=theme_settings.lazyload_mode
                        }}
                    </a>
                </figure>
            {{/if}}
        </div>
    </div>
    {{break}}
{{/if}}
{{/each}}

Note: if "break" doesn t work, it may show more than one post, and you may need to come up with another way to only show the first.





相关问题
BigCommerce get featured blog based on blog post tag

I am trying to get the featured blog post based on the "featured" tag assigned to it. Below are my code logic. Is there any issue on the code below? I only want to display one featured blog-...

Including .hbs handlebars file in Typescript transpile

I ve been trying to use handlebars in my Typescript node.js project by importing it through the readFileSync fs method, however when the project is transpiled into Javascript the .hbs file does not ...

Handlebars.js in Django templates

I need a javascript templating system and i think handlebars.js does an excellent job in this case. I m having syntax conflicts with handlebars templates inside a django template because django tries ...

Handlebars Template rendering template as text

I created a helper in Handlebars to help with logic, but my template parses the returned html as text rather than html. I have a quiz results page that is rendered after the quiz is completed: <...

Change rails view output from helper

I have rails helpers (hs_if, hs_else): = hs_if :current_user do Hello, {{ current_user/name }}! = hs_else do = link_to Sign In , sign_in_path ...which produces handlebars templates: {{#if ...

Handlebar.js not refreshing my template

OK. My HTML looks like below. <div id="json"></div> <div id="content-placeholder"> <script id="some-template" type="text/x-handlebars-template"> <table> <...

Handlebars Template not able to process JSON from Backbone

Handlebars is unable to read the JSON object that I am sending it as context. Here is the function that makes the call to the Mustache template and gives it the context: render: function() { var ...

Calling Helper Within If Block in Handlebars Template

I am working with Handlebars.js template engine and am trying to figure out a way to do something like this (contrived example): {{#if itemSelected "SomeItem"}} <div>This was selected</...

热门标签