This twig whitespace control thing looks very interesting and I think (was thinking) it can help us to output clean and good looking html. See the section about this in the Twig Coding Standards: http://drupal.org/node/1823416
But I can't get it really working in a way so we have
1) right intendations, whitespaces and linebreaks in the html output
and
2) good looking code in the templates as well.
For example the breadcrumb.html.twig template:
{% if breadcrumb is defined %}
<nav class="breadcrumb" role="navigation">
<h2 class="element-invisible">{{ 'You are here' | t }}</h2>
<ol>
{% for item in breadcrumb -%}
{% if loop.first -%}
<li>{- item -}} »</li>
{% elseif loop.last -%}
<li>{{- item -}}</li>
{% else -%}
<li>{{- item -}} »</li>
{%- endif -%}
{%- endfor -%}
</ol>
</nav>
{% endif %}
returns something like
<nav class="breadcrumb" role="navigation">
<h2 class="element-invisible">You are here</h2>
<ol>
<li><a href="/drupal8_twig/" class="link">Home</a> »</li>
<li><a href="/drupal8_twig/node/1" class="link">test</a></li>
</ol>
</nav>
but it should be of course:
<nav class="breadcrumb" role="navigation">
<h2 class="element-invisible">You are here</h2>
<ol>
<li><a href="/drupal8_twig/" class="link">Home</a> »</li>
<li><a href="/drupal8_twig/node/1" class="link">test</a></li>
</ol>
</nav>
I didn't find a way to do this, even when removing any indents in the template file. Am I missing something?
Comments
Comment #1
steveoliver commentedCriz, I updated the Twig Coding Standards Whitespace section last night in an attempt to help illustrate how to use the whitespace modifier. It does take a little trial and error, but this should produce the indentation you're after:
I tested it locally and it seems the item links themselves are outputting linebreaks -- it seems we don't preprocess theme_breadcrumb at the moment.
Comment #2
crizthanks steveoliver for the fast reply.
yes, for testing purposes I used {% spaceless %} to remove the link whitespaces. Your code would be
but gives me again
can you reproduce this?
Comment #3
steveoliver commentedYeah, this is weird.
produces this:
So what the heck is
</ol>doing up on the same line as the<li>s? Is this a bug with twig engine?Comment #4
dasjoas stated in irc, having full white-space control seems not to be possible with twig atm.
reference: https://github.com/fabpot/Twig/pull/645
Comment #5
criz@dasjo
I am not sure, the example in the twig pull request is a little bit different. IMO this should be working, at least I would expect it.
Comment #6
steveoliver commentedI wish this worked as expected. Marking postponed on upstream Twig.
Comment #7
steveoliver commentedUpdate: whitespace control works about ~95% as expected. I've experienced a few strange issues in the past with whitespace apparently being affected by previous lines, but the referenced github (pull request 645) issue isn't wasn't exactly the issue I experienced (as criz pointed out) -- that issue is about removing trailing whitespace AND the preceding newline, which I don't find helpful at the moment. Experiment with whitespace control and report back with any issues (I will). Keeping active until we're 100% sure it works correctly.
Comment #8
jenlamptonJust adding a note here that in Drupal core the goal is to use as few whitespace controllers as possible (but of course you can add as many as you like in contrib, or your own custom themes).
This was a request from Angie, since everyone who sees the hyphens in the template files thinks they are typos at first glance. So, wherever possible we should just delete the whitespace instead of using a twig whitespace controller.
Maybe this can also help us with the last 5% where Twig is not working for us anyway :)
Comment #9
c4rl commentedIn my opinion, the prevalence of the typo perception is inversely proportional to the frequency of the syntax in question. Rephrased: If we use whitespace control frequently, people will be less likely to assume these are typos.
Comment #10
jenlampton"At first glance" means that the viewer has only ever seen one whitespace controller. It doesn't matter how many of them we put all over core, because the first impression still only happens once. Every existing Drupal developer (front or back end) will still have a first impression with Twig. :)
Now, that doesn't mean we can't use this first impression as a teaching experience, and use that moment to send them scurrying to read the documentation. But, in a perfect world, it shouldn't be necessary for people to read documentation on Twig before they do anything with it.
I want theme development for D8 to be intuitive initially, and only require heading to the documentation when something truly complex is needed. If we can postpone the "WTF" moments, and instead have the first impression be "Oh, I get it!" I think that would be a huge win :)
Comment #11
steveoliver commentedAt first I agreed with c4rl 100%, but jen makes a great point about the initial impression. whitespace control is actually a second-level or second degree of theming concern. most people see code nowadays parsed by Firebug etc., so I think:
Comment #12
c4rl commentedThis isn't a "perfect world." This is reality. The reality of template theming in Drupal 8 is that it is going to be a huge win over theming in Drupal 7 for reasons very familiar to you and me (data types, printing methods, move from concatenation to templates). Whitespace controllers are not difficult to grok, not nearly as difficult as it was to grok all of the inconsistencies in Drupal 7. So, whether we hit a home run with whitespace controllers or not, we will be in a much, *much* better world than D7, though not "perfect."
I am, at this point, a newcomer to Twig (but familiar with Drupal, PHP, and HTML), as I am guessing are most people in this thread and most people who contributed to http://drupal.org/node/1823416. I am hesitant at this point to make sweeping generalizations or rules about how to use whitespace modifiers as is proposed on http://drupal.org/node/1823416 due to this inexperience, and furthermore find the "wrong" example on that node much more legible than the "correct" method.
Because of this inexperience, and the difference in opinion, it is therefore my recommendation that we should avoid including any declaration about whitespace controllers in the coding standards until we understand what problems we are trying to solve. Assuming what themers want has not been productive in the past.
It is also my fear that "Dos and don'ts" will be misinterpreted, i.e.
Well what about this:
If
attributes.classis empty, you will have a trailing space unless you use the modifier. In PHPTemplate we had to do something like:...in order to achieve the same result.
Let's face it: PHPTemplate lacks native whitespace controls entirely: If you want legible templates, you will have illegible HTML; If you want legible HTML, you will have illegible templates. We're used to this. So Twig whitespace controllers are a huge win, and easy to grok (imho). We didn't tell people in the coding standards before how to use `if` statements to check for classes. Why would use make such sweeping declarations about whitespace controllers?