| Crates.io | ludtwig |
| lib.rs | ludtwig |
| version | 0.10.0 |
| created_at | 2021-01-31 11:43:59.887018+00 |
| updated_at | 2025-02-20 19:01:30.321176+00 |
| description | Linter / Formatter for Twig template files which respects HTML and your time |
| homepage | |
| repository | https://github.com/MalteJanz/ludtwig |
| max_upload_size | |
| id | 348835 |
| size | 177,453 |
Linter / Formatter for Twig template files which respects HTML and your time.
--fix.ludtwig-ignore to ignore whole files completely (like your .gitignore)ludtwig-config.toml (use -C to create one) to configure the rules for your project / adjust the code style{%- ... -%} whitespace removal braces are not yet supported,
see #56Run the following cargo command to install or update ludtwig:
cargo install ludtwig
You can install cargo here
If you don't want to install the rust toolchain / cargo you can still use the other methods below.
Download the latest release binary for your operating system and put it in your PATH for easy access.
Have a look at the docker image or try it with the following command to
lint the current working directory:
docker run --rm -v $(pwd):/ludtwig maltejanz/ludtwig:latest ludtwig .
You can also use the nix package.
After the installation have a look at ludtwig --help for more information. It should be self-explanatory.
Also have a look at the default config file if you want to customize the way how ludtwig analyses your files.
To create it in your current working directory run ludtwig -C.
To prevent many cases of creating invalid / dirty HTML by Twig, ludtwig only allows the Twig syntax in certain places. Without this restriction it wouldn't be possible to parse the combined syntax in a single hierarchical syntax tree. Have a look at the example below to get the general idea where Twig syntax is allowed:
{% 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 %}
Ludtwig doesn't accept and will produce parsing errors for any Twig syntax that could cut off HTML in obvious ways, e.g.:
{% if condition %}
<div class="container">
{% endif %}
Some content
{% if condition %}
</div>
{% endif %}
produces the following parsing error:
...
error[SyntaxError]: The parser encountered a syntax error
┌─ bad-example.html.twig:3:1
│
3 │ {% endif %}
│ ^^ expected </div> ending tag but found {%
...
This is intentional, as the example above is very error-prone to maintain correctly and this trade-of allows to represent both HTML and Twig syntax in a single hierarchical syntax tree. In contrast, the Twig PHP compiler mostly treats all other syntax that isn't Twig, as plain text, which comes with the benefit that Twig can be written between any character, which isn't that useful in practice when writing HTML templates, but can be if you're not writing a template for HTML.
In general, you only want to write valid HTML (full elements) between your Twig syntax (
{% ... %}). With some exceptions for HTML attributes.
Copyright (c) 2025 Malte Janz
ludtwig is distributed under the terms of the MIT License.
See the LICENSE file for details.
For testing purposes this repository also includes code from the following sources (not included in distributed binary):