Fetching parsed template output through PHP

Wednesday 25 May 2011 5:21:06 am

By : Sebastiaan van der Vliet

I needed a PHP script to export the template output from a module ($module_result.content) to a file stored in the storage folder. Of course this could be achieved by curl or  get_file_output, but I wanted a script to generate the template output rather than fetching it.

Following a discussion "Get a template output from a module", started by Roberto Sacchi (http://bit.ly/jmmDKW), I started the script by attempting to use the following code to output the template contents:

$NodeID = $nodeItem->attribute( 'node_id' );
$Module = false;
$tpl = eZTemplate::factory();
$LanguageCode = "dut-NL";
$ViewMode = "full";
$ini = eZINI::instance();
$viewParameters = array();
$collectionAttributes = false;
$validation = array( 'processed' => false,'attributes' => array() );
$localVars = array( "cacheFileArray", "NodeID", "Module", "tpl", "LanguageCode", "ViewMode", "Offset", "ini","cacheFileArray", "viewParameters", "collectionAttributes","validation" );
$args = compact( $localVars );
$data = eZNodeviewfunctions::contentViewGenerate( false, $args );
$content = $data['content'];

However, there seemed to be too much overhead. After searching the kernel files I came up with the following solution to fetch the output:

$ViewMode = "full";
$tpl = eZTemplate::factory();
$moduleName = "Content";
$Module = eZModule::findModule( $moduleName );

$NodeID = $NodeItem->attribute( 'node_id' );
$Node = $Module->run( 'view', array( $ViewMode, $NodeID ), array( 'ViewCache' => false,
 'AttributeValidation' => array( 'processed' => true, 'attributes' => false ), 'CollectionAttributes' => false ) );

$moduleResult=array();
$moduleResult['content'] = $Node['content'];
$tpl->setVariable( "module_result", $moduleResult );
$templateResult = $tpl->fetch( "design:pagelayout.tpl" );
$templateResult = ezpEvent::getInstance()->filter('response/output', $templateResult );
eZFile::create( $filePath, false, $templateResult );

The variable $Node['content'] holds the entire parsed template ($module_result.content). For example, in this case it holds the parsed template code for the view /content/view/full/[node_id]. Moreover, you will be able to retrieve a list of all used templates through $Node['template_list'].

However, I also needed the pagelayout: the last five lines take care of that. Instead of design:pagelayout.tpl you can also specify any pagelayout available. The resulting template code is then stored in a file.

Powered by eZ Publish™ CMS Open Source Web Content Management. Copyright © 1999-2014 eZ Systems AS (except where otherwise noted). All rights reserved.