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

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

Main resources:

Total runtime0.0164 sec
Peak memory usage2,048.0000 KB
Database Queries3

Timing points:

CheckpointStart (sec)Duration (sec)Memory at start (KB)Memory used (KB)
Script start 0.00000.0065 589.1484152.6406
Module start 'layout' 0.00650.0028 741.789139.4609
Module start 'content' 0.00940.0048 781.2500101.4453
Module end 'content' 0.01420.0022 882.695342.3047
Script end 0.0163  925.0000 

Time accumulators:

 Accumulator Duration (sec) Duration (%) Count Average (sec)
Ini load
Load cache0.002716.1804140.0002
Check MTime0.00127.3866140.0001
Mysql Total
Database connection0.00127.043210.0012
Mysqli_queries0.002515.282530.0008
Looping result0.00000.090210.0000
Template Total0.001710.410.0017
Template load0.00095.480310.0009
Template processing0.00084.882210.0008
Override
Cache load0.00063.710810.0006
General
dbfile0.00031.578980.0000
String conversion0.00000.050940.0000
Note: percentages do not add up to 100% because some accumulators overlap

Templates used to render the page:

UsageRequested templateTemplateTemplate loadedEditOverride
1print_pagelayout.tpl<No override>extension/community/design/community/templates/print_pagelayout.tplEdit templateOverride template
 Number of times templates used: 1
 Number of unique templates used: 1

Time used to render debug report: 0.0001 secs