adrianmak Posted April 14, 2015 Posted April 14, 2015 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
nickie Posted April 14, 2015 Posted April 14, 2015 In functions, use wire('user')->languageor, in case of module files: $this->wire('user')->language 1
LostKobrakai Posted April 14, 2015 Posted April 14, 2015 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. 1
Soma Posted April 14, 2015 Posted April 14, 2015 Same issue as you got here https://processwire.com/talk/topic/5857-using-pages-in-custom-function/?fromsearch=1 and lostkobrakai explained there too ... 4
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now