12 releases

0.6.0 Nov 8, 2024
0.5.3 Mar 1, 2024
0.5.2 Feb 10, 2024
0.5.0 Sep 13, 2023
0.2.1 Jan 31, 2021

#2623 in Parser implementations

Download history 10/week @ 2024-08-24 7/week @ 2024-08-31 10/week @ 2024-09-07 11/week @ 2024-09-14 58/week @ 2024-09-21 43/week @ 2024-09-28 21/week @ 2024-10-05 21/week @ 2024-10-12 6/week @ 2024-10-19 65/week @ 2024-11-02 50/week @ 2024-11-09 18/week @ 2024-11-16 23/week @ 2024-11-23 17/week @ 2024-11-30 82/week @ 2024-12-07

142 downloads per month
Used in ludtwig

MIT license

635KB
15K SLoC

Ludtwig-Parser

GitHub Crates.io Crates.io

Rust crate that parses Twig / HTML templating syntax into a lossless syntax tree. It does not conform to the official HTML spec and the input is required to be as idiomatic as possible (but the parser still tries to recover from errors / parse as much as possible). For example missing closing tags in HTML result in a parsing error (even if browsers can interpret the HTML and reconstruct the closing tag). This makes it possible to represent the template in a hierarchical untyped tree which is easy to navigate and contains both Twig and HTML syntax.

Accepted syntax example

{% block my_component %}
    {% set isActive = true %}
    <div id="my-component"
         class="my-component {% if isLarge %}large{% endif %}"
         {{ dataAttribute }}="data"
         {# Single word attributes don't strictly require quotes #}
         data-active={{ isActive }}
    >
        {% block my_component_inner %}
            <span id="my-span"
                  {% if isActive %}
                      style="color: red"
                  {% endif %}
            >
                hello {{ name }}
            </span>
        {% endblock %}
    </div>
{% endblock %}

Disclaimer

It is developed together with the ludtwig CLI application for formatting and analyzing Twig template files.

License

MIT - see LICENSE file.

License notices

For testing purposes this repository also may include code from the following sources:

Dependencies

~5.5MB
~69K SLoC