Wednesday 24 January 2007 5:05:00 pm
site.ini.append.php [TemplateSettings] TemplateCache=enabled TemplateCompile=enabled TemplateOptimization=enabled
The template language is parsed and executed by eZ Publish. Since this task can be quite time-consuming, there is a feature in eZ Publish to convert templates to pure PHP code. This makes execution quite a lot faster. The TemplateCache setting enables cache-blocks (as explained on the next page) and template compilation to be used. TemplateCompile controls whether template compilation should be done and TemplateOptimization attempts to optimize the PHP in these files.
For better performance, you could replace complex pieces of template code with custom operators that do the same thing in PHP code. Here is an example of a complex piece of template code that will return some redundant data:
{def $article_list=fetch( content, tree, hash( parent_node_id, $node.node_id ) ) $main_node_ids_array=array() $unique_article_list_array=array()} {foreach $article_list as $article} {if $main_node_ids_array|contains( $article.object.main_node_id )|not} {set main_node_ids_array=$main_node_ids_array|append( $article.object.main_node_id )} {set unique_article_list_array=$unique_article_list_array|append( $article )} {/if} {/foreach} {foreach $unique_article_list_array as $unique_article} {node_view_gui view=line content_node=$unique_article.object.main_node} {/foreach}
Assume now that we need to display a drop-down list with the names of a hundred objects stored under a folder somewhere in the content structure. Instead of fetching all objects, we can use a custom template operator to return the SQL query result as an array with object names. Here is an example SQL query for use in a template operator:
SELECT ezcontentobject.name, ezcontentobject.id, ezcontentobject_tree.contentobject_id FROM ezcontentobject_tree, ezcontentobject, ezcontentobject_name WHERE path_string LIKE '/1/124/%' AND depth 2 AND node_id !=124 AND ezcontentobject_tree.contentobject_id = ezcontentobject.id AND ezcontentobject_tree.contentobject_id = ezcontentobject_name.contentobject_id AND ezcontentobject_tree.contentobject_version = ezcontentobject_name.content_version AND ezcontentobject_name.content_translation = 'eng-GB' ORDER BY ezcontentobject.published DESC
This returns all object names and their ids where the parent node id is 124. Then, in the template we can use the following code:
{foreach objectsqlnames() as $object} {$object.name} {/foreach}
With 100 objects, the code above can be three times faster than code with the regular fetch function. It requires two fewer SQL queries and returns only the data that is needed.
If you need to display some information about node children or grandchildren in the node template, this is possible with $node.children and $node.children.0.children. Instead of grabbing content with the fetch function like this:
{def $children=fetch( 'content', 'list', hash( 'parent_node_id', $node.node_id ) )} {foreach $children as $child} {$child.url_alias|ezurl} {$child.name} {$child.node_id} {/foreach}
...display child data as follows:
{foreach $node.children as $child} {$child.url_alias|ezurl} {$child.name} {$child.node_id} {/foreach}
This requires two fewer SQL queries and thus reduces the page load time.
When fetching objects you should always use limit and offset in order to control the records that are returned. Some queries return a lot of redundant records if you do not specify the number of results to return (limit) and the record where the return count (offset) should start. Here is an example fetch statement that uses both limit and offset.
{fetch( 'content', 'list', hash( 'parent_node_id', 2, 'limit', 15, 'offset', 10, 'only_translated', true(), 'language', 'ger-DE' ) )}