## Apache Zeta Components Manual :: Docs For Class ezcTemplate

### Template::ezcTemplate

### Class ezcTemplate

The main class for processing templates.

The ezcTemplate class compiles a source template (*.ezt) to PHP code, executes the PHP code, and returns the output. The generated PHP code will be stored on disk as a compiled template.

If a compiled template already exists of the to process template, the ezcTemplate class executes directly the compiled template; thus omitting the compile step.

A simple invocation is simply to create the template object and call the process() method, e.g.

1. $t=new ezcTemplate ();
2. echo$t-> process ("page.ezt");

The location for the source templates and compiled templates among other things are specified in the ezcTemplateConfiguration configuration object. A default configuration is always present and can be accessed via the $configuration property.

Usually one configuration object will be enough, since most of the templates will use the same configuration settings. If for some reason, other configuration settings are needed you can assign an ezcTemplateConfiguration object to the $configuration property.

The following example shows how to change the template and compilation directory by creating a new configuration object.

1. $t=new ezcTemplate ();
2. $t->configuration=new ezcTemplateConfiguration ("design/templates",
3. "/tmp/compilation");
4. echo$t-> process ("page.ezt");

Another approach is to pass the ezcTemplateConfiguration object to the process method. This method will use the given configuration instead.

1. $t=new ezcTemplate ();
2. $config=new ezcTemplateConfiguration ("design/templates",
3. "/tmp/compilation");
4. echo$t-> process ("page.ezt",$config);

The properties ezcTemplate::send and ezcTemplate::receive are available to set the variables that are sent to and retrieved from the template.

The next example demonstrates how a template variable is set and retrieved:

1. $t=new ezcTemplate ();
3. $t->send->mySentence="Hello world";
4. echo$t-> process ("calc_sentence_length.ezt");
6. $number=$t->receive->length;

The template code:

1. {use$mySentence=""}
3. {var$length=str_len($mySentence)}
4. {return$length}

Source for this file: /Template/src/template.php

Version: | //autogen//

### Properties

ezcTemplateAstNode | read | `$astTree` The generated astTree (debug).
string | read | `$compiledTemplatePath` The path of the compiled template.
ezcTemplateConfiguration | read/write | `$configuration` Contains the template configuration.
string | read | `$output` The output of the last processed template.
ezcTemplateVariableCollection | read | `$receive` Contains the variables that are returned by the template.
ezcTemplateVariableCollection | read/write | `$send` Contains the variables that are send to the template.
ezcTemplateTstNode | read | `$tstTree` The generated tstTree (debug).

### Method Summary

public ezcTemplate | `__construct( )` Intializes the ezcTemplate with a default configuration and empty $send and $receive properties.
public string | `generateOptionHash( )` Generates a unique hash from the current options.
public string | `process( $location , [ $config = null] )` Processes the specified template source and returns the output string.

### Methods

#### __construct

ezcTemplate __construct( )

Intializes the ezcTemplate with a default configuration and empty $send and $receive properties.

Configuration of the object must now be done through the properties:

- ezcTemplate::configuration
- ezcTemplate::send
- ezcTemplate::receive

#### generateOptionHash

string generateOptionHash( )

Generates a unique hash from the current options.

For example the default values would return:

1. "updqr0"

Note: This is mostly useful for the template component, relying on the output of this function is not a good idea.

#### process

string process( string $location , [ezcTemplateConfiguration $config = null] )

Processes the specified template source and returns the output string.

Note: The first time a template is accessed it needs to be compiled so the execution time will be higher than subsequent calls.

##### Parameters:

Name | Type | Description
--- | --- | ---
`$location` | string | The path to the template file to process, can be a PHP stream.
`$config` | ezcTemplateConfiguration | Optional configuration object which overrides the default one defined in this object ($configuration).

##### Exceptions:

Type | Description
--- | ---
`ezcTemplateFileNotWriteableException` | If the directory could not be created.
`ezcTemplateParserException` | If the template could not be compiled.

Documentation generated by phpDocumentor 1.4.3