Jump to content

Making an external class available to templates


Pete
 Share

Recommended Posts

I need to make an external class available to all templates.

It's configurable, and rather than have to edit the config file I'd rather make it available by using a configurable module.

I know I can bake objects and variables available for us in templates simply by using $page->somename = $object_or_variable by hooking it in befor page render, however I'd like to create several functions in the module that are then available on every template on the site.

I've been looking at the FormTemplateProcessor module for inspiration, as this seems to do pretty much what I'm after, but I'd like to have it automatically available rather than having to fetch it at the top of the header template like this:

$form = $modules->get('FormTemplateProcessor');

I know it's only one line of code, but since I would like the functions to be available site-wide (front-end and admin), I'd prefer a way of making it available automatically.

Any suggestions? Rather than me posting examples (because I don't have any yet) let's just pretend I want to automatically load the FormTemplateProcessor module and have its functions available :)

Link to comment
Share on other sites

I'm being an idiot. Fetching the module as per the code example above is the way to go.

Not sure why I had a sudden allergic reaction to having to have one line in the template, wereas I would have to have lines of code elsewhere in the template to output the results of the functions anyway.

I'm not even sure there is a better way to include an external class and access its functions than this, especially since I want to create some custom functions that aren't available as part of the class itself.

Link to comment
Share on other sites

Pete, if I'm understanding correctly, I think you can just make the module an 'autoload' module. Then ProcessWire will include it automatically at runtime. If you want to bake in some functions or instantiate some objects, just do it outside of your module's class, but in the same file (or have your autoload module include the file that they are in, in it's init() method). If you want to add a new API variable that will be sent to all the templates, you can do this in your .module file, outside of the class:

$myvar = 'string, object, or whatever you want';
Wire::setFuel('myvar', $myvar);

Then all your templates will have that $myvar variable and your modules will be able to access it from $this->myvar or wire('myvar');

Link to comment
Share on other sites

Thanks, I'll bear that in mind. It was to do with the IPB Forum module I sent over to you so I found a good enough solution in the end by adding it to the $page object :)

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