Log in

View Full Version : Make custom templates and call it with "vb:raw"


MyLibary
05-11-2012, 07:03 PM
Hello all,

Im trying to make some custom template for example i have created template name:
custom_header_mysite_beta

and i would like to use the content in this code in aother template, for example at header template in the next way:

{vb:raw ad_location.global_header1}

In my case:

{vb:raw custom_header_mysite_beta}

How can i do that?

kh99
05-11-2012, 07:43 PM
You need to create a plugin, render your template, and preRegister the result to the header template. Hook location parse_templates is a good one to use, and the code could be like:

$template = vB_Template::create('custom_header_mysite_beta');
$customheader = $template->render();
vB_Template::preRegister('header', array('custom_header_mysite_beta' => $customheader));

MyLibary
05-11-2012, 07:54 PM
I just take it exactly like it is and paste it in a new plugin?
How should i name the plugin?

Once i have done it template will be created and i will be able to use this code:
{vb:raw custom_header_mysite_beta}

in the header?

Thanks alot!

kh99
05-11-2012, 07:56 PM
I just take it exactly like it is and paste it in a new plugin?
How should i name the plugin?

Yes, you should be able to copy and paste that code. You can name the plugin anything you want - name it something so that years from now when you wonder what it is, the title might give you some idea :).


Once i have done it template will be created and i will be able to use this code:
{vb:raw custom_header_mysite_beta}

in the header?

Yes, that should work.

MyLibary
05-11-2012, 08:02 PM
May i ask what the last row mean?
vB_Template::preRegister('header', array('custom_header_mysite_beta' => $customheader));

What is that $customheader and what if i change it to something else?

kh99
05-11-2012, 08:06 PM
May i ask what the last row mean?
vB_Template::preRegister('header', array('custom_header_mysite_beta' => $customheader));


Before you can use something like {vb:raw custom_header_mysite_beta} in a template, you must register (or preRegister) a value for custom_header_mysite_beta. There's an article describing this process here: https://vborg.vbsupport.ru/showthread.php?t=228078 (Sorry, I probably should have sent you to that article first).


What is that $customheader and what if i change it to something else?

You can change it to whatever you want as long as you change it on the line above as well. The point is that the result of render() is what is registered to custom_header_mysite_beta. ($customheader is just a temporary variable).

MyLibary
05-11-2012, 08:10 PM
Works great!

Do i have limit of how much plugins like that i can create?
Or in other words will it slow my website if i will create up to 20 custom plugins like this?

Thanks alot for your time and your help!

kh99
05-11-2012, 08:15 PM
Do i have limit of how much plugins like that i can create?
Or in other words will it slow my website if i will create up to 20 custom plugins like this?

I don't think it matters how many plugins you have since all the code for a given hook location gets built in to one string and loaded from the datastore in one call. But unless you have a reason to want to keep them separate, there's no reason you couldn't do it all in one plugin.

You can also use one preRegister call (assuming they're all going to the header template), like:

$template = vB_Template::create('template1');
$templatevalues['template1'] = $template->render();

$template = vB_Template::create('template2');
$templatevalues['template2'] = $template->render();

$template = vB_Template::create('template3');
$templatevalues['template3'] = $template->render();

vB_Template::preRegister('header', $templatevalues);


But if you'd rather make them separate, I don't think there's a lot of overhead involved in multiple plugins or preRegister calls.

MyLibary
05-11-2012, 08:23 PM
So you say i can do something like this in the same plugin right?


$template = vB_Template::create('custom_header_mysite_beta1');
$customheader1 = $template->render();
vB_Template::preRegister('header', array('custom_header_mysite_beta1' => $customheader1));

$template = vB_Template::create('custom_header_mysite_beta2');
$customheader2 = $template->render();
vB_Template::preRegister('footer', array('custom_header_mysite_beta2' => $customheader2));

$template = vB_Template::create('custom_header_mysite_beta3');
$customheader3 = $template->render();
vB_Template::preRegister('headerinclude', array('custom_header_mysite_beta3' => $customheader3));


And if i use this string you suggested

should i call in this way:

{vb:raw template1}
{vb:raw template2}
{vb:raw template3}

?

Thanks alot for you kind help!!

kh99
05-11-2012, 08:31 PM
So you say i can do something like this in the same plugin right?

Yes, that code looks OK to me.


And if i use this string you suggested

should i call in this way:

{vb:raw template1}
{vb:raw template2}
{vb:raw template3}

?


Yes, if you were to use the example code I posted above. The array key is what you use in the vb:raw tag.

MyLibary
05-11-2012, 08:51 PM
No way to thank you, you are amazing!!!

Thanks alot for your lovely help!