Jump to content

Passing variables from module to template


lehtu
 Share

Recommended Posts

Hi,

I have been struggling with this one. I'm building module that hooks at before rendering template. In my module I have set the $user variable which I want to use in my template files. I don't want to touch on any of my template files. Only thing I need is to pass $user variable as is for my templates. Is this even possible? Tried some obvious ways, but haven't had any luck yet..

And using session is not an option in this case ;)

Thanks already!

Link to comment
Share on other sites

Welcome to the forums!
 
The $user variable is already taken in the globally accessible wire/fuel object, so don't use that.
 
from your module you can set the var as follows (in your before page render hook):

$this->wire->set('foo', 'bar');

in your template you can call it like any other wire/fuel variable (like $user, $page, $session etc), since the they are passed to templates automatically:

echo $foo; // 'bar'

More on THE fuel/wire: https://processwire.com/talk/topic/3876-using-fuel/

Soma:

You can use fuel but Ryan once explained it's deprecated and an old artifact using it.

  • Like 5
Link to comment
Share on other sites

Okay, thanks for the answer! This works just like you said and it's awesome! But.. I'm building this module as part of my API and that's why I want to use $user variable. I'm using processwire users ofc.

Any clues how to make this happen? Is it possible to override $user at this point?

Link to comment
Share on other sites

I don't know if it's possible (I doubt it), can't test right know, but don't do it! The user is a reserved object in PW.

Edit: you can overwrite the user, but PW throws and Exception if it's not an instance of the User class. So if you want to pass a custom user object, extend the class and add your custom stuff to it. Or extend the user class with hooks.

Marcrura

wire('user') only returns the user object so you cannot assign it this way, and it's the same as $this->wire('user') if you want to set it do it like described above, so: $this->wire->set('user', $userExtended);

Link to comment
Share on other sites

I don't know if it's possible (I doubt it), can't test right know, but don't do it! The user is a reserved object in PW.

Edit: you can overwrite the user, but PW throws and Exception if it's not an instance of the User class. So if you want to pass a custom user object, extend the class and add your custom stuff to it. Or extend the user class with hooks.

Marcrura

wire('user') only returns the user object so you cannot assign it this way, and it's the same as $this->wire('user') if you want to set it do it like described above, so: $this->wire->set('user', $userExtended);

I'm using User class, it's nothing fancy I'm just logging in user via POST variables. So there is no need to even modify basic User class. I have ProcessWire 2.5.3 and it doesn't pass $this->wire->set('user', $myUser); for my template. This works though: $this->wire->set('api_user', $myUser);

My first solution at this point was to set $this->wire->set('api_user', $myUser); and in _init.php I just simply assign $user = $api_user;

But in my case this is much cleaner solution is to forget the module and put whole code in _init.php.

Thanks for help!

  • 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...