4

I have a header template with PHP parsing on Input an the following code:

<?php 
   $this->EE->config->_global_vars['user_language'] = "nl";
?>

When I use {user_language} in my header template it works as expected and outputs "nl".

However, my header template is being embedded in my main template, and when I use {user_language} over there, it doesn't output anything.

I know it has something to do with the parsing order, but how do I fix it ?

3
  • There is no logic there to determine what language needs to be set, so why are you doing it in the template instead of somewhere in the configs? If there is more to it than just setting the user_language global variable, going into more detail will probably help you get more useful answers.
    – UltraBob
    Dec 7, 2012 at 0:41
  • @UltraBob off course there's some logic to determine the language that needs to be set, I just left it out in my example to keep it as comprehensible as possible.
    – janvl
    Dec 7, 2012 at 7:47
  • Your call, but I think not having it leads to fewer answers based on a clear understanding of what environment you need to set your global variable from. Based on what we knew I thought Isaac's answer was best, but based on the answer you accepted, it seems like you probably have a multilanguage site setup very similar to what I have.
    – UltraBob
    Dec 7, 2012 at 8:31

3 Answers 3

3

I agree that this is a parse order issue.

It would probably be better for you to set your global variable in the config.php. Not only will this ensure it's set before the templates are parsed, but it'll also prevent you from needing to use PHP in your template, which makes things a bit more secure and efficient.

If you're using the Focus Lab Master Config, there's already a section in config.master.php for global variables that you can just add this to.

Otherwise, it's as simple as adding $assign_to_config['global_vars']['user_language'] = 'nl'; to your config.php.

You can then use the variable directly in your templates as {user_language}

1
  • I moved the php to my index.php, right before I declare my global var. There was indeed no need to put this in my template.
    – janvl
    Dec 7, 2012 at 8:28
5

You should do this in an Extension instead - an extension on the sessions_end hook (which is called near the beginning of every request to the site, just after the Session library has finished loading). You could then create new Global Variables before any template parsing begins, and which would then be available to all of your templates.

Extensions are very simple to write - just a single PHP file. You can use the awesome pkg.io tool to generate a skeleton for you, just select the sessions_end hook, and it will generate a class that you can fill in with your custom logic.

1

If it is static for all users, I like the idea of setting it in the config.php file.

If you are setting it differently depending on the user, I highly recommend just creating your own plugin or extension to accomplish the task. It is more efficient, and avoids many (not all) parsing order issues. Once you've done one or two, it's almost as easy as what you are already doing, and you can leave PHP processing off in your templates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.