View Full Version : How Can I use a custom template as a variable
EquinoxWorld
07-08-2011, 01:44 PM
Hello everyone, does anyone know how I can use a custom template (for example: COFTW_FAQ) and insert it as a variable into another template (OFTW). I saw this article here https://vborg.vbsupport.ru/showthread.php?t=119933 but it's not working for me with vb4. Anyone know where I can find updated documentation on this? Basically I want to use a custom template inside another template using a plug in. Any ideas anyone? Please help. :)
<a href="https://vborg.vbsupport.ru/showthread.php?t=228078" target="_blank">https://vborg.vbsupport.ru/showthread.php?t=228078</a>
EquinoxWorld
07-08-2011, 02:20 PM
https://vborg.vbsupport.ru/showthread.php?t=228078
Thank you for the link. Although I see that I can register a variable and use it in a template, that's fine but how do I register a template as a variable then use it in another template? I'm so lost right now, any help will be so much appreciated.
Basically I want to crate a "shell" and then use {vb:raw oftw_faq} (which is the contents of another custom template) inside that template.
Scroll down to the section that's labeled "Save into a variable for later use in custom template". Basically you save the output of render() into a variable, then you use it in another template like you would any other variable. If the "including" template is also one you're rendering in your code, then you can just register the variable as you would any other. If you want to include it in one of the existing vb templates, then you probably need to use vB_Template::preRegister() (search for that in the article linked above).
Using examples from the article,
$templater = vB_Template::create('mytemplate1');
$templater->register('my_var', $my_var);
$templater->register('my_array', $my_array);
$mytemplate_rendered = $templater->render();
...
$templater = vB_Template::create('mytemplate2');
$templater->register('my_template1', $mytemplate_rendered);
$templater->register('my_array2', $my_array2);
$mytemplate2_rendered = $templater->render();
EquinoxWorld
07-08-2011, 02:59 PM
So it would be 2 plug-ins that I wold have to create right? One to render the template as a variable then another to render the variable in the other template?
--------------- Added 1310141238 at 1310141238 ---------------
OK this is what I got so far:
First Plug-in to register the template into a variable:
$templater = vB_Template::create('COFTW_FAQ');
$templater->register('oftw_faq', $oftw_faq);
$mytemplate_rendered = $templater->render();
Second Plug-in to register my variable into the "shell" template:
$templater = vB_Template::create('OFTW');
$templater->register('COFTW_FAQ', $mytemplate_rendered);
$mytemplate2_rendered = $templater->render();
And I'm using: {vb:raw oftw_faq} in the OFTW template but it does not show up still. ANy ideas what I could be doing wrong?
It could be two plugins or the same plugin, it just depends on what you're doing (if you're using the same hook location for both, then you definitely don't need two plugins).
Based on the code you posted, you'd need {vb:raw oftw_faq} in the 'COFTW_FAQ' template to see $oftw_faq, and {vb:raw COFTW_FAQ} in the 'OFTW' template to see the first template.
Also if you use two plugins, depending on the hook locations, you might need "global $mytemplate2_rendered;" in one or both locations.
BTW, you're missing a couple of quotes in the code for the second template.
EquinoxWorld
07-08-2011, 03:28 PM
Basically I want to create a "shell" template with OFTW. With OFTW being the main template and using COFTW_FAQ template as an insert into OFTW. The script that uses OFTW is oftw.php and I just created template COFTW_FAQ with the following contents which I want to insert into OFTW:
<div class=block>
<h2 class=blockhead>F.A.Q</h2>
<div class=blockbody>Hello
</div>
</div>
Does that mean I have to include the second template in oftw.php?
Updated Plug-ins: (still not working :( ) (both with global_start as hook)
$templater = vB_Template::create('COFTW_FAQ');
$templater->register('oftw_faq', $oftw_faq);
$mytemplate_rendered = $templater->render();
$templater = vB_Template::create('OFTW');
$templater->register('COFTW_FAQ', $mytemplate_rendered);
$mytemplate2_rendered = $templater->render();
And using {vb:raw COFTW_FAQ} in OFTW template but still not getting anything. Any more ideas as to why still not showing up anything?
If you're using the same hook location then you should probably just use one plugin. But if you want to use two for whatever reason, then you should set the execution order fields so that they run in the order you expect.
But if you've created a php file called oftw.php, then you may not need to use plugins at all, just put the code you need in the file. There's nothing magic about a plugin, it just lets you add code without editing the existing php files.
EquinoxWorld
07-08-2011, 03:57 PM
If you're using the same hook location then you should probably just use one plugin. But if you want to use two for whatever reason, then you should set the execution order fields so that they run in the order you expect.
But if you've created a php file called oftw.php, then you may not need to use plugins at all, just put the code you need in the file. There's nothing magic about a plugin, it just lets you add code without editing the existing php files.
True although for what I am trying to accomplish it is better. I am trying to use javascript to load diferent content (within the OFTW template) inside a div. using this code for example:
<li class="usercp_nav"><a href="JavaScript:void()" onclick="document.getElementById('DivExample').innerHTML = '<p>{vb:raw COFTW_FAQ}</p>';">F.A.Q</a></li>
So I can create a side menu and when clicking on one of the menu links the content on the right changes so I wanted to use plug ins so I won't have to write the entire code instead of just {vb:raw COFTW_FAQ}. This would make it easier to edit the content of each menu item. I know I can just have the content load non a different page but I am trying to have ti inline with JavaScript.
vBulletin® v3.8.12 by vBS, Copyright ©2000-2024, vBulletin Solutions Inc.