Forums / Developer / Variables passed to and from *_view_gui

Variables passed to and from *_view_gui

Author Message

Piotrek Karaś

Tuesday 29 September 2009 8:01:36 pm

Hi there,

Is there any built-in method of passing variables <b>to and from</b> template parts that are rendered using *_view_gui -like functions? I did try all reasonable combinations of global and root scopes, but nothing seemed to work.

This is especially required for node templates,such as the eZ Flow zone template. I would like to be able to initialize a variable before the zones and their blocks get rendered, then append to this variable in each zone/block/block item, and then use that variable at the end of the zone template and pass it to persistent variables.

Now, I do have a solution for this, which I can share later, but it's a trick/hack of sorts, even though it does not affect the installation in any way. I was wondering if there was a better way of achieving that.

Thanks,
Piotrek

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Gaetano Giunta

Wednesday 30 September 2009 1:04:18 am

The usual way to pass vars down the chain is to add them in *view_gui:
{*view_gui object=$myobject newvar=$newvar}
and in the node template, add newvar=$newvar to your {include} calls.
A bit tedious, as it involves fixing lots of templates, but it works.

To get stuff back, your best option is, as you said, the persistent var, as it it pretty much the only one that will survive the view cache.

To pass vars down the templates which are included from the pagelayout, the simplest way is to actually declare them in the pagelayout itself. Those vars will be available everywhere without being declared in {include} calls, and you will be able to assigned values to them in any template and have it propagated afterward. Not too sure how they fare within node views...

Principal Consultant International Business
Member of the Community Project Board

Piotrek Karaś

Wednesday 30 September 2009 1:18:45 am

Hello Gaetano,

Yes, passing vars down the chain is not an issue.

Now, passing vars up the chain does not necessarily have to do anything in common with persistent variables. I mentioned those as that's part of my particular solution, but let's skip communication between node template and pagelayout template for a while. Let's concentrate on a node template only, here's an example:

{def $my_var=array()}
{attribute_view_gui attribute=$attribute in_attribute_var=$my_var}
{$my_var|attribute(show,1)}

and then in the attribute template included above:

...
{set $in_attribute_var=$in_attribute_var|append( 'new_element' )}
...

What does the inspection show you? Because it doesn't work down and up for me.

Your last suggestion would be fine for pagelayout, it doesn't seem to work in node templates either. Maybe I did something wrong, but I naturally use global variables in the pagelayout included templates and those work fine. And I think it shouldn't even work since the module result templates are rendered before the pagelayout is (which is the fundamental thing for persistent vars to work...).

Here's my solution, in form of three operators that do the trick about passing vars up and down in node templates - a bit primitive, but... ;)

case 'selfutils_php_globalvar_set':
    {
        $name = $namedParameters['name'];
        $value = $namedParameters['value'];

        global $selfUtilsPhpGlobalVar;
        if ( !isset( $selfUtilsPhpGlobalVar ) )
        {
            $selfUtilsPhpGlobalVar = array();
        }

        $selfUtilsPhpGlobalVar[$name] = $value;
    }
    break;
case 'selfutils_php_globalvar_get':
    {
        global $selfUtilsPhpGlobalVar;
        
        $name = $namedParameters['name'];
        
        $result = null;
        if ( isset( $selfUtilsPhpGlobalVar[$name] ) )
        {
            $result = $selfUtilsPhpGlobalVar[$name];
        }
        $operatorValue = $result;
    }
    break;
case 'selfutils_php_globalvar_append':
    {
        global $selfUtilsPhpGlobalVar;

        $name = $namedParameters['name'];
        $key = $namedParameters['key'];
        $value = $namedParameters['value'];

        if ( isset( $selfUtilsPhpGlobalVar[$name] ) )
        {
            if ( is_array( $selfUtilsPhpGlobalVar[$name] ) )
            {
                if ( $key === null )
                {
                    $selfUtilsPhpGlobalVar[$name][] = $value;
                }
                else
                {
                    $selfUtilsPhpGlobalVar[$name][$key] = $value;
                }
            }
        }
    }
    break;

Cheers,
Piotrek

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

Gaetano Giunta

Wednesday 30 September 2009 5:21:26 am

Now I see your point better.

The way for making variables global by declaring them in the pagelayout works also for the templates included by a node view by usage of include:

{def $x = 'x'}
before --- {$x} ---
{include uri='design:test.tpl'}
after --- {$x} ---

plus

Inclusion begin
before --- {$x} ---
{set $x = 'y'}
after --- {$x} ---
Inclusion end

It does not work otoh for *_view_gui template functions

Principal Consultant International Business
Member of the Community Project Board

Piotrek Karaś

Wednesday 30 September 2009 10:43:20 am

Well, then if anyone's interested I may polish, test and pack my solution into an extension on day ;) However, I think this should one day become a feature of eZ Publish because it may really be useful in a series of scenarios. I will summit this as an enhancement request unless someone comes up with a better solution in few days.

Thanks for talking it over,
Cheers,
Piotrek.

--
Company: mediaSELF Sp. z o.o., http://www.mediaself.pl
eZ references: http://ez.no/partners/worldwide_partners/mediaself
eZ certified developer: http://ez.no/certification/verify/272585
eZ blog: http://ez.ryba.eu

eZ debug

Timing: Jan 18 2025 16:12:33
Script start
Timing: Jan 18 2025 16:12:33
Module start 'content'
Timing: Jan 18 2025 16:12:34
Module end 'content'
Timing: Jan 18 2025 16:12:34
Script end

Main resources:

Total runtime1.2590 sec
Peak memory usage4,096.0000 KB
Database Queries200

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0081 588.9453180.8281
Module start 'content' 0.00811.0880 769.7734590.9531
Module end 'content' 1.09610.1629 1,360.7266342.0234
Script end 1.2590  1,702.7500 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.00520.4160210.0002
Check MTime0.00200.1599210.0001
Mysql Total
Database connection0.00100.078210.0010
Mysqli_queries1.173593.20682000.0059
Looping result0.00210.16861980.0000
Template Total1.226197.420.6131
Template load0.00240.194420.0012
Template processing1.223797.191220.6118
Template load and register function0.00010.007410.0001
states
state_id_array0.00080.064710.0008
state_identifier_array0.00140.114820.0007
Override
Cache load0.00220.1750460.0000
Sytem overhead
Fetch class attribute can translate value0.00170.134630.0006
Fetch class attribute name0.00140.111070.0002
XML
Image XML parsing0.00120.095930.0004
class_abstraction
Instantiating content class attribute0.00000.0018100.0000
General
dbfile0.00290.2313340.0001
String conversion0.00000.000530.0000
Note: percentages do not add up to 100% because some accumulators overlap

CSS/JS files loaded with "ezjscPacker" during request:

CacheTypePacklevelSourceFiles
CSS0extension/community/design/community/stylesheets/ext/jquery.autocomplete.css
extension/community_design/design/suncana/stylesheets/scrollbars.css
extension/community_design/design/suncana/stylesheets/tabs.css
extension/community_design/design/suncana/stylesheets/roadmap.css
extension/community_design/design/suncana/stylesheets/content.css
extension/community_design/design/suncana/stylesheets/star-rating.css
extension/community_design/design/suncana/stylesheets/syntax_and_custom_tags.css
extension/community_design/design/suncana/stylesheets/buttons.css
extension/community_design/design/suncana/stylesheets/tweetbox.css
extension/community_design/design/suncana/stylesheets/jquery.fancybox-1.3.4.css
extension/bcsmoothgallery/design/standard/stylesheets/magnific-popup.css
extension/sevenx/design/simple/stylesheets/star_rating.css
extension/sevenx/design/simple/stylesheets/libs/fontawesome/css/all.min.css
extension/sevenx/design/simple/stylesheets/main.v02.css
extension/sevenx/design/simple/stylesheets/main.v02.res.css
JS0extension/ezjscore/design/standard/lib/yui/3.17.2/build/yui/yui-min.js
extension/ezjscore/design/standard/javascript/jquery-3.7.0.min.js
extension/community_design/design/suncana/javascript/jquery.ui.core.min.js
extension/community_design/design/suncana/javascript/jquery.ui.widget.min.js
extension/community_design/design/suncana/javascript/jquery.easing.1.3.js
extension/community_design/design/suncana/javascript/jquery.ui.tabs.js
extension/community_design/design/suncana/javascript/jquery.hoverIntent.min.js
extension/community_design/design/suncana/javascript/jquery.popmenu.js
extension/community_design/design/suncana/javascript/jScrollPane.js
extension/community_design/design/suncana/javascript/jquery.mousewheel.js
extension/community_design/design/suncana/javascript/jquery.cycle.all.js
extension/sevenx/design/simple/javascript/jquery.scrollTo.js
extension/community_design/design/suncana/javascript/jquery.cookie.js
extension/community_design/design/suncana/javascript/ezstarrating_jquery.js
extension/community_design/design/suncana/javascript/jquery.initboxes.js
extension/community_design/design/suncana/javascript/app.js
extension/community_design/design/suncana/javascript/twitterwidget.js
extension/community_design/design/suncana/javascript/community.js
extension/community_design/design/suncana/javascript/roadmap.js
extension/community_design/design/suncana/javascript/ez.js
extension/community_design/design/suncana/javascript/ezshareevents.js
extension/sevenx/design/simple/javascript/main.js

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
5content/datatype/view/ezimage.tpl<No override>extension/sevenx/design/simple/templates/content/datatype/view/ezimage.tplEdit templateOverride template
5content/datatype/view/ezxmltext.tpl<No override>extension/community_design/design/suncana/templates/content/datatype/view/ezxmltext.tplEdit templateOverride template
12content/datatype/view/ezxmltags/paragraph.tpl<No override>extension/ezwebin/design/ezwebin/templates/content/datatype/view/ezxmltags/paragraph.tplEdit templateOverride template
4content/datatype/view/ezxmltags/line.tpl<No override>design/standard/templates/content/datatype/view/ezxmltags/line.tplEdit templateOverride template
5content/datatype/view/ezxmltags/literal.tpl<No override>extension/community/design/standard/templates/content/datatype/view/ezxmltags/literal.tplEdit templateOverride template
1pagelayout.tpl<No override>extension/sevenx/design/simple/templates/pagelayout.tplEdit templateOverride template
 Number of times templates used: 33
 Number of unique templates used: 7

Time used to render debug report: 0.0002 secs