PDA

View Full Version : mainupating a variable in template before using in vb each


CvP
11-30-2011, 06:17 PM
Sample code:
<vb:if condition="$first_item = X($myvar)">
<!-- do something -->
</vb:if>
<vb:each from="myvar" key="k" value="v">
<!-- do something -->
</vb:each>


I tried array_shift(), each() methods in place of X. Doing so causes this error.

Warning: Invalid argument supplied for foreach() in [path]/includes/functions.php on line 3555

Anyone have any idea how to workaround this?

PS: I'm aware of the counter workaround in vb:each but that doesn't go well with how I wanna write the template.
The only workaround I can now think of array_shift the value in the actual php code (rather than in template) before registering the variable to the template which is something I wanna avoid.


btw, you'll need to add this plug in to use these functions:
hook: template_safe_functions
code: $safe_functions[] = 'each';

kh99
11-30-2011, 10:06 PM
I tried it using array_shift and it seems to work.


$myvar = array('First', 'Second', 'Third');

$templater = vB_Template::create('test_template');
$templater->register('myvar', $myvar);
print_output($templater->render());



<vb:if condition="$first_item = array_shift($myvar)">
First: {vb:raw first_item}<BR/>
</vb:if>
<vb:each from="myvar" key="k" value="v">
Loop: {vb:raw k} {vb:raw v}<BR/>
</vb:each>


although I think you lose the original array keys when doing it that way.

I couldn't see how you could get to fetch_error_array() with $errors not an array unless maybe you have a plugin that changes $errors (maybe using hook template_compile?) or maybe a custom tag.

CvP
12-02-2011, 09:54 AM
I think it has to do with my array being multi-level :/