Download
We're evolving to serve you better! This current forum has transitioned to read-only mode. For new discussions, support, and engagement, we've moved to GitHub Discussions.

Can’t create custom template in Mercury theme

  • #5473
    Avatar photo[anonymous]

    There’s a guide: https://getpublii.com/dev/how-to-create-custom-templates/

    I’m using Mercury theme

    1. Create a post-TEMPLATE_SLUG.hbs file

    Going to /themes/mercury-override/

    Creating post-CUSTOM_INDEX.hbs file with code from index.hbs since I want to make a custom template for index page

    1. In the theme’s config.json file, please add the following entry:
    "postTemplates": {
       "TEMPLATE_SLUG": "Human readable template name"
    }

    Creating a copy of config.json from /themes/mercury in /themes/mercury-override

    In that file I’m adding in the end of the file required code as this:

       
            {
                "label": "Drop cap",
                "cssClasses": "dropcap",
                "selector": "p"
            }
        ]
        "postTemplates": {
       "CUSTOM_INDEX": "Custom Index"
    }
    }

    Saving file. Going to Posts section, choosing any post, going into Post settings → Other options

    Post template: Not available in your theme

    Ok, what am I doing wrong here? If I restart Publii app, not only there are no post templates available, but all other options are gone. To fix this, I need to undo all my changes, delete all created files. I don’t get it. I tried doing this in /themes/mercury got same results. Tried to paste only required code in config.json, same results.

    #5474
    Avatar photo[anonymous]

    Hi,

    1) Creating your custom post template in Publii/sites/your_site/input/themes/mercury-override/post-custom_index.hbs

    2) In your   Publii/sites/your_site/input/themes/mercury-override/config.json

    Insert the “postTemplates” after the “supportedFeatures” block like so:

    "supportedFeatures": {
      "blockEditor": true,
      "tagsList": true,
      "tagImages": true,
      "authorImages": false,
      "searchPage": true,
      "errorPage": true
    },
      "postTemplates": {
      "custom_index": "Custom Index"
    },
    "config": [
      {
        "name": "postsPerPage",
        "label": "Posts per page",
        "value": 11,
        "type": "number"
      },

    You should see “Custom Index” available in the Post settings → Other options.

    Hope this helps.

    #5475
    Avatar photoBob

    I will just add, that the custom templates are dedicated to posts, author, and tag pages only. If you want to override the index.hbs, just copy it to the override folder and modify as needed.

    #5481
    Avatar photo[anonymous]

    Thank you all, it worked.

    [anonymous] wrote:

    I will just add, that the custom templates are dedicated to posts, author, and tag pages only. If you want to override the index.hbs, just copy it to the override folder and modify as needed.

    Well, I can do that, and I did, for some changes. But I need to be able to create a frontpage in Editor, so I can edit it easily. Maybe I miss something again…

    Another question: I would like to add another post template, adding in config.json:

        },
        "supportedFeatures": {
            "blockEditor": true,
            "tagsList": true,
            "tagImages": true,
            "authorImages": false,
            "searchPage": true,
            "errorPage": true
        },
          "postTemplates": {
      "custom_index": "Custom Index",
      "custom_post": "Custom post"
    },

    And creating a file post-custom_post.hbs, and template Custom post in Post options don’t shows up again, am I missing something in syntax again? I’m sorry for these newbie questions

    #5487
    Avatar photo[anonymous]

    endingmirage wrote:

    And creating a file post-custom_post.hbs, and template Custom post in Post options don’t shows up again, am I missing something in syntax again? I’m sorry for these newbie questions

    If you hadn’t reload the site after making the changes to config.json, the Theme  Custom settings won’t be updated yet.

    What I normally do is to switch to a different site (assuming you have created more than one site), then switch back again to the original one you were working on. That way, it forces Publii to load the modified config.json and updates with the changes you’ve made.

    #5489
    Avatar photo[anonymous]

    Thank you, it is working now, seems like restarting app doing the trick too