Jump to content

$user variable only available on template file scope ?


adrianmak
 Share

Recommended Posts

I have a language switcher function put in a function.php, and which is include in $config->prependTemplateFile

function langbar() {
	$out = "<div class='langbar'>";
	$currentLanguage = $user->language;

	foreach($languages as $language) {
          if(!$page->viewable($language)) continue;
	  $user->language = $language; 
	  if($language->id == $currentLanguage->id)
              $active = "class='active'";
          else 
            $active = "";

          $out .= "<a href='$page->url'>$language->title</a>";        
	}
	$user->language = $currentLanguage;
	$out .= "</div>";
   	return  $out;
}

Then, in the output main.php

<?php echo langbar(); ?>

Nothing Outpputed except the opening and closing div

I tried to echo $user->language; in the main.php and it has the value of current user language.

Does it mean that, I could not have to use a function (to make the template file code more clean) to display a language switcher in template

Link to comment
Share on other sites

The api variables are only available in the template scope. They aren't global in any way. As soon as you're creating a function you create a new scope, therefore the variable is not accessible anymore. That's the reason for the above posted ways to access the api.

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...