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