vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   mainupating a variable in template before using in vb each (https://vborg.vbsupport.ru/showthread.php?t=274871)

CvP 11-30-2011 06:17 PM

mainupating a variable in template before using in vb each
 
Sample code:
HTML 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.

Code:

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.

Code:

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

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


Code:

<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 :/


All times are GMT. The time now is 04:12 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01097 seconds
  • Memory Usage 1,716KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_code_printable
  • (1)bbcode_html_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (3)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete