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 eZ publish 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 eZ publish 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 eZ publish 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 eZ publish.
 * - 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;
}

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

eZ debug

Timing: Jan 19 2025 04:28:25
Script start
Timing: Jan 19 2025 04:28:25
Module start 'layout'
Timing: Jan 19 2025 04:28:25
Module start 'content'
Timing: Jan 19 2025 04:28:26
Module end 'content'
Timing: Jan 19 2025 04:28:26
Script end

Main resources:

Total runtime0.9418 sec
Peak memory usage4,096.0000 KB
Database Queries55

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0060 589.2891152.6406
Module start 'layout' 0.00600.0036 741.929739.4766
Module start 'content' 0.00960.9307 781.4063472.9063
Module end 'content' 0.94040.0014 1,254.312512.1250
Script end 0.9418  1,266.4375 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00310.3277160.0002
Check MTime0.00130.1353160.0001
Mysql Total
Database connection0.00100.107910.0010
Mysqli_queries0.885594.0152550.0161
Looping result0.00050.0524530.0000
Template Total0.910496.720.4552
Template load0.00800.847420.0040
Template processing0.902495.812820.4512
Template load and register function0.00020.016210.0002
states
state_id_array0.00110.119910.0011
state_identifier_array0.00100.104420.0005
Override
Cache load0.00750.7973170.0004
Sytem overhead
Fetch class attribute can translate value0.00050.057410.0005
Fetch class attribute name0.00120.126640.0003
XML
Image XML parsing0.00200.215510.0020
class_abstraction
Instantiating content class attribute0.00000.001860.0000
General
dbfile0.00220.2379160.0001
String conversion0.00000.001040.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1node/view/full.tplfull/forum_topic.tplextension/sevenx/design/simple/override/templates/full/forum_topic.tplEdit templateOverride template
3content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
3content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
4content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
1content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
1content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 14
 Number of unique templates used: 7

Time used to render debug report: 0.0001 secs