With Block — Dwoo PHP Template Engine

Documentation for the Dwoo with block. Change the variable scope inside a template block to access nested data more easily.

Moves the scope down into an array of values, allowing one to type {$var} instead of {$array.var} within the {with} block

with(array $var)
  • var : the array where to move the scope




Example:

{$arr.foo}
{with $arr} {$foo} / {$arr.foo} {/with}

Data:

'arr' => array( 'foo' => 'bar' )

Output:

bar
bar / 

Note : The tricky thing with this plugin is that as the scopes moves to another location, you can not access the global variables anymore.

As you can see with the above example, {$arr.foo} within the {with} block is not valid and returns null. To fix this you can use the _parent and _root keywords. $_root links to the top-level scope, and $_parent goes up by one level. The magic $dwoo variable is not affected by this and is accessible in the same way no matter the current scope.

Example:

{with $arr.sub}
{$foo} / {$_root.arr.sub.foo} / {$_parent.foo}
{$_root.url} / {$_parent._parent.url}
{$dwoo.version}
{/with}

Data:

'arr' => array( 'sub' => array( 'foo' => 'bar' ) )
'url' => 'example.org'

Output:

bar / bar / bar
example.org / example.org
0.3.3