Forums / Developer / Execute template code from with object attribute

"Please Note:
  • At the specific request of Ibexa we are changing this projects name to "Exponential" or "Exponential (CMS)" effective as of August, 11th 2025.
  • This project is not associated with the original eZ Publish software or its original developer, eZ Systems or Ibexa".

Execute template code from with object attribute

Author Message

James Robertson

Wednesday 07 September 2005 8:25:19 pm

I would like to be able to execute a piece of Exponential template code from within an object's attribute content? (ie. Perhaps something a bit like JavaScript eval() function.)

For example: I have Text-block attribute that stored a fragment of XHTML. This fragment is displayed without washing. However, I would like to be able to insert a piece of Exponential template code within the XHTML and have it execute prior to display.

I imagine, within the template, this might look like:
{$node.data_map.xhtml|eval()}

James Robertson

Wednesday 21 September 2005 4:09:56 pm

I guess one solution might be to modify the eZ template 'include' function so that it can be passed template code as a string instead of having to point to a template file.

What URI schemas does 'include' support. I guess if it support HTTP you could pass it the template code this way - instead of from the file-system.

Anyone have any ideas?

James Robertson

Thursday 18 May 2006 3:24:55 pm

Solution supplied by eZ systems [thanks Kristion] for Exponential version 3.6.4. It can be added as a 'template operator' extension [good luck working out how to do that ;-]:

/*
 * WARNING:
 * - This function works directly towards the template system, 
 *     and might not be compatible with new versions of Exponential.
 * - The evaluated code will run in interpreted mode which can behave 
(sligtly) 
 *     different from compiled code, and is slower.
 * - By using fetches and templatecode in content object attributes you can 
 *     make a system which is very hard to maintain!
 *
 * This code should ONLY be used by professionals who understands what they 
are doing
  */
function ezeval( $templateText )
{
     include_once( 'kernel/common/template.php' );
     include_once( 'lib/eztemplate/classes/eztemplate.php' );

     $tpl =& templateInit();
     $root = array( EZ_TEMPLATE_NODE_ROOT, false );
     $resourceData = array( 'resource' => 'design', 'template-filename' =>  
'eval_code' );
     $rootNamespace = '';
     $output = '';
     $tpl->parse( $templateText, $root, $rootNamespace, $resourceData );
     $tpl->process( $root, $output, '', '' );

     return $output;
}