Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce generic twig tags #154

Merged
merged 6 commits into from
Feb 27, 2020
Merged

Conversation

twbartel
Copy link
Collaborator

@twbartel twbartel commented Feb 25, 2020

The main thing about this merge request is that it adds parsing capability for arbitrary Twig tags. This capability is activated through an option allowUnknownTags.

Background: If we only want to parse a Twig file, without executing it, we don't need custom parsers for all kinds of Twig tags. In this case, Melody can generate AST nodes of type GenericTwigTag for Twig tags it does not know.

This is good enough for "lone" Twig tags like {% exit 404 %}. However, sometimes, pairs or even groups of Twig tags belong together. Example:

{% nav entry in entries %}
    <li>
        <a href="{{ entry.url }}">{{ entry.title }}</a>
        {% ifchildren %}
            <ul>
                {% children %}
            </ul>
        {% endifchildren %}
    </li>
{% endnav %}

Here, {% nav %} and {% endnav %}, as well as {% ifchildren %} and {% endifchildren %} belong together.

In order to have such tags adequately represented in the AST, they have to be made known to the Melody parser beforehand. The new option multiTags is used for that:

{
    allowUnknownTags: true,
    multiTags: { 
        nav: ['endnav'],
        ifchildren: ['endifchildren'],
        switch: ['case', 'default', 'endswitch']
    },
}

multiTags is an object whose keys are the tag names of the first in a sequence of tags (here, nav and ifchildren), and whose values are arrays containing the other tag names that can occur in the sequence. It's important that the concluding tag name comes last in this array. Other than that, the order does not matter.

@twbartel twbartel merged commit 26ba7bd into trivago:master Feb 27, 2020
@twbartel twbartel deleted the generic-twig-tags branch February 27, 2020 11:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant