Jump to content

$session and $input variables in templates in v3++


joshuag
 Share

Recommended Posts

How can I use $session and $input or regular $_POST, $_COOKIE, $_SESSION, etc. in templates now that the template files are compiled? 

Or a better question would be: How are people managing needs like this for sites that use the file complier? 

I have tried turning off file compiling but one of the down sides is there is a cascading effect of errors because I have to go back and replace everything to use the ProcessWire namespace, including custom modules we have built. I know that I can add the name space to the start of files, but this doesn't always seem to work as expected.

Banged my head against the wall for a good while until I figured out what had happened :P

I am concerned that a huge majority of websites might not be able to be upgraded. 

Any suggestions would really help.

Thanks in advance,

- Joshua 

Link to comment
Share on other sites

There's no difference in how you would use $session, $input, or any API variable or any PHP superglobal ($_GET, $_POST, etc). These have nothing to do with namespace. 

If you must disable the template compiler, edit your /site/config.php and add $config->templateCompile = false; to it. That will prevent any of your template files from getting compiled. 

As for compiling modules: At this time I wouldn't recommend disabling the module compiler, otherwise almost no 3rd party modules will work. That's because there are few if any PW3-specific modules at present. If you come across a module that isn't working after being compiled, let me know about it so I can investigate it. 

When it comes to template files (and you've disabled the template compiler): you don't need to add a namespace to all of them. You only need to add it to those that are calling ProcessWire specific functions like the wire() function, or those that are referring to ProcessWire constants or classes directly. Things like this:

$foo = new PageArray();

$date = wireDate('Ymd');

$page->addStatus(Page::statusUnpublished); 

The parts in bold above are those that are referring to things in the ProcessWire namespace. Adding "namespace ProcessWire;" to the top of the file makes it simple, that's all you'd have to do. If you didn't want to do that, you could also just update a wire() function call to be like this:

$date = \ProcessWire\wireDate('Ymd'); 

Or you could just enable the template compiler, which will do this for you. 

However, maybe you aren't using things like the wireDate() or wire() functions and maybe you aren't referring to things like Page::statusUnpublished. If that's the case, you can skip adding a namespace at all, and likewise don't need the compiler. ProcessWire's API variables like $pages and $input and $session and all of that are going to be present either way. These have nothing to do with namespace. 

  • Like 4
Link to comment
Share on other sites

How can I use $session and $input or regular $_POST, $_COOKIE, $_SESSION, etc. in templates now that the template files are compiled? 

It sounds like you are assuming that the file compiler works like a cache converting the values of these variables into hard values at the time of compilation?

As LK and ryan mention, this is not the case - the file compiler is really just for dealing with Namespace issues  (although it can be used for other things like tags compiling). The key thing is that the resulting file is still all php with variables intact.

  • Like 2
Link to comment
Share on other sites

Thank you very much for the replies. 

I must have some problems with my current install(s) of PW or something causing a conflict. I cannot get $session, $input, or the PHP $_SESSSION, $COOKIE etc. variables to work at all if I have the file compiler turned on as is. I am going to start a new PW3 install and do some testing, to see if I get different results. 

I am relieved to hear that it's suppose to work like normal. 

I have 2 websites that I had upgraded and that is how I ran into this. Will report back with anything I find. 

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