Changing the content of a master template's slot
This example demonstrates how to change the client template that is used to fill dynamic data into a master template.
You either can download the complete application and have a look at the code or you can create the project step by step as described below.
The example is based on the "Hello World" example and extends its code. So it's not necessary for you to do all the basic configuration steps such
as deleting the predefined templates and the welcome controller which is the default controller in the Simple Framework download package.
The download contains the complete project as zip archive. Just unpack it into the web application root directory of your webserver.
changeClientTemplate.zip 90.98 KB
"Changing the content of a master template's slot" step by step
Step 1: Setup the project
Since this project extends the "Hello World" example we need to copy the Hello World project first in order to get a second one we can work with.
You only need to do this step, if you want to keep both projects, the "Hello World" example as well as this "Changing the content... " one.
If you don't care about the Hello World project you directly can work with it. In that case just rename the root directory of the "Hello World" example from helloWorld to changeClientTemplate and go on with step 2 of this exercise.
Otherwise make a copy of the "Hello World" example and name it changeClientTemplate.
Step 2: Preparing the master template
Open the master template file ressources/templates/mastertemplates/masterTemplate.phtml. The file should look like this in the moment.
We will augment the template with some static content and rename the placeolder slot. But first we should give the <body> tag a new color. This is useful to see the static and dynamic areas of the template. Replace the color definition white with gray.
Now let's add some static content such as a headline and some text and rename the placeholder slot from slott_hello_world to slott_switch.
The manipulated master template should look like this.
Step 3: Preparing the client templates
To switch between different content for the placeholder we need different client template files. In this example we will use three ones. One default file that is used if no other template was requested and two switchable templates. Create the files ressources/templates/clientTemplates/default.phtml, ressources/templates/clientTemplates/ct_1.phtml and ressources/templates/clientTemplates/ct_2.phtml now with the following content.
Step 4: Configuration
Remember that the mapping section in the AppControllerSettings.xml file still is configured for the "Hello World" example. The next image shows how the file should look like in the moment.
Edit this section as shown in the next image.
Also change the value for default_controller from helloWorld to switchClientTemplate.
Step 5: Creating a new controller
In this step we will create the controller that we have configured as the default controller in the last step.
Before we do this, we should delete the old controller from the "Hello World" example. To do so, delete the file code/controllerImpl/helloWorld.php
Now create the file code/controllerImpl/switchClientTemplate.php with the following content.
That's all. Now close your browse in order to delete the current session. Before you reopen the browser delete any files under ressources/tmp.
Now open the browser and call the application with a url similar to this http://localhost/changeClientTemplate. You should see something like this
Our controller checks for a GET-parameter named switch with one of the values 1 or 2.
If you call the url http://localhost/changeClientTemplate?switch=1 we bind the client template ct_1.phtml to the slot. Calling http://localhost/changeClientTemplate?switch=2 binds the client template ct_2.phtml. Any other call will result in using the default template again.
We learned in this example that it doesn't take more than a piece of code like this
to toggle the layout binding for a client template.