PDA

View Full Version : Call a custom template in various places


Scimia
07-21-2015, 11:58 AM
I have this code

<vb:if condition="is_member_of($post, 6, 69)">
<div class="targhetta admin"><span class="orecchie"></span><span class="text">Amministratore</span></div>
<br style="content: ' '; display: block; margin-top: 5px;" />
</vb:if>
<vb:if condition="is_member_of($post, 5)">
<div class="targhetta smod"><span class="orecchie"></span><span class="text">Supermoderatore</span></div>
<br style="content: ' '; display: block; margin-top: 5px;" />
</vb:if>
<vb:if condition="is_member_of($post, 7)">
<div class="targhetta mod"><span class="orecchie"></span><span class="text">Moderatore</span></div>
<br style="content: ' '; display: block; margin-top: 5px;" />
</vb:if>

I want to create a custom template and call it in postbit_legacy and memberinfo, how can I do this?

kh99
07-21-2015, 01:15 PM
You would need to use a plugin. In the plugin you'd set up any variables you need, register them to your template, then render the template and save the output in a variable. You'd then register that variable to the postbit template, and then use 'vb:raw' in postbit to put it where you want.

I know that sounds complicated, but it's not really that difficult. There's an article here on how to render a template: https://vborg.vbsupport.ru/showthread.php?t=228078

Scimia
07-21-2015, 02:28 PM
You would need to use a plugin. In the plugin you'd set up any variables you need, register them to your template, then render the template and save the output in a variable. You'd then register that variable to the postbit template, and then use 'vb:raw' in postbit to put it where you want.

I know that sounds complicated, but it's not really that difficult. There's an article here on how to render a template: https://vborg.vbsupport.ru/showthread.php?t=228078

Appears a bit complicated also in the article, can't you write me some specific example for my problem?

For example, I have a custom template called "targhette" (that contains the code I've posted above) and i want to call it with {vb:raw targhette} in postbit_legacy and memberinfo, how I can do this?

I need to create a new plugin with this code? Is this correct?

$templater = vB_Template::create('targhette');
$templater->register_page_templates();
$templater->register('my_var', $my_var);
$templater->register('my_array', $my_array);
print_output($templater->render());

kh99
07-21-2015, 02:43 PM
That's pretty close. I think like this:

$templater = vB_Template::create('targhette');
$templater->register_page_templates();
$templater->register('my_var', $my_var);
$templater->register('my_array', $my_array);
$targhette = $templater->render();

vB_Template::preRegister('postbit_legacy', array('targhette' => $targhette));

and using hook postbit_display_complete, probably. For memberinfo you'd need another plugin (and change the template name in preRegister, of course). For that one use hook member_complete instead. Also, there is no $post for the memberinfo, but the user's info is in $userinfo, so you could just put $userinfo in place of $post in the register call. If you leave the name in the register call as 'post' then you can use the same template.

Edit: oh, I'm assuming that you know that the my_var and my_array register lines are just examples. If you use variables in your custom template, you'd want to replace those two lines with one or more lines to register your variables.

Scimia
07-21-2015, 04:06 PM
That's pretty close. I think like this:

$templater = vB_Template::create('targhette');
$templater->register_page_templates();
$templater->register('my_var', $my_var);
$templater->register('my_array', $my_array);
$targhette = $templater->render();

vB_Template::preRegister('postbit_legacy', array('targhette' => $targhette));

and using hook postbit_display_complete, probably. For memberinfo you'd need another plugin (and change the template name in preRegister, of course). For that one use hook member_complete instead. Also, there is no $post for the memberinfo, but the user's info is in $userinfo, so you could just put $userinfo in place of $post in the register call. If you leave the name in the register call as 'post' then you can use the same template.

Edit: oh, I'm assuming that you know that the my_var and my_array register lines are just examples. If you use variables in your custom template, you'd want to replace those two lines with one or more lines to register your variables.

I've got this working for the postbit, but now i'm having problem to get this work in user profile, I need to use the same template (targhette) but in this page.

I'm trying with this plugin:


$templater = vB_Template::create('targhette');
$templater->register_page_templates();
$templater->register('userinfo', $post);
$targhette = $templater->render();

vB_Template::preRegister('memberinfo_block_minista ts', array('targhette' => $targhette));


And I write {vb:raw targhette} in memberinfo_block_ministats, but doesn't work.
The template hook i'm using is member_complete.

kh99
07-21-2015, 04:23 PM
I think you want to change the userinfo register line like this:

$templater->register('post', $userinfo);

kh99
07-21-2015, 04:29 PM
Oh, if you're putting it in a block you probably need a different hook. Maybe try 'member_execute_start' instead.