jordanlev Posted October 16, 2014 Share Posted October 16, 2014 Hi, I want to "inject" certain variables into a certain template from a module. How exactly can I achieve this in my module code? (It's a Process module, if that makes any difference). Specifically, I need to be told by PW which template it is about to render, then depending on which template it is I want to be able to send some variables to that template (so in the template file itself, in addition to the usual `$page->whatever` fields, it would also have `$my_custom_var` etc. that was set from the module "hook"). Thanks! -Jordan Link to comment Share on other sites More sharing options...
kongondo Posted October 16, 2014 Share Posted October 16, 2014 Jordan, I am not sure whether this would help: https://processwire.com/talk/topic/4647-best-hook-for-replacing-page-rendering/?p=45968 but I am failing to understand how a Process Module (modules that run in the backend/admin) has anything to do with something that will be rendered in the front-end (basically a page's template)... 1 Link to comment Share on other sites More sharing options...
jordanlev Posted October 16, 2014 Author Share Posted October 16, 2014 Thanks for the link -- that does look like what I was asking for. But are you saying that a Process Module's init() function only gets run when a user is in the backend/admin of the site, and not when front-end pages are being viewed? If that's the case, then I guess there's nothing I can do about this. Or is there a different kind of module type I can use that *does* get run on every page view, even the front-end? Link to comment Share on other sites More sharing options...
Craig Posted October 16, 2014 Share Posted October 16, 2014 Probably the simplest way to do this is to create a hook method or property on pages - rather than "injecting" variables into templates. The Helloworld.module file is a basic example of how to do this - example3 and example4 functions, but here is how it might look (completely untested - guide only!)Module init: $this->addHookProperty('Page::hello_world', $this, 'helloWorld'); Module function: function helloWorld($event) { $page = $event->object; if ($page->template->name !== 'my_template') return; $event->return = "Hello, world. The ID of this current page is " . $page->id; } Template: <h1><?php echo $page->title ?></h1> <p><?php echo $page->hello_world ?></p> 2 Link to comment Share on other sites More sharing options...
adrian Posted October 16, 2014 Share Posted October 16, 2014 You can load a process module on the front end if you need to access a method or property from it. Usually Process modules are not autoloading because they are called from a page under setup or as a helper from another module. But if you have it already doing something in the admin, you could load it on the front-end: $modules->get("ModuleClassName"); and then call the required method/property. I would suggest limiting most parts of the module's functionality to just the admin template so when you load it on the front end, only the relevant method/property is instantiated and processed - the rest of the code should be excluded. It looks like Craig might have detailed what you need as far as setting things up. 1 Link to comment Share on other sites More sharing options...
kongondo Posted October 16, 2014 Share Posted October 16, 2014 @Jordan, Some useful links: https://github.com/ryancramerdesign/ProcessHello http://wiki.processwire.com/index.php/Module_Creation http://processwire.com/api/modules/ https://processwire.com/talk/topic/741-a-guideline-for-module-naming/?p=6267 https://processwire.com/talk/topic/2394-how-to-present-your-module/ https://processwire.com/talk/topic/1313-modules-process-and-the-difference/ 1 Link to comment Share on other sites More sharing options...
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