Partials

Timothy Kempf edited this page Oct 15, 2013 · 12 revisions

A partial is a Handlebars view that is included inside pages, layouts, or even other partials. Partials are generally used encapsulate markup that is used across a number of pages, or simply to "clean up" complex markup by moving portions to their own files.

======

Creating a Partial

Technically, any *.hbs file inside of /views can be used as a partial. This is useful in cases when you want a standalone page that can also be included within other pages as a partial. Partials don't require any special bits like pages or layouts, just markup.

Partials are included in your views by using {{> path/to/partial}}. Say you have the following file structure:

/views
├── index.hbs
├── layout.hbs
└── header.hbs

If you want to include header.hbs as a partial in layout.hbs, just do this in layout.hbs:

<!DOCTYPE html>
<html>
    ...
        <!-- Header -->
        {{> header }}
    ...
</html>

This will render the contents of header.hbs inside of layout.hbs where you place {{> header }}, using the full context of the page.

======

Passing a Context to a Page

Sometimes, you won't want to pass the entire page context to your partial, but instead a specific part of it. In order to pass a portion of the context to your partial, just specify it as the second argument when you include the partial:

<!DOCTYPE html>
<html>
    ...
        <!-- Header -->
        {{> header page }}
    ...
</html>

This will make the partial use only the contents of the page portion of the context, rather than the entire thing.

NEXT: Assets

======

Solidus is still under development. If you have comments or questions, please reach out to us in our IRC channel: #solidus on irc.freenode.net

======

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.