PDA

View Full Version : Using variables in templates


DRJ
07-24-2013, 07:49 PM
I am having issues with this. I just upgraded from 3 to 4 and want to have a variable that I can use in an If Statement in a template.

So in a template I can do this:

<vb:if condition="$var == 1">

In a pluggin I can do this:

$var=1;
vB_Template::preRegister('header',array('var' => $var));

Now if I use {vb:raw var} I get the value but when I try to use that in an If statement I can't get it to work.

If I try to use $var I just get the text $var and not the value.

Please advise.

Thanks

Zachery
07-24-2013, 07:55 PM
You're likely putting your code in the wrong plugin, which pluigin do you have it in, did you try moving it to another plugin point?

tbworld
07-24-2013, 08:20 PM
What template are you trying to insert into?

DRJ
07-24-2013, 09:32 PM
I added it to global start, I tried a few others but was just guessing at that point.

I want it in the header.

If I do VB:Raw I can get the value no problem.

But if I do $Var, I get just the text.

The issue being I want to be able to use an If Statement.

Basically this is for ads and I have a variable saying what type it is, 1 for flash, 2 for image. Before I had an If conditional and based on that variable I could play the flash or add a hyperlink for the image.

But can't seem to get the syntax right for using the variable in the If statement.

Thanks

tbworld
07-24-2013, 11:25 PM
I am having issues with this. I just upgraded from 3 to 4 and want to have a variable that I can use in an If Statement in a template.

So in a template I can do this:

<vb:if condition="$var == 1">

In a pluggin I can do this:

$var=1;
vB_Template::preRegister('header',array('var' => $var));
Now if I use {vb:raw var} I get the value but when I try to use that in an If statement I can't get it to work.
Thanks

Try hook: parse_templates

Show me your "if condition" statement.

DRJ
07-25-2013, 04:00 AM
Not sure what I was doing wrong before, but now it seems to be working fine.

I ended up deactivating all the custom plugins because I was getting errors elsewhere. And now when I try adding these variables the same way I was before it works so not sure what was the cause of the issue.

Could have been a typo as well but I tried it several times with simple variables like $x and it wouldn't work so not really sure.

Thanks for the assistance.

tbworld
07-25-2013, 04:06 AM
Not sure what I was doing wrong before, but now it seems to be working fine.

I ended up deactivating all the custom plugins because I was getting errors elsewhere. And now when I try adding these variables the same way I was before it works so not sure what was the cause of the issue.

Could have been a typo as well but I tried it several times with simple variables like $x and it wouldn't work so not really sure.

Thanks for the assistance.

Reading here on vb.org, it is not uncommon to hear that solution. I think the plugin cache in the datastore for some reason does not get updated.

I currently have a board that is doing the same behavior, but only on "headinclude". The template is rendered correctly in database, the right plugin code is in the datastore, the right template page (styleid) is displaying -- yet the variable is not being replaced in the template. I am a bit baffled, but I aim to track down the problem tonight.

I normally just put the code directly in the template, but after reading your post and @chris's; I decided to check it out.