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

Make resulting HTML more readable #167

Merged
merged 1 commit into from
Dec 20, 2024
Merged

Make resulting HTML more readable #167

merged 1 commit into from
Dec 20, 2024

Conversation

jean-gui
Copy link
Contributor

@jean-gui jean-gui commented Dec 19, 2024

This PR is a proposal to improve the generated markup with better nesting and indentation. It consists in three things:

  • when using a control block (for or if for example), put them at the same level as their enclosing HTML element
  • use the ~ modifier on the left part of those blocks
  • use {# -#} after HTML markup to remove white spaces and new lines we may want for readability of the template, but that are not necessary in the HTML

Some downsides:

  • this is not how PHPStorm indents Twig by default (I don't know if that can be changed)
  • templates are a little bit less readable because of the lack of indentation between a control block and its parent HTML element
  • {# -#} blocks clutter templates a bit

@jean-gui jean-gui force-pushed the template_indentation branch 2 times, most recently from 61a89d5 to 6cec2bb Compare December 19, 2024 15:47
@deniak
Copy link
Member

deniak commented Dec 20, 2024

Based on a symfony blog post, there's another option using the spaceless filter instead of using the {# -#} which I'm not a big fan of:

<ul class="clean-list" role="list">
{%~ for link in links %}
    <li>
    {%- apply spaceless %}
        <a href="{{ link.url }}" hreflang="{{ 'footer.links.hreflang'|trans({}, 'w3c_website_templates_bundle') }}">
            {{ link.copy|raw }}
        </a>
    {% endapply -%}
    </li>
{%~ endfor %}
</ul>

That said, I'm not sure if making the HTML more readable is worth the hassle and the inspector already indents the code properly.

@jean-gui
Copy link
Contributor Author

Based on a symfony blog post, there's another option using the spaceless filter instead of using the {# -#} which I'm not a big fan of:

The problem is that the spaceless filter is also deprecated and will be removed in Twig 4, see https://twig.symfony.com/doc/3.x/filters/spaceless.html

That said, I'm not sure if making the HTML more readable is worth the hassle and the inspector already indents the code properly.

@jean-gui jean-gui force-pushed the template_indentation branch from 6cec2bb to 8fd186e Compare December 20, 2024 08:55
@jean-gui jean-gui merged commit 5c5fdf3 into main Dec 20, 2024
@jean-gui jean-gui deleted the template_indentation branch December 20, 2024 10:59
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.

3 participants