Download

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.

What are you waiting for?

Start building your site today.

  1. 1 Download Publii
  2. 2 Write your content
  3. 3 Publish your site
Create website