Jump to content

Adding new API variables for templates (or sharing code between Process module and frontend templates)


Recommended Posts

Hi,

I'm working on a calendar module for a site.

I have a Process module that provides an admin page, which using fullcalendar, wraps up the creating of event pages a bit so it's doable for the client.

Right now, I have the logic to provide a JSON feed of events in the Process module, and essentially the same logic in the templates to generate the event listing. I'm not interested in using fullcalender in the frontend for this site, so while the event are obviously the same, the Process module needs to generate a JSON event list, while the frontend template just needs to iterate the events and render them as it pleases.

I'd like to generalize my code a bit, so that both the Process module and the frontend template can use the same code to generate the events, then just output them differently. That includes stuff like expanding recurrences - which right now is pretty much duplicated.

I'm thinking it would be nice to have a $calendar variable in the templates, and I guess that would/could end up as a $this->wire('calendar') in the Process module, but I'm quite new to ProcessWire development (I've done a few sites where only clicking around in Admin and writing templates was necessary), so a few pointer on how to go about this and/or where to look would be greatly appreciated.

I hope it makes sense :-)

Thanks in advance

Link to comment
Share on other sites

Hi lindquist,

That's a good solution you've come up with.

If you want to provide an API variable for your object, here's an example, pretty easy like everything in ProcessWire:

// This must be inside an autoload module

public function ready()
{
  $this->wire('calendar', new Calendar());
}

// Or, if you're Calendar module is autoloaded, make the same instance available as API variable
public function ready()
{
  $this->wire('calendar', $this);
}
  • Like 4
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...