Skip to content

Partials

Devin edited this page Jun 29, 2020 · 7 revisions

Partials let you components across your website without having to duplicate the same logic. By default, they live in the views/partials/ directory of your project.

Using partials

You can use partials in any of your pages by calling the partial method. It takes two parameters:

  • partial (required) — The path to your partial, relative to the partials/ directory.
  • data (Optional) — A hash with data to expose to the partial. Keep in mind that the partial is already aware of all variables available in the page where it's called.

For example, to render the partial views/partials/videos/youtube.erb, you can use the following code:

partial('videos/youtube.erb', :video_code => 'dQw4w9WgXcQ')

You can also wrap the data in a :locals key:

partial('videos/youtube.erb', :locals => { :video_code => 'dQw4w9WgXcQ' })

In both cases, you can use the data as variables in your partials. For example, in your views/partials/videos/youtube.erb file, you can access :video_code like this:

<a href="https://www.youtube.com/watch?v=<%= video_code %>">Open the video</a>

Templating languages

Partials, like pages, can leverage all the templating languages that Frontman supports. Make sure to include all the extensions in the partials name, so that Frontman can associate the correct templating language(s) with it.

Configuration

If you want to change the partials directory, you can set the :partial_dir configuration value in your config.rb file to point to a new directory.

Frontman::Config.set(:partial_dir, 'my-partial-dir/')

Clone this wiki locally