vscode-twig-language icon indicating copy to clipboard operation
vscode-twig-language copied to clipboard

Twig Block Shorthand Synatx Indentation

Open wolfmoritz opened this issue 5 years ago • 3 comments

The latest release 0.8.7 did not fix the indentation issue with using Twig block shorthand syntax issue #15. I still see unexpected block and HTML indentation. Also, closing HTML tags are often indented past the opening tag, and do not match in column.

For example, this HTML:

<!doctype html>
<html lang="en">

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

  <title>
    {% if block('htmlTitle') is defined %}{{ block('htmlTitle') ~ ' - ' }}{% endif %}
    Administration
  </title>
  <link rel="stylesheet" href="...link-to-file">

  {% block head '' %}
</head>

<body>
  {% include "@admin/includes/_alert.html" %}
  {% block maintTitle 'CMS' %}
  {% block leadText 'Lead text goes here...' %}

  <section id="default" class="content-wrapper">
    <div class="sidebar">
      {% block sidebar '' %}
    </div>

    <main role="main" class="content">
      {% block content '' %}
    </main>
  </section>

  <script src="...link-to-file"></script>

  {% block foot '' %}
</body>

</html>

Is formatted like this:

<!doctype html>
<html lang="en">

    <head>
        <meta charset="utf-8">
        <meta content="width=device-width, initial-scale=1, shrink-to-fit=no" name="viewport">

        <title>
            {% if block('htmlTitle') is defined %}
                {{ block('htmlTitle') ~ ' - ' }}
            {% endif %}
            Administration
        </title>
        <link href="...link-to-file" rel="stylesheet">

        {% block head '' %}
        </head>

        <body>
            {% include "@admin/includes/_alert.html" %}
            {% block maintTitle 'CMS' %}
                {% block leadText 'Lead text goes here...' %}

                    <section class="content-wrapper" id="default">
                        <div class="sidebar">
                            {% block sidebar '' %}
                            </div>

                            <main class="content" role="main">
                                {% block content '' %}
                                </main>
                            </section>

                            <script src="...link-to-file"></script>

                            {% block foot '' %}
                            </body>

                        </html>

Note the extra indent on the {% block leadText %}, and the closing tags on body, section, and div.

wolfmoritz avatar May 28 '19 06:05 wolfmoritz

Thank you for letting me know! There is an issue where block tags don't always need a closing "endblock" so I believe it is a problem/edge case for the formatter to parse. I raised it with the developer and he thought he fixed it but then reverted it when I realised twig has an "endblock" tag. I'll keep you updated and hopefully, we can find a solution soon!

mblode avatar May 28 '19 22:05 mblode

I'm having a similar issue with the {{ head() }} tag, used by CraftCMS

    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1" name="viewport">
    {% block head %}{% endblock %}
    {{ head() }}
  </head>
  <body class="{{ bodyClasses is defined ? bodyClasses|join(" ") : "page" }}">

is adding an extra tag and going all funny

    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1" name="viewport">
    {% block head %}{% endblock %}
  </head>
{{ head() }}</head><body
class="{{ bodyClasses is defined ? bodyClasses|join(" ") : "page" }}">

second save directly after first (no manual file changes) makes it even worse as it adds an extra

into the mix

<head>
    <meta charset="utf-8">
    <meta content="width=device-width, initial-scale=1" name="viewport">
    {% block head %}{% endblock %}
  </head>
  {{ head() }}
</html></head><body
class="{{ bodyClasses is defined ? bodyClasses|join(" ") : "page" }}">

thedavidthomas avatar Jun 06 '19 13:06 thedavidthomas

I just tested my example using version 0.9.0, and the formatting issue appears to be resolved. Thanks!

wolfmoritz avatar Dec 31 '19 11:12 wolfmoritz