The following sample was tested with v1.9.4 and 1.12.1 - both exhibit the same behaviour (project is running 1.9.4, and 1.12.1 is latest version at the time this issue is being raised)
Issue #166 appears to be cover the same issue, but that is reported as fixed back in v1.0
I have been through the documentation, and there appears to be no way to disable the DJLint formatter (the 'djlint:off' comment appears to only disable linting, not formatting?).
The issue:
I'm embedding some Jinja2 code inside a script block, and unfortunately the embedded Javascript formatter is butchering the code badly... so I've used the 'beautify preserve:start/end' pragmas to disable the javascript formatting... which works.
However, when I disable the javascript formatting, the djlint formatter starts adding extra indentation on every execution.
Sample code (entire contents of 'formatter_test.html.j2' file):
{% macro generate_clear_script(ids) %}
<script type="text/javascript">
/* beautify preserve:start */
function reset_adv_search_form() {
{%- for id in ids %}
$('#{{ id }}').val([]);
$('#{{ id }} option').prop("selected", false);
{%- endfor %}
$('#from_date').val('');
}
/* beautify preserve:end */
</script>
{% endmacro %}
After the first iteration:
{% macro generate_clear_script(ids) %}
<script type="text/javascript">
/* beautify preserve:start */
function reset_adv_search_form() {
{%- for id in ids %}
$('#{{ id }}').val([]);
$('#{{ id }} option').prop("selected", false);
{%- endfor %}
$('#from_date').val('');
}
/* beautify preserve:end */
</script>
{% endmacro %}
After the second iteration:
{% macro generate_clear_script(ids) %}
<script type="text/javascript">
/* beautify preserve:start */
function reset_adv_search_form() {
{%- for id in ids %}
$('#{{ id }}').val([]);
$('#{{ id }} option').prop("selected", false);
{%- endfor %}
$('#from_date').val('');
}
/* beautify preserve:end */
</script>
{% endmacro %}
And third iteration:
{% macro generate_clear_script(ids) %}
<script type="text/javascript">
/* beautify preserve:start */
function reset_adv_search_form() {
{%- for id in ids %}
$('#{{ id }}').val([]);
$('#{{ id }} option').prop("selected", false);
{%- endfor %}
$('#from_date').val('');
}
/* beautify preserve:end */
</script>
{% endmacro %}
I've tested this behaviour up to 15 iterations, at which point I stopped. Every iteration, the code ends up further and further indented.
Note: Also tested the same code using 'djlint:off/on' comments - had no impact (same indenting behaviour observed). test code with djlint:off/on comments, for reference:
{% macro generate_clear_script(ids) %}
{# djlint:off #}
<script type="text/javascript">
/* beautify preserve:start */
function reset_adv_search_form() {
{%- for id in ids %}
$('#{{ id }}').val([]);
$('#{{ id }} option').prop("selected", false);
{%- endfor %}
$('#from_date').val('');
}
/* beautify preserve:end */
</script>
{# djlint:on #}
{% endmacro %}
Current workaround: I've explicitly added the impacted file to the exclusion list in the .djlintrc file, but this is sub-optimal, as it means that none of the code in the file is validated as part of the CI/CD pipeline.
released