Jump to content

How to hook into template rendering to pass it variables?


jordanlev
 Share

Recommended Posts

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

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

  • Like 1
Link to comment
Share on other sites

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

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>
  • Like 2
Link to comment
Share on other sites

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.

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