Search code examples
twigpatternlab.io

Data inheritence in twig pattern lab


I am using twig Pattern Lab includes with the longhand syntax (needed for integration reasons), aka {% include "@atoms/path/to/pattern.twig" %}.

If I have variables defined in a pattern, and then include that pattern in another pattern, the included pattern's variables are set to null, unless I pass in a new context with with.

For example:

my-atom

<p>{{ myvar }}</p> {# myvar is set when viewing my-atom directly #}

my-molecule

<div>{% include "@atoms/my-atom/my-atom.twig" %}</div> {# Prints an empty <p> tag #}

<div>{% include "@atoms/my-atom/my-atom.twig" with { myvar: "Hello!" } %}</div> {# Prints "<p>Hello!</p>" #}

I would expect the variables from the molecule to flow down to the atom, falling back to the atom's values if the variable isn't defined in the molecule, similar to how the short hand syntax works (aka, {% include "atoms-my-atom" %}).

Is this a feature? A bug? Is there something I'm missing?

Thanks so much in advance!


Solution

  • It is working as intended as far as I know, at least on Symfony it works that way, I dunno the reason behind it tho. This is why I prefer to render those partial views in my controller and pass them to the main view as variables.