GitHub

Creating custom partials

Theme developers can create custom partials in their themes. It is handy if you want to reuse your code in many views.

To create a custom partial just create a *.hbs file in the theme partials directory. The name of the partial file will be used in the theme code.

E.g., if you create partials/cookiebar.hbs file in your theme, then you can use it in other files as:

{{> cookiebar}}

It is also possible to create partials which have params - this is especially useful when you have problems with context.

Let's analyze the following situation:

We have the following context for a view:

{
    config: {
        optionValue: true 
    },
    pages: [
       { title: "Lorem" },
       { title: "Ipsum" }
    ]
}

And we load our partial in the main file as follows:

{{#each pages}}
    {{> page}}
{{/each}}

We can access {{title}} in our partial, but {{config}} is unavailable even if we use {{../config}}.

The solution is creating a param for our partial:

{{#each pages}}
    {{> page config=../config}}
{{/each}}

In the above example, we will be able to access {{config}} in our partial.

Subscribe

Get the latest Publii news, updates and more delivered directly to your email inbox

You can change your mind at any time by clicking the unsubscribe link in the footer of any email you receive from us, or by contacting us at contact@tidycustoms.net. By clicking below, you agree that we may process your information in accordance with our Privacy Policy.