How to make eZ Publish use override conditions when loading a template

I answered this question today on IRC and a colleague asked me the same thing about two weeks ago… it's time to write down the solution :-)

Basically, you just need to tell what design keys you want to use and their value to the template engine of eZ Publish. The design keys are the parameters you will be able to use in an override condition. Let's take an example with a simplistic PHP view (it lacks a lots of checkings):

require_once 'kernel/common/template.php';
$NodeID = intval( $Params['NodeID'] );
$node = eZContentObjectTreeNode::fetch( $NodeID );
$tpl = templateInit();
$tpl->setVariable( 'node', $node );

// setting up the context to use override conditions
$res = eZTemplateDesignResource::instance();
$designKeys = array( array( 'class_identifier', $node->attribute( 'class_identifier' )),
                     array( 'parent_node_id', $node->attribute( 'parent_node_id' )) );
$res->setKeys( $designKeys );

$tpl->fetch( 'design:mymodule/myview.tpl' );

In this code, I define two design keys: class_identifier and parent_node_id , so I can write override rules that match on the class identifier or on the parent node id of the node or on both, for example:


With this override condition, eZ Publish will use the template located in override/templates/myview/folder.tpl in the design when the node is a folder, otherwise it will use the default one (templates/mymodue/myview.tpl ).