Share » Learn » eZ Publish » The eZ Template Component

The eZ Template Component

Monday 17 July 2006 2:50:00 am

  • Currently 3 out of 5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

As mentioned above, we are only looking at the parts of the PHP code within the controller that deal with the eZ Template component and the action controller.

The main controller

The template engine is called by two of the controller methods. In the constructor of the controller, we configure the template engine. The method display() loads a template, submits the current action controller to it and displays it.

Here is the interesting part of the constructor:

class tsGrcController
     {
         // ...
         public function __construct()
         {
             // ...
             $eztConfig = ezcTemplateConfiguration::getInstance();
             $eztConfig->templatePath = dirname( __FILE__ ) . "/templates";
             $eztConfig->compilePath = dirname( __FILE__ ) . "/templatesc";
         }

I removed the code in this section that reads the selected action from a GET parameter and instantiates the necessary action controller, which is then stored in the private attribute $action. The code snippet above shows the configuration of the Template component. The call to ezcTemplateConfiguration::getInstance() is a singleton implementation for the template configuration. We tell the configuration object where our templates are stored and where compiled templates will be stored. The eZ Template component compiles its templates into pure PHP code and caches the compiled versions. Templates automatically get recompiled when they change. (Note that the directory where compiled templates are stored must be writable by the user account running the web server.)

public function run()
     {
         $this->action->run();
 
         $template = new ezcTemplate();
         $template->send->action = $this->action;
 
         echo $template->process( $this->action->template . ".ezt" );
     }

This is the run() method of the main controller. It simply calls the run method of the action controller in the first line. (We will talk about this in greater detail below.) After that, a new template is instantiated. The third line of code sends the action controller to the template. Sending a variable to a template is necessary to make it available to the template code. You can send as many variables as you like to a template by simply assigning them to a child of the template's $send attribute. The name you choose for the child is the name under which the variable will be available in the template. The last line makes the template engine process the template (which is selected by the action controller) and prints the results. If the selected template is already compiled, the Template component runs the compiled version. If no compiled version is available or if the template has changed since the last compile, it is compiled and stored.

The action controller

Now we will look at the action controller we mentioned above:

class ActionSearchdetails
     {
         private $searchId;
         public $template = "searchdetails";
         public $title = "Search details";
         public $site;
         public $search;
         public $results;
         public function __construct()
         {
             // ...
         }
         public function run()
         {
             $this->search = tsGrcController::getSession()->load( 'Search', $this->searchId );
             $this->results = $this->search->getResults();
             $this->site = $this->search->getSite();
         }
 
     }

The constructor, which I did not include here, processes the parameters needed by this action, specifically the ID of the search we want to display. The retrieved search ID is stored in the private member $searchId. In the first line of the run() method, you see how the Search object is loaded from the database and stored in the public member called $search. After that, we call several methods on this object to retrieve the related results and the site this search belongs to. Both are also stored in public attributes.

36 542 Users on board!

Tutorial menu

Printable

Printer Friendly version of the full article on one page with plain styles

Author(s)