question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing data to includes

See original GitHub issue

Twig supports sending data to includes, like so:

{% include 'include.twig' with {
    name: 'whatever'
} %}

This is really useful in many cases, like re-using a block in different contexts, with different behaviour / data. Have anyone looked into adding support for this?

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:109
  • Comments:38 (7 by maintainers)

github_iconTop GitHub Comments

62reactions
neysimoescommented, Mar 30, 2016

Not the best solution, but works for me.

Input:

{% set sectionHeader = { title: 'Title 1', subtitle: 'Subtitle 1' } %}
{% include "_partials/section-header.html" %}

{% set sectionHeader = { title: 'Title 2', subtitle: 'Subtitle 2' } %}
{% include "_partials/section-header.html" %}

_partials/section-header.html

<header class="section-header">
    <h3 class="section-title">{{sectionHeader.title}}</h3>
    <p class="section-subtitle">{{sectionHeader.subtitle}}</p>
</header>

Output:

<header class="section-header">
    <h3 class="section-title">Title 1</h3>
    <p class="section-subtitle">Subtitle 1</p>
</header>

<header class="section-header">
    <h3 class="section-title">Title 2</h3>
    <p class="section-subtitle">Subtitle 2</p>
</header>
21reactions
fdintinocommented, May 27, 2017

As the new maintainer I’m slowly making my way through old tickets. This seems like something worth adding.

Read more comments on GitHub >

github_iconTop Results From Across the Web

php - Laravel - How to pass data to include - Stack Overflow
I need to pass data to the header on every single view. Is there a way to pass data just to the include...
Read more >
Passing Data with a Twig Include Statement - CraftQuest
The Twig include statement is a fantastic little tool because it allows us to chunk our template code into smaller, reusable pieces.
Read more >
Passing post variables to includes in Jekyll - sverrirs.blog
In Jekyll includes do not have access to variables defined in your posts or pages. But don't worry here is how you can...
Read more >
Passing data to includes - overrides & defaults
What you need to do in your include is to check, if the variable was passed in from the outside, and if not,...
Read more >
Passing variables between pages using URL GET or POST ...
There are different ways by which values of variables can be passed between pages. One of the ways is to use the URL...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found