There is one big exception that breaks the law of logic free templates: You can skip a part of the template if a variable is not set. This one and only exception was introduced as this is needed very often and still keeps the templates readable.
Let’s modify our example to get a simple use case:
<html>
<body>
<?username <h1>Hello <%username;optional;text/plain%>!</h1>?>
</body>
</html>What does this mean? If the variable following the question mark is unset, all characters up to the closing question mark tag are skipped. If the variable is set, the characters following the blank after the variable’s name are printed. The variable used inside such a statement automatically gets optional.
So upload the modifications and try again with both URL we’ve use so far. You can see that the welcome message is skipped if the variable username is not set. The conditional templating was above all introduced to enable the skipping of optional attributes. This is a task needed very often as HTML is aware of many optional attributes. If you want for example let the user of your template modify the background color of the page, you can do it like this:
<html>
<body <?color bgcolor=”<%color%>”?>>
<h1>Hello <%username;optional;text/plain%>!</h1>
</body>
</html>The optional attribute bgcolor that can be defined in the body-tag will not be printed if the color variable is not set. Upload the changes and check this behaviour by optionally adding the color variable to the URL:
http://localhost:8080/projector/hello?username=Daniel&color=red
The page will look like this: