Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Is there such a thing as a partial variable?

Started by Muut Archive 11 years ago · 4 replies · 655 views
11 years ago

So I have this:

TWIG
{% include 'partials/nav-page.html' %}

Inside this partial is this:

TXT
page1-link / page1-preview / page1-gallery

I want to output this partial on several pages and change a variable inside the partial depending on the page, eg:

TWIG
{% include 'partials/nav-page.html' variable=“1” %}

AND

TWIG
page{{ variable }}-link / page{{ variable }}-preview / page{{ variable }}-gallery

Would output:

TXT
page1-link / page1-preview / page1-gallery

And

TWIG
{% include 'partials/nav-book.html' variable=“2” %}

Would output:

TXT
page2-link / page2-preview / page2-gallery

Etc

This is a poor explanation, but is this possible? :D

11 years ago

I think the best will be using page headers. See:
http://learn.getgrav.org/themes/theme-vars#headers

So let's say you have markdown page and you want that page to output variable "gallery". In header you put:

YAML
variable: gallery

and in partial template file you just do:

TWIG
{{ page.header.variable }}

This will output "gallery" variable name in your partial file. You can add as many variables as you want.

11 years ago

Oh yeah... thanks. I already use page headers in the page, just didn't think about the partial being able to access them :D

Thanks for the help!

11 years ago

Indeed, what you were asking could also be done like this:
{% include 'template.html' with {'some_variable': page.header.variable} %}
should you ever have need of it.

But like Karol explained, the header variables are available from partials too ;)

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1330 9 years ago
Archive · by Muut Archive, 9 years ago
2 922 9 years ago
Archive · by Muut Archive, 9 years ago
2 4051 9 years ago
Archive · by Muut Archive, 9 years ago
1 2930 9 years ago
Archive · by Muut Archive, 9 years ago
3 1108 9 years ago