5. Nested conditions

It is possible to use even nested conditions. This can be a very powerful feature but should be only used as a last resort as it makes the template more complicated. But to be complete we want to show this possibility in our last example:

<html>
   <body>
      <?username <h1 <?style style="<%style%>"?>>Hello <%username%>!</h1>?>
   </body>
</html>

If you followed the previous examples carefully you can call yourself an expert of Projector templating and might be able to understand this example without explanations as it is just a combination of the previous ones: If the variable username is not set, the whole headline is skipped and the page will show up empty.

Check this by entering the URL without this parameter as you’ve done in the “Hello world” example. If you open the page source of the rendered page, you’ll see that the body tag is empty:

<html>
   <body>
  
   </body>
</html>

Now add the username as parameter as we’ve done in the first example or this chapter. This is the page source of the generated page:

<html>
   <body>
      <h1 >Hello Tony!</h1>
   </body>
</html>

You can see that the headline is printed but the style attribute is not yet included. We can apply a style to the headline by encoding the style parameter to the URL:

http://localhost:8080/projector/hello?style=color:red&username=Tony

The page that will show up looks like this:

Figure 6.3. Dynamic style

Dynamic style

This is the associated page source:

<html>
   <body>
      <h1 style="color:red;">Hello Tony!</h1>
   </body>
</html>

You can play around with this example and add some other style attributes to the headline.