vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vBulletin 4 Articles (https://vborg.vbsupport.ru/forumdisplay.php?f=242)
-   -   [HOW TO - vB4] Rendering templates and registering variables - a short guide (https://vborg.vbsupport.ru/showthread.php?t=228078)

Seductor 05-15-2012 09:36 AM

Oh, and is there any way to show all my array cells If I don't know which size is the array?

Edited: I suspect that it is done in this way.
Code:

<vb:each from="my_array" key="key1" value="my_result">
    {vb:var my_result}
</vb:each>

I am doing something wrong. I have this:
resource(46) of type (mysql result)

And each row is:
Code:

array(21) { ["id"]=> string(2) "18" ["name"]=> string(5) "ABRIR" ["description"]=> string(23) "Iniciar la interacci?n." ["userid"]=> string(4) "2858" ["username"]=> string(20) "Seducci?n Cient?fica" ["dateline"]=> string(10) "1311875182" ["lastupdate"]=> string(10) "1311933838" ["categoryid"]=> string(1) "1" ["status"]=> string(1) "1" ["ipaddress"]=> string(13) "81.202.205.41" ["attach"]=> string(1) "0" ["threadid"]=> string(1) "0" ["lastupdater"]=> string(20) "Seducci?n Cient?fica" ["lastupdaterid"]=> string(4) "2858" ["tags"]=> string(16) "abrir,escalada 1" ["popup"]=> string(23) "Iniciar la interacci?n." ["views"]=> string(1) "0" ["votenum"]=> string(1) "0" ["votetotal"]=> string(1) "0" ["linkurl"]=> string(7) "http://" ["banner"]=> string(7) "http://" }
I'm doing this in my template:
Code:

<vb:each from="my_query" value="sentence">
  Test {vb:raw sentence.name}
</vb:each>

Notice this: Test {vb:raw sentence.name}

But It doesn't show nothing, as if the each is not being executed. I expected to show, at least, 46 Test words.

kh99 05-15-2012 12:52 PM

Quote:

Originally Posted by Seductor (Post 2329515)
I am doing something wrong. I have this:
resource(46) of type (mysql result)


You are registering the return from one of the query functions. You need to call fetch_array() to get an array for each row, like:

Code:

$results = $db->query_read("some sql");
while ($row = $db->fetch_array($results))
{
    // do something with $row
}


Of course each row is an array (even if you only selected one column). If you are selecting one column from a number of matching rows, you won't get an array with all the matching values, you still get an array for each row. If you want an array with all the matching values you'd have to build that yourself.

Note also that you don't *have* to register an array to your template and use vb:each - you could format your own html string in the while loop above, then just register the string.

tbworld 05-26-2012 07:20 PM

[QUOTE=Boofo;2287132]Try this in the parse_templates hook:

Boofo, I was working on specialized routine and trying to use some of the vb-ojects to solve my problem when I came across the code you posted here. This redirected me to the right vb routines. Thanks so much for sharing!

Dave-ahfb 05-26-2012 08:11 PM

I have a problem on a page. I can use the variable {vb:raw companyname} in the places of the template that I need to. However when I want to place it in my navbits which are created by the php file it just echos itself.
Code:

$navbits = construct_navbits(array('' => '&nbsp;</span></li><li class="navbit"><span><a href="/">Home</a></span></li><li class="navbit"><span><a href="../hostindex.php">Web Hosting</a></span></li><li class="navbit lastnavbit"><span>{vb:raw companyname}
 
'));
$navbar = render_navbar_template($navbits);

// ###### YOUR CUSTOM CODE GOES HERE #####
$pagetitle = '$companyname';
$templater = vB_Template::create('headerincludea');
$headerincludea = $templater->render();

$templater = vB_Template::create('webhost-php');
$templater->register('companyname', $companyname);
$templater->render();
 

 
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('pagetitle', $pagetitle);
$templater->register('headerincludea', $headerincludea);


print_output($templater->render());


kh99 05-26-2012 08:43 PM

Try putting $companyname in your navbits instead of {vb:raw companyname} Since it's in a single-quoted string you need to use concatenation, like:

Code:

$navbits = construct_navbits(array('' => '&nbsp;</span></li><li class="navbit"><span>
<a href="/">Home</a></span></li><li class="navbit"><span>
<a href="../hostindex.php">Web Hosting</a></span></li>
<li class="navbit lastnavbit"><span>' . $companyname . '
 
'));


Dave-ahfb 05-26-2012 08:57 PM

Quote:

Originally Posted by kh99 (Post 2333261)
Try putting $companyname in your navbits instead of {vb:raw companyname} Since it's in a single-quoted string you need to use concatenation, like:

Code:

$navbits = construct_navbits(array('' => '&nbsp;</span></li><li class="navbit"><span>
<a href="/">Home</a></span></li><li class="navbit"><span>
<a href="../hostindex.php">Web Hosting</a></span></li>
<li class="navbit lastnavbit"><span>' . $companyname . '
 
'));


Thanks a ton! I had tried $companyname without the '. .' and had the same results (echoed $companyname.

Chr1sj 05-31-2012 11:57 AM

Can anyone help me out what i'm doing wrong?

i'm trying to add something to the Default navbar template. (a server status for my game server)
but first i'm trying out the example but i can't get it to work.

This is my plugin setup.
Product : vBulletin
Hook location: Global_start
Title: server Status
execution order :5

Plugin code:
Code:

/* Some Code, setting variables, (multidimensional) array */
$my_var = "abc";

/* render template and register variables */
$templater = vB_Template::create('navbar');
    $templater->register('my_var', $my_var);
$templater->render();

Then in the navbar template of my default style i add
Code:

{vb:raw my_var}
even tried withing
Code:

<p>{vb:raw my_var}</p>
Anyone able to help me out ?

Edit: and Enable Plugin/Hook System is enabled also

kh99 05-31-2012 12:02 PM

You don't want to render the navbar template because the vbulletin code already does that for you. To get your variable registered to that template you want to use preRegister, like;

Code:

$my_var = "abc";
vB_Template::preRegister('navbar', array('my_var' => $my_var));


Chr1sj 05-31-2012 12:28 PM

Thanks a lot. :up:
Now i can start creating my plugin understanding the preRegister.

codewaggle 06-10-2012 08:41 AM

Quote:

Originally Posted by Abe Babe (Post 1953310)
I have a template that I need to insert into multiple pre-existing templates. After a bit of struggling, I have managed to get the following code running.

Code:

$templater = vB_Template::create('layout_start');
    $templater->register('my_var', $my_var);
$templatevalues['start_insertvar'] = $templater->render();
vB_Template::preRegister('FORUMHOME', $templatevalues);

I have two questions. Is there any way to preRegister for more than one pre-existing template (or a global registration), or do I have to create Plugins for every page I want to add this to (*groan*)? And what is the best hook to have this on. I am currently using 'parse_templates'.

------------------------------------

I'm doing this using $GLOBALS[templatevalues] = $templatevalues; rather than preRegister().

Code:

$templater = vB_Template::create('layout_start');
$templater->register('my_var', $my_var);
$templatevalues['start_insertvar'] = $templater->render();

$GLOBALS[templatevalues] = $templatevalues;

UPDATE: (Thanks to kh99 for his guidance.)
The following line isn't needed and was used incorrectly:
$template->register_global('templatevalues');


Then use this in the templates where you want the output to appear:
Code:

{vb:raw GLOBALS.templatevalues.start_insertvar}
Be Well


All times are GMT. The time now is 07:06 AM.

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

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01618 seconds
  • Memory Usage 1,758KB
  • 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
  • (14)bbcode_code_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (4)pagenav_pagelink
  • (1)pagenav_pagelinkrel
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)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
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete