1. Dynamic content

We have already used a Projector template in our “Hello world” example. Now we want to improve this page by giving the user a warm welcome. We want to welcome the user with his given name so we replace “world” with a tag that indicates that this part should be filled with dynamic content, in our case with the name of the current user.

<html>
   <body>
      <h1>Hello <%username%>!</h1>
  </body>
</html>

As you can see, you don’t tell Projector at this stage how to retrieve the username. It is just a name of a variable that will be bound later. Why? The reusability of the template will be increased dramatically if you don’t specify how to retrieve the dynamic content. If you are using the template only once this might look a bit uncomfortable and overdosed, but as your templates might be much more complex in real life, you will be happy if you can reuse them across different projects.

Thanks to this approach Projector can offer some generic templates that can be used out of the box to generate tables, forms or menus.

After uploading the template we try to reload the page in our web browser. But instead of seeing our lovely welcome page, Projector tells us that a variable is missing! This is very true as we just defined a variable by entering the Projector tag that defines dynamic content.

At this stage we can support our template processor with the required input by adding this information to our URL:

http://localhost:8080/projector/hello?username=Tony

Now that we provide the required input our page looks like this:

Figure 6.1. Dynamic content

Dynamic content