views:

145

answers:

2

Hello !

I have a Dwoo template - in example base.html and there is an array $data.

{assign '' 'globalVar'} {* $globalVar empty right now *}


{foreach $data element}
    {include '_partial.html'}
{/foreach}

...

<b>{$globalVar}</b>

And here is _partial.html

<p>
    {$element.someText}
</p>

{assign "$globalVar $element.someVar" 'globalVar'}

What I want to gain ? In every partial file I want to modify some $globalVar that I want to use after {foreach}.

And here is an important thing - I do not want to do the partial's {assign} in base.html ! Only in _partial.html.

Is it possible to pass $globalvar to _partial.html by reference so partial file would modify this variable ?

A: 

I did not find any real solution - just this workaround:

Dwoo allows to access superglobal vars with $dwoo.get.variable or $dwoo.post.variable etc..

We can get data or also set, so in _partial.html I did something like:

{$dwoo.env.globalVar "$globalVar $element.someVar"}

In base.html I can access it now with:

{$dwoo.env.globalVar}
hsz
+1  A: 

I would say you can take two approaches, but there is no really good way to do it from the template itself.

The easiest is to just create a variable before aclling $dwoo->get, and assign it by reference, if you're using a Dwoo_Data object you just do:

$globalVar = '';
$data->assignByRef('globalVar', $globalVar);

If you're not using a Dwoo_Data object, do this instead:

$globalVar = '';
$data['globalVar'] =& $globalVar;

That basically initializes the variable as a reference, so then it will be passed along as a reference to the included templates.

Then in the partial template I would recommend assigning like that because it's easier but that's up to you:

{$globalVar .= $element.someVar}

The other option would be to just put a new stdClass() object in your data, then you can assign stuff to it like

{$globalObj->stuff .= $element.someVar}

And since it's an object, it will be passed by reference automatically, so that solves your problem too.

So yeah, it's doable, but you've to "plan" before you're in the template, because you can't just pass data back to the parent template from an included one.

I hope that helps, Cheers

Seldaek