PDA

View Full Version : calling a $_SERVER['REQUEST_URI'] within a template


bleavitt
06-03-2010, 09:11 PM
I'm in the process of migrating from 3.0 to 4.0 setup of vBulletin. I've noticed that in 4.0, php tags do not work within the template files themselves. Hence it would seem that something like {vb:var variable_name} is the format I need to employ for what I'm trying to accomplish. I've got a custom login redirect that I'm working to implement. To do so, I need to call the equivalent of $_SERVER['REQUEST_URI'] within the template.

My question is does such a predefined variable exist? If not, is there an easy way to create this variable? Basically, what I am doing is sending a call to a login page which needs to know which page the user was on when the call was initiated.

I've already spent several hours searching the forums and Google for a solution to no avail. I've also read through the Template Tags and Languages and Phrases section without much success.

I'm completely new to the vBulletin environment, so any help would be appreciated.

Attilitus
06-03-2010, 09:17 PM
Here is a general solution:

Add this plugin code to init_startup (or any other plugin location occurring before your template is registered.)

$myarray=array();
$myarray['variable_name']='variable content';
vB_Template::preRegister('template_name', $myarray);


The result is that {vb:var variable_name} will now take on the value 'variable content' in the template 'template_name'.

bleavitt
06-03-2010, 09:26 PM
Thanks Attilitus for your response,

However, I'm not seeing how this will allow me to use the php global variable $_SERVER['REQUEST_URI'] within a vBulletin template. Maybe, I'm missing something?

Thank you.

Attilitus
06-03-2010, 09:27 PM
Your problem's solution is a special case of my general solution.


$myarray=array();
$myarray['variable_name']=$_SERVER['REQUEST_URI'];
vB_Template::preRegister('template_name', $myarray);

bleavitt
06-03-2010, 09:39 PM
Ok, that makes more sense. This leads to two more questions then. Where do I go to find a suitable plugin to tack this bit of code on to? I've got FTP access, I wouldn't even know wher to begin looking. Or, is this something that can be achieved from the control panel?

And then just to make sure I understand the syntax, in my template file, I'd use something like:

{vb:raw $myarray['variable_name'] }

Is that correct?

Thanks again.

Attilitus
06-03-2010, 09:44 PM
No. As I explained in the first post:


The result is that {vb:var variable_name} will now take on the value 'variable content' in the template 'template_name'.


In words: The keys of the array are the names of the variables to be accessed inside your template.

To add this: go into your admincp, and Add New Plugin. The location of the plugin should be init_start, and the plugin code should be what I posted here.

Alternatively, since it sounds as if you have limited experience: you might wait for someone else to come along with alternative advice on how to solve your problem. (vBulletin might provide you server variables in templates already, I'm not sure.)

~Tim

bleavitt
06-04-2010, 03:38 PM
Thanks Tim again for your help,

I apologize for overlooking the second part of your first post. I think I've followed your instructions. However, I'm still in the dark. I've not been able to make your suggestion work, as far as I can tell.

In the Add a New plugin section, I've followed your instruction as explained above, Here's the code that I created based off of what you suggested above:

$server_vars=array();
$server_vars['request_uri']=$_SERVER['REQUEST_URI'];
vB_Template::preRegister('TestSite_Beta', $server_vars);

I've associated it with init_startup hook and gave it a title/name of "Request URI variable" (though that seems irrelevant). Then in the template file "header" for the "TestSite_Beta" template, where we're replacing the standard in-page login with a link to another login page -- the link that is going in place of the login form is needing to have a "Requesting URI" variable embedded into it. So I've used {vb:var request_uri} where I want the requesting URI to appear.

The full line of html looks like this:
<a rel="nofollow" href="/login/icslogin.php?destination={vb:var request_uri}">Register | Login</a>

When it is tested however, I cannot get it display any sort of variable for the requesting URI. This is where I'm stuck. It comes out blank in the generated html.

Attilitus
06-04-2010, 07:13 PM
Then in the template file "header" for the "TestSite_Beta" template
I do not understand what this means.

My provided code will make the variable {vb:var variable_name} available in the template with name 'template_name'. If the template in which you are adding the variable is not named exactly 'template_name' then the variable will not be accessible from that template.

Perhaps this is your problem:

Note that once a template is parsed, all non-registered variables are removed from the template string. That is: If you have two templates: template1 and template2. Then suppose you render template1 and store it in variable var_template1, then put {vb:raw var_template1} in template2. Registering a variable for template2, will not allow you to use that variable in template1.

bleavitt
06-14-2010, 05:25 PM
Just to conclude this thread, here's the solution that worked for me. Attilitus was right on with his suggestions above below is the specific solution that worked for me:

In the Plugins Manager under the Plugins/Products menu, I created a new plugin, set the hook to init_startup and then used this line of code:


$server_varis=array();
$server_varis['request_uri']=$_SERVER['REQUEST_URI'];
vB_Template::preRegister('header', $server_varis);


The confusion was in the name of the template file that I was pre-registering the variable for. 'header' refers to the specific template file within the whole template theme package.

Thanks again for your help, Tim.

-Brent