View Full Version : Including a loop
BaussDesigns
05-01-2010, 02:46 PM
I have a question - If I make a while or for loop in a plugin, how could I include it in a template?
What I want to do?
I want to make a table with all users and some custom fields with a mysql query. Or could I make it ready in the plugin and then include it in the template? If so, how?
Hi BaussDesigns,
i hope i understood u in the right way, here is an example of my code how I am solving that issue:
$result = $vbulletin->db->query_read($sql); //have a query here such like "SELECT * FROM" . TABLE_PREFIX and what so ever ....
$dataformeeting = $this->makeArray( $result ); // i got a make Array method in my class, because I like to work with multidimensional arrays however what i get is something like this:
//array(0=>array("id"=>1), 1=>array('title'=>"testTitle"), ... and so on)
//here starts your requested part i am fetching a template which just has something like this
//<tr>
//<td>{vb:raw value.id}</td>
//<td>{vb:raw value.title}</td>
//</tr>
//make template ready
foreach ($dataformeeting as $item => $value)
{
$templater = vB_Template::create('meeting_my_meetings_list');
$templater->register('value', $value); //u have to register a coustom var for your template, in my case i use the value which i got from the foreach
$mymeetingslist .= $templater->render(); // it is absolutely important that u have .= because u are in a loop, and u dont want to overwrite everytime the $mymeetinglist (in my case)
}
Thats it basically
hope it helps you at least in getting an idea!
Andreas
05-02-2010, 03:39 PM
Rendering very small templates (that are used nowhere else) in a loop is kinde wasting resources.
It would be a lot more efficient to use <vb:each> in the parent template.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2025, vBulletin Solutions Inc.