Cigol
Cigol

Reputation: 105

reusing handlebars & panini partials with different data

I've a number of 'building blocks' I've created of my own to use when templating a site and I'm wondering how I could use Panini to re-use partials on the same page, with different data.

Say for example I have a partial which basically adds a h1 tag followed by a single p tag but I want to be able to re-use that same partial on the same page with different data each time.

This is the content of the partial file for example;

<h1> {{ h1Header }}</h1>
<p> {{ pParagraph }} </p>

The Frontmatter data in the Index file;

---
h1Header: Hello!
pParagraph: This is some text.
---

And this to call the partial into action;

{{> partial }}

Unless I'm doing something fundamentally wrong the way I'm using it at the moment would mean I'd have to create several different partials for each possible outcome.

I was wondering if there was some way of sending arguments etc. If someone can point me in the right direction with even the simplest of examples just to get a feel of what I can do and what to look into I'd be grateful.

Upvotes: 0

Views: 1185

Answers (1)

Kurztipp
Kurztipp

Reputation: 411

You can pass data to your partials passing a context or parameters to your partial. You can pass different data every-time you render the partial, according to the manual.

Having a partial called test:

<h1>{{foo}}</h1>

You can render it with specific data:

{{> test foo="bar"}}
{{> test foo="foobar"}}

Which results in

<h1>foo</h1>
<h1>foobar</h1>

Upvotes: 2

Related Questions