PDA

View Full Version : A little confused now (calling a template)


HMBeaty
11-14-2009, 12:18 AM
Just got vBulletin 4.0.0 Suite set up on my test site for now, trying to get all of my modifications working before I install it on my live site. So, for those of you who have already had the time to play around with vB 4, how do you call templates now.

I USED to do it like in this thread (https://vborg.vbsupport.ru/showthread.php?t=119933&highlight=call+template+plugin) by using the method below
eval('$mytemplate = "' . fetch_template('mytemplate') . '";');
but that apparently no longer works.

So, anyone know how to call a template via a plugin now? haha
(vBulletin 4.0.0 is definately a lot more confusing than I thought it'd be)

Lynne
11-14-2009, 04:56 AM
First, you need to register the variables you will be using in the template and then you need to render it.
Example from one of mine using a template_hook:
$templater = vB_Template::create('my_custom_template');
$templater->register('a_variable ', $a_variable);
$template_hook['some_hook'] .= $templater->render(); or - using a variable (which must then be registered for us in the next template):
$templater = vB_Template::create('my_custom_template');
$templater->register('a_variable ', $a_variable);
$somevariable .= $templater->render();
or - print_output:
$templater = vB_Template::create('my_custom_template');
$templater->register('a_variable ', $a_variable);
print_output($templater->render());
There is a blog entry by Kier over on vb.com that shows this.

Yellow Slider
11-15-2009, 05:41 PM
Hey, thank you for your reply.
Can you please be more specific?
What shout i put instead of a_variable, $tagforms, $template_hook['some_hook'], etc?
What should be the hook location?

TheLastSuperman
11-15-2009, 06:11 PM
Hey, thank you for your reply.
Can you please be more specific?
What shout i put instead of a_variable, $tagforms, $template_hook['some_hook'], etc?
What should be the hook location?

See it uses { & } now for some things and the vb: is now in all your templates.

<img src="{vb:stylevar imgdir_misc}/paperclip.png" class="inlineimg" />

&

{vb:raw template_hook.postbit_signature_end}

Are some examples, check your templates you'll see codes there and tinker with it, very easy to catch on ;) and your template "postbit signature end" is being called via that code which was located in the postbit template :D.

Lynne
11-15-2009, 06:21 PM
a_variable is the name of the variable you are registering. $tagforms was supposed to be $a_variable, but I forgot to change it - it is now changed. As for the $template_hook, that would be the name of the template_hook you are trying to use.... if you are using a template_hook. I gave several examples up there - you would not use them all for one template call.

Omegatron
11-15-2009, 10:09 PM
How about if the hook you want does not exist. In vb3 say for instance after navbar you could print a blook right about the forumbits below the navbar. There is no such hook in vb4

http://www.vbulletin.com/forum/showthread.php?325960-Template-question-for-plugins

Lynne
11-15-2009, 10:28 PM
Then either add your own hook or modify the template manually.

Omegatron
11-15-2009, 10:44 PM
Are there tutorials on adding your own hook location?

--------------- Added 1258332480 at 1258332480 ---------------

Nevermind got it but still would have not liked to hard code something into forumhome if I did not have to ;)

Lynne
11-15-2009, 10:52 PM
I think I just responded to a thread you made on vb.com. Until they add a hook under the navbar on FORUMHOME, you can just do a str_replace on <div id="pagetitle">

Omegatron
11-15-2009, 11:11 PM
yeah you did I tried the str_replace and it did not work I asked if you knew why it did not.

Lynne
11-16-2009, 03:32 AM
yeah you did I tried the str_replace and it did not work I asked if you knew why it did not.
I can't follow this on two sites. I guess I'll check the other thread tomorrow morning. However, if you didn't post all your code over there, then I'm not gonna be able to help. (Actually, you should probably continue this over here if you need code help since they don't like discussing plugin code and such over there.) So, post your code here and we'll see if we can figure out what is wrong. Include template code if you are calling templates also.

Pitman
11-16-2009, 05:58 AM
First, you need to register the variables you will be using in the template and then you need to render it.
Example from one of mine using a template_hook:
$templater = vB_Template::create('my_custom_template');
$templater->register('a_variable ', $a_variable);
$template_hook['some_hook'] .= $templater->render(); or - using a variable (which must then be registered for us in the next template):
$templater = vB_Template::create('my_custom_template');
$templater->register('a_variable ', $a_variable);
$somevariable .= $templater->render();
or - print_output:
$templater = vB_Template::create('my_custom_template');
$templater->register('a_variable ', $a_variable);
print_output($templater->render());
There is a blog entry by Kier over on vb.com that shows this.I'm not a coder or anything, but I do require certain small plugins that would most likely be only useful for me. I have played with these examples, as well as ones previously provided on vBulletin.com, but have failed to wrap my head around how it is actually supposed to work. In my case, I simply need to be able to use a custom variable containing data in templates. One simple example of this would be as follows:$random_number = rand(1, 105);How exactly should the plugin be formulated to make the variable $random_number accessible in the header template?

Omegatron
11-16-2009, 09:23 AM
well your plugin would hook where ever you wish and to access the variable in the header template it would be something like this

{vb:raw random_number}

--------------- Added 1258370775 at 1258370775 ---------------

Lynne thanks I posted there because its a huge design flaw in the new system IMO. I added my own hook name and it works fine but using the str_replace or something is needed I think because the whole point of the plugin system is to NOT make hand edits to files templates etc.

anyway here is the code I posted using etc


Template question for plugins.

Now say for instance I want to show a block of info on forumhome. I know I can do this since the template_hook is now a super global.


$templater = vB_Template::create('forumhome_addon');
$templater->register_page_templates();
$templater->register('block_info', $block_info);
$template_hook['navbar_end'] .= $templater->render();


That works fine prints the block at the end of the navbar and everything is fine. But I dont want it there. See in vb3 you did a fetch_template and str_replace on the $navbar variable and added your template under so it showed up before your forum list. This is what I want to be able to add a block of info before the $forumbits. Nothing I do a str_replace on works because there is no hook there. The plugin is at forumhome_complete and this does not work



$templater = vB_Template::create('forumhome_addon');
$templater->register_page_templates();
$templater->register('block_info', $block_info);
$thistemplater = $templater->render();

$search_text = '<div id=\"pagetitle\">';
$vbulletin->templatecache['FORUMHOME'] = str_replace($search_text,$thistemplater.$search_te xt,$vbulletin->templatecache['FORUMHOME']);



I have tried doing a str_replace for that div tag or forumbits etc. Anyone have any hints on how to get it to work where I want it. Like I said it works fine if like add my new template into the actual template hook but thats the wrong location. There is no hook it seems at the top of the forum which IMO is an oversight on IB's part since in vb3 the top of the forumhome was right after $navbar and now it isnt

Pitman
11-16-2009, 09:43 AM
well your plugin would hook where ever you wish and to access the variable in the header template it would be something like this

{vb:raw random_number}Well, I understand that much of it, but I'm not sure about how to go about formatting the plugin and registering the variable in the way that I need to. Any way that I've tried to register the variable in the provided examples I've seen anywhere have proved to be ineffective. I understand how to use the variable in the templates, but I'm unsure of how to correctly register the variable in the plugin while effectively being able to use {vb:raw random_number} in a specific and already available template.

Furthermore, while providing more information, in the following example: $templater = vB_Template::create('my_custom_template');
$templater->register('a_variable ', $a_variable);
$somevariable .= $templater->render(); I only understand the $templater->register('a_variable ', $a_variable); there. Being that I'm not creating a template, the first line of code makes no sense to me. I also do not understand the render code or what I should use there either.

Paul M
11-16-2009, 10:34 AM
Lynne thanks I posted there because its a huge design flaw in the new system IMO.
What design flaw do you mean exactly ?

Omegatron
11-16-2009, 10:52 AM
Well if you can no longer display a block before the forumbits because you can not longer do a str_replace and there is not a hook for there its a design flaw. I do not beleive the point of vb4 was to go backwards from vb3 in the plugin system. if vb is going to make the only way you can include a template is through the hook system since its a superglobal then they need to do hooks everywhere. The whole purpose of the plugin system is to keep you from having to hack vb files templates.

i did what I needed to do by creating a new template hook name and editing the forumhome template to include the template hook where i wanted it. Remember in vb3 how alot of hacks appeared right before the forumbits or right after. there are no hook locations in forumhome other than in who's online. In vb3 you just did a str_replace to print your template right after the navbar. So if you can not longer do a str_replace and have to use a hook instead and there are non then its a design flaw especially somewhere as prized a location as the top of bottom of the forumbits. Just my Opinion ;)

--------------- Added 1258376073 at 1258376073 ---------------

Pitman

You need to register any variables for use in a template. The initial post in this thread by lynne gives examples ;)

ragtek
11-16-2009, 10:59 AM
I think there's now a way to preRegister the variables, so as i understood it, you'll need no more str_replace ;)

I hope realy that there will be some great articles for vB4.
The best way would be, if some vbulletin developers would write articles, because they could show "us" the best way to extend vB4

Yellow Slider
11-16-2009, 11:48 AM
a_variable is the name of the variable you are registering. $tagforms was supposed to be $a_variable, but I forgot to change it - it is now changed. As for the $template_hook, that would be the name of the template_hook you are trying to use.... if you are using a template_hook. I gave several examples up there - you would not use them all for one template call.

Unfortunately, it still doesn't work for me.
For example, if i want to use the template_hook method, and my template name is abc, my var name is var, and i want to use it in the forumhome template, the code should be something like this?

$templater = vB_Template::create('abc');
$templater->register('var ', $var);
$template_hook['forumhome'] .= $templater->render();

Shadab
11-16-2009, 12:13 PM
@Yellow Slider:

If I understand correctly, you want to evaluate the template "abc"
and make the result available in the FORUMHOME template via the variable "var" ?

Yellow Slider
11-16-2009, 12:20 PM
@Yellow Slider:

If I understand correctly, you want to evaluate the template "abc"
and make the result available in the FORUMHOME template via the variable "var" ?

Yes.

Shadab
11-16-2009, 12:25 PM
Yes.
Try the code below:

$abcHTML = vB_Template::Create('abc')->render();

vB_Template:preRegister('FORUMHOME', array('var' => $abcHTML));

Then you can use {vb:raw var} in your Forumhome template to insert the evaluated html.
Let me know how it goes.

Yellow Slider
11-16-2009, 12:32 PM
Try the code below:

$abcHTML = vB_Template::Create('abc')->render();

vB_Template:preRegister('FORUMHOME', array('var' => $abcHTML));

Then you can use {vb:raw var} in your Forumhome template to insert the evaluated html.
Let me know how it goes.

I get en error:
Parse error: syntax error, unexpected ':' in public_html/forum/global.php(28) : eval()'d code on line 3

Shadab
11-16-2009, 12:34 PM
I get en error:
Parse error: syntax error, unexpected ':' in public_html/forum/global.php(28) : eval()'d code on line 3
A colon was missing in line 3. Sorry. Try this:

$abcHTML = vB_Template::create('abc')->render();

vB_Template::preRegister('FORUMHOME', array('var' => $abcHTML));

Yellow Slider
11-16-2009, 12:39 PM
A colon was missing in line 3. Sorry. Try this:

$abcHTML = vB_Template::create('abc')->render();

vB_Template::preRegister('FORUMHOME', array('var' => $abcHTML));

Great, it's working, thank you :)

--------------- Added 1258383869 at 1258383869 ---------------

A colon was missing in line 3. Sorry. Try this:

$abcHTML = vB_Template::create('abc')->render();

vB_Template::preRegister('FORUMHOME', array('var' => $abcHTML));

One more question, why did you add the "HTML" at the end of the variable?

Omegatron
11-16-2009, 01:51 PM
Because the template example he gave the name of the template was $abcHTML thats all in his example.

Now the point I am making is I can make what I am doing work by defining a new hook works great. even pre registering values can work fine but that still means you need to hand edit the actual template you want to insert it to and place the variable there. With str_replace you never had to hand edit templates plugins where just that true plugins without having to touch the vb code. If I upgrade vb then one will have to redo there template edits which to me is a huge design flaw. If the vb developers are going to lock you into doing things a certain why then they really need to place template hooks in alot of places. ;)

Paul M
11-16-2009, 02:06 PM
Since I am at work, I cannot check your assertion that str_replace() cannot be used, but even if it were true, its not a design flaw in vb4 - it was never an official plugin method, but mearly a hack that happened to work. So if anything, it was a design flaw in 3.x

Off the top of my head, I cant think why it would no longer work, but as I said, Im at work, so cant check anything.

Lynne
11-16-2009, 02:11 PM
Because the template example he gave the name of the template was $abcHTML thats all in his example.

Now the point I am making is I can make what I am doing work by defining a new hook works great. even pre registering values can work fine but that still means you need to hand edit the actual template you want to insert it to and place the variable there. With str_replace you never had to hand edit templates plugins where just that true plugins without having to touch the vb code. If I upgrade vb then one will have to redo there template edits which to me is a huge design flaw. If the vb developers are going to lock you into doing things a certain why then they really need to place template hooks in alot of places. ;)
I think you may want to start a thread an post your code in there. In on of my mods, I preregistered variables and then used str_replace to put them in a template. So, it can be done just like before with a little bit of registering/preregistering of your variables.

Omegatron
11-16-2009, 02:27 PM
I already did my friend in post 13 ;) I basically responded right to you when you asked me to post the code.

I defined one variable for use in my template as thats all that is needed but I can not get the template included where I want because there is no hook using a str_replace

Defining a new hook works great thats not an issue but I want to keep from having to hand edit vb templates in any manner.

https://vborg.vbsupport.ru/showpost.php?p=1915136&postcount=13

Lynne
11-16-2009, 02:41 PM
I already did my friend in post 13 ;) I basically responded right to you when you asked me to post the code.

I defined one variable for use in my template as thats all that is needed but I can not get the template included where I want because there is no hook using a str_replace

Defining a new hook works great thats not an issue but I want to keep from having to hand edit vb templates in any manner.

https://vborg.vbsupport.ru/showpost.php?p=1915136&postcount=13
I was suggesting your own new thread because this one has so many users posting in it and it's hard to follow conversations.

Anyway, I don't think your search/replace is correct. You wrote:
$search_text = '<div id=\"pagetitle\">';
$vbulletin->templatecache['FORUMHOME'] = str_replace($search_text,$thistemplater.$search_te xt,$vbulletin->templatecache['FORUMHOME']);
And you don't need to escape the double-quotes since you have it inside single quotes.
$search_text = '<div id="pagetitle">';
$vbulletin->templatecache['FORUMHOME'] = str_replace($search_text,$thistemplater.$search_te xt,$vbulletin->templatecache['FORUMHOME']);

Omegatron
11-16-2009, 05:15 PM
If I do what you say its a parse error. In all my days of PHP you need to escape quotes when you use html in a php file. ;)

Lynne
11-16-2009, 05:23 PM
Really? Strange, because I have something almost exactly like that in a plugin and it works just fine:
$find = '<label for="select_daysprune">';
$add_before = '<label style="display:inline" for="uid">'.$vbphrase[userid].'</label>
and more stuff
';

$vbulletin->templatecache['moderation_filter'] = str_replace($find,$add_before.$find,$vbulletin->templatecache['moderation_filter']); I wonder if it's because you didn't preregister your variable after you defined it.
vB_Template::preRegister('FORUMHOME', array('thistemplater ' => $thistemplater ));


And, I doubt you need this line:
$templater->register_page_templates();

Omegatron
11-16-2009, 05:25 PM
yeah it does not like this line



$thistemplater .= $templater->render();


--------------- Added 1258406169 at 1258406169 ---------------

This is what I have blank white page. I am following the guides here


$thistemplater .= $templater->render();

vB_Template::preRegister('FORUMHOME', array('thistemplater ' => $thistemplater ));

$search_text = '<div id="pagetitle">';
$vbulletin->templatecache['FORUMHOME'] = str_replace($search_text,$thistemplater.$search_te xt,$vbulletin->templatecache['FORUMHOME']);

cellarius
11-17-2009, 07:54 AM
In all my days of PHP you need to escape quotes when you use html in a php file. ;)
Then you were wrong all those days.

Try
echo 'This is <span style=\"font-weight: bold\">bold</span>';This will output in the html source code:
This is <span style=\"font-weight: bold\">bold</span>And, needless to say, the text will not be bold in the browser.

On the other hand:
echo 'This is <span style="font-weight: bold">bold</span>';
will work just fine. No escapes there...

--------------- Added 1258452044 at 1258452044 ---------------

If I do what you say its a parse error.
You must be using a different PHP than me. I just tested Lynne's code in a plugin and it works just fine. No parse error whatsoever.

--------------- Added 1258452277 at 1258452277 ---------------

yeah it does not like this line



$thistemplater .= $templater->render();
--------------- Added 16 Nov 2009 at 22:16 ---------------

This is what I have blank white page. I am following the guides here


$thistemplater .= $templater->render();

vB_Template::preRegister('FORUMHOME', array('thistemplater ' => $thistemplater ));

$search_text = '<div id="pagetitle">';
$vbulletin->templatecache['FORUMHOME'] = str_replace($search_text,$thistemplater.$search_te xt,$vbulletin->templatecache['FORUMHOME']);

I just threw this code into a mod of mine instead of the template hook it normally uses. This works perfectly fine for me.
Please post the complete code you are using.

Omegatron
11-17-2009, 09:21 AM
If you dont know the scope of the entire discussion please do not be rude with comments like that.


$output = "This is <span style=\"font-weight: bold\">bold</span>";


Most definately needs to be escaped.

I have what I am doing done. This line is not liked is posted. I simply ended up creating a new hook location.


$thistemplater .= $templater->render();

cellarius
11-17-2009, 09:39 AM
If you dont know the scope of the entire discussion please do not be rude with comments like that.


$output = "This is <span style=\"font-weight: bold\">bold</span>";
Most definately needs to be escaped.
Yes, this line needs to be escaped. However, the previous example (which came from you, originally!) used single quotes for the echo statement. You may want to reflect on that difference before calling me rude.

I have what I am doing done. This line is not liked is posted. I simply ended up creating a new hook location.

As I said, the whole thing you wanted to do works perfectly well for me in my plugin, and it obviously does for Lynne. Maybe you did not want to hear that, as you did not want to post your whole code, but that's your decision absolutely. Glad it worked out for you, even if using other ways than intended.