Sign in

Custom field not working on index.hbs

  • This topic has 3 replies, 3 voices, and was last updated 1 year, 1 month ago by .
Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #6923
    Avatar photomac

    Hi,
    in the Artgallery theme (override) in the config.json file I have created a new Subtitle field, like this:

    “postConfig”: [
    {
    “name”: “subTitle”,
    “label”: “Sottotitolo”,
    “value”: “”,
    “type”: “textarea”
    },

     

    Then in the post.hbs page I inserted:

    <p class=”p-class”>{{@config.post.subTitle}} <p>.

    It works fine

    But in the index.hbs page it doesn’t work!

    I followed this guide: https://getpublii.com/dev/post-config-options/

    and tried:

    <p>{{postViewConfig.subTitle}} <p>.

    <p>{{@postViewConfig.subTitle}} <p>

    {{#if postViewConfig.subTitle}}
    {{title}}
    {{/if}}

    none of the 3 tests worked.
    What’s wrong?

    Thanks

    #6927
    Avatar photoitips3727

    Hi,
    in the Artgallery theme (override) in the config.json file I have created a new Subtitle field, like this…

    Looks like this option is intended for posts template (post.hbs), so perhaps not the home page template (index.hbs).

    #6930
    Avatar photoBob

    The post config option is strictly related to the post. It means that it will display, also on the homepage but for the post, it is assigned to.
    So let’s see how it can work within the index.hbs file:

    the loop displaying the posts listing with title and excerpt:

    {{#each posts}}
       <article class="feed__item">
          <header>         
             <h2>
                <a href="{{url}}" >
                   {{title}}
                </a>
             </h2>
          </header>   
          <p>
            {{{ excerpt }}}
          </p>
       </article>
    {{/each}}

    now let’s add the contents of the subTitle field between the title and excerpt section using the postViewConfig object.

    {{#each posts}}
       <article class="feed__item">
          <header>         
             <h2>
                <a href="{{url}}" >
                   {{title}}
                </a>
             </h2>
          </header>
          <p> 
            {{postViewConfig.subTitle}}
          </p>   
          <p>
            {{{ excerpt }}}
          </p>
       </article>
    {{/each}}

    As a result, the subTitle will be displayed only for the post where you fill in the content of the subTitle field.

    For example, if the loop displays five posts and you fill in the subTitle field for only one of them, for example Post-X, then the subTitle will only be displayed once in the loop in place of Post-X.
    The rest of the posts in the loop will show an empty paragraph – to avoid it you can use the “if” statement:

    {{#if postViewConfig.subTitle}}
       <p>{{postViewConfig.subTitle}} </p>
    {{/if}}

     

    --
    Do you appreciate the support you've received today? If so, consider donating to the Publii team by clicking here; we'll be sure to use your donation to make Publii even better!

    #6936
    Avatar photomac

    Thank you very much for the clarification and the examples, I did a test and it works very well.
    Thanks again for your help.

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.