Get a template output from a module

Author Message

Roberto Sacchi

Wednesday 04 February 2009 9:15:54 am

I'm writing a module....

I get an ID node.

Now I would like to execute a node, that has an associated template, and get the html of the loaded template.

I find a way to get it works:

$getdatafetch = eZHTTPTool::getDataByURL("http://localhost/ezpublish/index.php/content/view/full/ID_NODE",false,false);

 

but I think It's not a good way to resolve it.
I would like to get this template using ezpublish functions, like eZOperationHandler::execute, fetch or others...

Thanks

Rob

Ousmane KANTE

Saturday 07 February 2009 4:30:42 am

Hi,
Something like fetching the template would work:

$tpl = templateInit();

$node = eZcontentObjectTreeNode::fetch($your_node_id);
if($node)
$tpl->setvariable('node', $node);
$getdatafetch = $tpl->fetch('design:node/view/full.tpl');

Normally it will return the full view of your node (or it's override)

Consultant Ez Publish- Sapiens Consulting

Roberto Sacchi

Wednesday 11 February 2009 7:31:34 am

It works, but the problem is that I lost all css and image links in the output file.
The pagelayout.tpl is not included in the fetching (when I load node from ezpub, it's included)

How to do resolve it?

Roberto Sacchi

Wednesday 11 February 2009 7:58:58 am

I forget to ask you where have you found information about your code:

$tpl->setvariable('node', $node);

In Ez Docs and Classes I only have found:

eZTemplate::setVariable ( $ var,
$ val,
$ namespace = ""
)

that it's very generic.

When do you read that you can pass a node as variable?
Probably I miss some important documentation.....

Ousmane KANTE

Thursday 12 February 2009 7:11:03 am

You can use this "syntaxe" to add variable you want to use in your template.

as a node/view/[your_view].tpl, requires <i>$node</i> variable, you should be sur to have set it as tpl variable ...
if your template requires other variable(s), you can set it(them) as above...
For exemple, your css if you want to include them in node_view.

according to function definition and you can look at sample code in extensions such as ezdhtml (Online Editor).

Consultant Ez Publish- Sapiens Consulting

André R.

Thursday 12 February 2009 7:57:58 am

Or you can take a look in kernel\content\view.php, and see that it is a bit more complex then this.
But the code is reusable so a call like this will be enough to generate the whole $Result variable, and using the override system as well (the code above does not):

    $localVars = array( "cacheFileArray", "NodeID",   "Module", "tpl",
                        "LanguageCode",   "ViewMode", "Offset", "ini",
                        "cacheFileArray", "viewParameters",  "collectionAttributes",
                        "validation" );

    $cacheFileArray = array( 'cache_dir' => false, 'cache_path' => false );
    $args = compact( $localVars );
    $data = eZNodeviewfunctions::contentViewGenerate( false, $args ); // the false parameter will disable generation of the 'binarydata' entry
    return $data['content']; // Return the $Result array

You need to set all the variables in localVars though (see view.php for more info).

eZ Online Editor 5: http://projects.ez.no/ezoe || eZJSCore (Ajax): http://projects.ez.no/ezjscore || eZ Publish EE http://ez.no/eZPublish/eZ-Publish-Enterprise-Subscription
@: http://twitter.com/andrerom

Sebastiaan van der Vliet

Tuesday 24 May 2011 10:34:15 am

This code below also seems to work:

$moduleName = "Content";
  $Module = eZModule::findModule( $moduleName );
  $NodeID = [some node id];
  $ViewMode = "full";
  $Result = $Module->run( 'view', array( $ViewMode, $NodeID ),
                             array( 'ViewCache' => false, 'AttributeValidation' => array( 'processed' => true,
                                      'attributes' => false ),'CollectionAttributes' => false ) );
  print_r($Result);

Of course this only displays the contents of the full template. Does anybody know how to add the pagelayout.tpl code?

Certified eZ publish developer with over 9 years of eZ publish experience. Available for challenging eZ publish projects as a technical consultant, project manager, trouble shooter or strategic advisor.

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 18 2025 10:16:18
Script start
Timing: Jan 18 2025 10:16:18
Module start 'layout'
Timing: Jan 18 2025 10:16:18
Module start 'content'
Timing: Jan 18 2025 10:16:19
Module end 'content'
Timing: Jan 18 2025 10:16:19
Script end

Main resources:

Total runtime0.9371 sec
Peak memory usage4,096.0000 KB
Database Queries73

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0049 587.9375152.6250
Module start 'layout' 0.00490.0029 740.562539.4453
Module start 'content' 0.00780.9271 780.0078672.4609
Module end 'content' 0.93500.0020 1,452.468816.1563
Script end 0.9370  1,468.6250 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00380.4018160.0002
Check MTime0.00130.1411160.0001
Mysql Total
Database connection0.00070.077310.0007
Mysqli_queries0.861091.8757730.0118
Looping result0.00090.0986710.0000
Template Total0.901996.220.4510
Template load0.00230.247220.0012
Template processing0.899696.000520.4498
Template load and register function0.00020.018710.0002
states
state_id_array0.00100.103310.0010
state_identifier_array0.00130.134420.0006
Override
Cache load0.00210.2207500.0000
Sytem overhead
Fetch class attribute can translate value0.00080.081140.0002
Fetch class attribute name0.00160.174290.0002
XML
Image XML parsing0.00430.464140.0011
class_abstraction
Instantiating content class attribute0.00000.002390.0000
General
dbfile0.00490.5278240.0002
String conversion0.00000.000640.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
7content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
16content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
5content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
7content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
2content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 39
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs