Patternlab twig includes with params - twig

I am using the node version of patternlab with twig as template engine. I am using twig because my codebase is written in twig - so using mustache it not an option.
I simple try to include an pattern:
<button type="button" class="btn {{ buttonClass }}">{{ cta }}</button>
in an other pattern with:
{% include "base-button" with {'buttonClass': 'btn-primary btn-xs'} %}
This does not work. The pattern is included but the variable buttonClass is not set.
Any Ideas?

This seems to be an issue with the twig integration in this repo https://github.com/pattern-lab/patternengine-node-twig.
There are other issues with twig core features described here: https://github.com/pattern-lab/patternlab-node/issues/554
I finally decided to switch to the php/twig edition and the same twig-patterns are working as expected. If anybody have to use the node edition, i recommend to use mustache.

I believe there are plans to pass the Twig rendering in Pattern Lab Node to PHP, however, this isn't ready yet, but using The Twig engine for Patternlab/Node you can pass a variable via an include. It doesn't work exactly the same as normal twig but it does work.
{% set buttonClass = 'btn-primary btn-xs' %}
{% include "components-button" with buttonClass %}
You can also pass multiple variables:
{% include "components-button" with buttonClass anotherVariable %}

Related

Mustache and Twig: not escaping HTML

I've got Twig and Mustache combined. Since they both use same tags for bookmarks
{{ var }}
I changed Mustache tags, like this
Mustache.parse(template, ['${', '}']);
So I use this bookmark in Mustache
${ var }
And there are no conflicts.
The problem comes out when I need to tell Mustache not to escape HTML. As the Documentation says, I am to use
{{{ do_not_escape_this_var }}}
But this leads to a conflict with Twig! And It seems like there is no way tu customize it.
I know I could override Twig's bookmarks, but this is bad for the frontend team, used to Twig default tag.
Any solutions? Thank you
Like the Twig's documentation says, code can be escaped like this:
{{ '{{{ do_not_escape_this_var }}}' }}
For larger blocks of code, you can use verbatim to prevent Twig from parsing Mustache code as Twig code:
{% verbatim %}
{{{ do_not_escape_this_var }}}
{% endverbatim %}
With verbatim, you could also avoid parsing chunks of Mustache code with the default Mustache tags:
{% verbatim %}
<h1>Hello {{ name }}!</h1>
<p>How are you {{ today }}?</p>
{{! This is just Mustache, not Twig }}
{% endverbatim %}
Of course, then you can't use Twig between {% verbatim %} and {% endverbatim %}. I don't think it's a problem as mixing Mustache (with the default tags) and Twig code would be very confusing.

Automatically declare variables used in Twig template

I am refactoring some old twig templates, where many variables are passed to the templates but not declared in doc comments (which also work in PhpStorm for type hinting), like this:
{# #var user AppBundle\Entity\User #}
{# #var message string #}
<p>Hello {{ user.fullName }}!</p>
<p>{{ message }}</p>
Is there a tool that can pre-generate these doc comments from variables used in a template, ideally usable as PhpStorm plugin?
It will be good enough if it would extract just variable names without types (which are hard to guess from Twig syntax), just for me to make sure that I have not overlooked some variables.
Also, it will be great if it would not declare variables created in the template, like this:
{# #var users AppBundle\Entity\User[] #}
{# "user" variable is not documented as it is not passed into the template from outside #}
{% for user in users %}
<p>Hello {{ user.fullName }}!</p>
{% endfor %}
Such comments are helpful not only for type hinting, but also for the developer to see what should be passed into the template when refactoring/reusing the template.
Thanks in advance.
As far as I can see there's unfortunately nothing that IDE or available plugins could suggest for your use case.

How to push values to array in templates and partials to output them in layout?

We are using Twig as our templating engine but I'm stuck with a problem of the rendering order now. See - https://straightupcraft.com/articles/twig-processing-order
We are using amp for our new site and with amp and web components in general it is necessary to import the components (once) to use them. So the idea was to create a Twig Extension to accomplish exactly this. Everytime a component is used it is called via this twig function call. The function is checking the component, adding it to a stack and you can use it from now on. The stack then is transformed into a list of imports for the head of the page.
The problem here is Twigs top to bottom rendering. When I call said function in a partial or a template the rendering of "layout.twig" already took place and all function calls below/after are not affecting the stack.
Is there an option to change the rendering to inside out, so that the rendering is first using the lowest partial and "bubbles" up to templates and finally layout without them rendering before?
Another way to get the values in layout would nice as well.
Desired behavior:
Layout.twig
{{ getAllComponents() }}
{# should output component1 and component2 #}
{% block content %}
Page.twig
{% extends Layout.twig %}
{{ addComponent('component1') }}
{% include partials.twig %}
<component1>...</component1>
partials.twig
{{ addComponent('component2') }}
<component2>...</component2>
Actual behavior:
Layout.twig
{{ getAllComponents() }}
{# is empty #}
There is no option to change this behavior. However you can use a trick to archive this:
Render your content to a variable before you call getAllComponents()
layout.twig
{% set mainContent %}
{% block content %}{% endblock %}
{% endset %}
{{ getAllComponents() }}
{{ mainContent }}
There actually is a magical solution to this problem -- some genius wrote this Deferred Twig Extension which lets you run some code (output a variable, call a function, etc) in a block near the top of your layout, but wait until the rest of the twig templates have parsed before executing them. There's an example on that github page showing how you can achieve this.
As far as I know, no you cannot switch the rendering of a TWIG template.
However, I would recommend possibly looking at the {% embed %} or {{ render }} tags.
Embed allows specific TWIG files to be placed within others with variables, so you could pass each component into their relevant partials.twig file.
Render allows the use of embedding a separate controller action, so you create a ControllerAction that took in a component variable and renders its own TWIG file.
Documentation on Embed: https://twig.sensiolabs.org/doc/2.x/tags/embed.html
Documentation containing Render: https://twig.sensiolabs.org/doc/2.x/api.html

Wrap contextual html around a specific twig variable {{ product.name }}

I want to automatically wrap some html, lets say <span data-id=".."> when I call {{ product.name }} in my twig template.
So when in a twig template, I use {{ product.name }}, I want the output to be: <span data-type="product" data-id="8" data-prop="name">My product name</span>. I cannot use twig filters or macros, since I really need the template syntax to be {{ product.name }}, so the end-user (template designer), does not have to care about it.
The reason I need this is because I am building an on-page editting tool for twig templates, so I need to know the contexts of those variables from within HTML.
I have tried to override the Compiler that the Twig_Environment uses, but I cannot seem to alter the output of the twig variable node.
How can I do this?
EDIT
I wanted to mention that I need this to use the {{ product.name }} syntax, since other designers will work with those templates outside of Symfony 2. I want to make almost all twig variables editable in the front-end, so a solution with filters or macros can indeed work, but it kills the usability and readability of the platform I am writing. There is no public API currently in twig that can achieve what I want, that is why I am fiddling with the twig compiler. I do not have the required knowledge of the Twig internals to achieve this. If someone could point me into a direction that would be great!
UPDATE 2
I have found a place where I can achieve what I want. Every GetAttr node is compiled to $this->getAttribute($someContext, "property"). So if I can change the subclass of compiled twig template, I can achieve what I want. By default all twig templates extend from Twig_Template. I want to extend this method.
How can I change the subclass of all compiled twig templates?
UPDATE 3
I've found a way to use my own base class for all compiled twig templates. I can simply set it as an option on the twig environment, see this link. I hope to get it working tomorrow and I will post a proper answer on how I solved it all together. Not sure how I will handle escaping, since that will happen after the $this->getAttribute() is called.
I think macros are the best candidates for those kind of wrappings.
For example:
main.twig
{% import "macros.twig" as macros %}
{{ macros.display_product(product) }}
macros.twig
{% macro display_product(product) %}
<span data-id="{{ product.id }}" data-prop="name">{{ product.name }}</span>
{% endmacro %}
Context
product:
id: 8
name: My Georgeous Product
Result
<span data-id="8" data-prop="name">My Georgeous Product</span>
See fiddle
I've solved it by wrapping the PrintNode that is created when parsing a VAR_START token (inside the twig parser) with my own EditablePrintNode. In that node I traverse the expression that is compiled by the print node and get the necessary property path and pass that as an argument to a wrapper function around the default twig escape function that is compiled by the PrintNode.
I suggest to write a custom filter. You can find the doc here about how to write and configure in your environment
// an anonymous function
$filter = new Twig_SimpleFilter('my_custom_product_filter', function ($product) {
return '<span data-id="'.$product->getId().'" data-prop="name">'.$product->getName().'</span>';
});
You need to register as described in the doc
then you can use as follow:
{{ myProduct|my_custom_product_filter}}
Hope this help

Twig - Do not interpret

I'm looking for a tag which would enable me to stop interpreting in a section, in a template.
So I'm looking for something that is similar to smarty's literal tag.
Thanks
I think you're looking for {% verbatim %} / {% endverbatim %}. In earlier versions these were called {% raw %} / {% endraw %}.
See also Twig Escaping.
In Twig, it is now verbatim, see http://twig.sensiolabs.org/doc/tags/verbatim.html

Resources