Skip to content

40%off everything for Black Friday, with code . Until Dec 1.

Bright Themes

How the block and contentFor helpers work in Ghost

{{{block}}} leaves a named slot in a Ghost layout, and {{#contentFor}} fills it from a template. Use them to add scripts, classes or meta tags to one kind of page only.

Norbert2 min read

Most Ghost themes have one layout, default.hbs, with the HTML every page shares. Templates such as post.hbs or page.hbs fill it with their content. The {{{block}}} and {{#contentFor}} helpers let a template put content in other places of that layout too: a script before </body>, a class on <body>, a tag in <head>.

The block helper

{{{block "name"}}}, with three curly braces, leaves a named slot in a layout. Give each slot a unique name:

Handlebars
<!-- default.hbs -->
<body>
  ...
  {{{block "scripts"}}}
</body>

When no template fills the slot, Ghost leaves it out, so a layout can have as many slots as you like.

The contentFor helper

{{#contentFor "name"}} … {{/contentFor}} fills a slot. It goes in a template that uses the layout, which the template names with {{!< default}} on its first line:

Handlebars
<!-- page.hbs -->
{{!< default}}

{{#contentFor "scripts"}}
  <script>
    runPageScripts();
  </script>
{{/contentFor}}

When Ghost renders a page with this template, the script appears where {{{block "scripts"}}} is in default.hbs. Pages that use other templates don’t get it.

The body helper

{{{body}}} works like a block that every template fills without contentFor: it is where the rest of the template goes.

Handlebars
<!-- default.hbs -->
<div class="site-wrapper">
  {{{body}}}
</div>
Handlebars
<!-- post.hbs -->
{{!< default}}

<article class="post-content">
  {{content}}
</article>

Note

A file with {{{block}}} slots is a layout, and Ghost can’t use it directly as a template. post.hbs, page.hbs and index.hbs can use a layout and fill its slots, but they can’t hold slots of their own.

When to use them

  • Scripts for one kind of page. Load a script only where it’s needed, for example a table of contents or share buttons on posts, so other pages stay light. The featured posts slider tutorial loads its library this way, on the homepage only.
  • Classes on the body or other tags. Add a slot inside an attribute, such as <body class="{{body_class}} {{{block "body_class"}}}">, and fill it from a template to style that template differently.
  • Tags in the head. Leave a slot in <head> and fill it from a template, for example with a preload for that template’s hero image or a meta tag only those pages need.
  • Optional sections. Leave a slot for an ad unit or a banner in the layout, and fill it only in the templates that should show it, inside a {{#match}} or {{#if}} when it depends on a condition.

Because the content stays in the template that needs it, the layout stays the same for every page and is easier to maintain. For code you add in Ghost Admin rather than in the theme, use code injection instead. For how layouts, templates and partials fit together, read Ghost theme structure, explained. To add conditions, see how the match helper works.

Related posts

More on helpers

Get the Bright Themes newsletter

New tutorials and explainers, what changed in Ghost, and our theme releases. No spam, unsubscribe anytime.

Get the Bright Themes newsletter

New tutorials and explainers, what changed in Ghost, and our theme releases. No spam, unsubscribe anytime.