(As promised, my experience-based opinion.)
First of all, if you can prevent it: do not mix languages, whenever possible.
As per your example, it seems like you are mostly wondering about how it should be done in a templating language mixed with HTML, which I will cover. (Another case would be building SQL using another programming language, which would require completely different rules).
As an example, I'll cover Django (but PHP, Twig, etc) work mostly the same. Django has logical blocks in the templates (open {% tag %}
, closing {% endtag %}
). HTML has logical blocks (open <tag>
, closing </tag>
). Here I mostly follow this rule: increment after an open tag, decrement after a closing tag. Another rule I keep: make sure that the content of a Django block is well-formatted HTML.
As per your example, in my opinion this is very wrong:
<div>
IF FOO
<div someattribute>
ELSE
<div otherattribute>
END FOO
<p>content</p>
</div>
</div>
Why? Because the content of the IF
is not a well-formatted HTML. What should it look like than?
<div>
<div
IF FOO
someattribute
ELSE
otherattribute
END FOO
>
<p>Content</p>
</div>
</div>
(Because your template language does not have nice parsable syntax, we need the new-lines. In Django, I'd write)
<div>
<div {% if foo %}someattribute{% else %}otherattribute{% endif %}>
<p>Content</p>
</div>
</div>
As an added benefit, imagine that the <div>
has 7 attributes, 1 of which is decided upon based a specific condition. Or worse: what if it has 3 attributes each of which is dependent on a condition. That would give 8 different tags.
Keeping in mind the "rule" to keep the content of all blocks well-formatted within that scope, you do not have to worry about breaking the indent of either language.
Another point which I would like to cover (though slightly off-topic): If the opening and closing tags are on different lines, they should be the only tag on that line.
Wrong:
<ul>
<li>This is something <a href="http://programmers.stackexchange.com">Usefull</a>
</i>
</ul>
Instead, prefer
<ul>
<li>This is something <a href="http://programmers.stackexchange.com">Usefull</a></li>
</ul>
or
<ul>
<li>
This is something <a href="http://programmers.stackexchange.com">Usefull</a>
</li>
</ul>
(Warning: advanced usage): Sometimes you have multiple wrapping tags, you may (not must) consider this as one tag:
<ul>
<li><div id="something"><span class="like this">
Something like this
</span></div></li>
</ul>
However, preferably don't mix languages (host/guest) when doing this. And preferably don't do this with for
loops. And don't do this with .
TL;DR for your specific question.
- You have a 'host' (eg. Django templating) and 'guest' language (eg. HTML). Make sure the host language blocks always contain correctly formatted guest language blocks.
- The opening of each block should increment the indentation, the closing of each block should line up with the opening.
And the general note which I should add (as always): use your own judgement. Know when to deviate.