Jump to content

How to manage template files inside a module


enricob
 Share

Recommended Posts

Hi all,

I am quite new to PW, and I am starting to create my first modules.

In a site I am working on, I have already created some templates, fields and pages to manage a simple event management, with event list page, event detail page, list of categories, and so on.

Now I would like to put all this inside a module, so when I start a new site all I have to do is install the module and I am ready to work with the events.

I already understood how to create the module and do the installation (and uninstallation of course) of templates, fields and pages, but now I am wondering what's the best way to manage the template files (.php) that the module pages will use.

I noticed others have chosen to copy the needed templates files inside the "site/templates" directory during the installation of the module...

Do you think this is the best approach? Are there any alternatives?

I am not fully convinced since this way I am adding new files to "site/templates" folder and since this folder could already contain a lot of files the situation could become a litte messy (since I have a mix of files I created and files that a module created).

If this is the right way to do it, I am wondering if in PW there is a way to use subfolders inside the "site/templates" folder, I am thinking for example to have a subfolder named as the module name, so all templates .php files related to that module could stay here, in a more organized way.

Thank you for a feedback on this!

enrico

Link to comment
Share on other sites

Hi Enrico,

Welcome to PW and the forums...

As for template files, the have to go inside site/templates but remember you can use includes in your template files to grab other files in other folders or sub-folders...

Have a read here:

https://processwire.com/talk/topic/740-a-different-way-of-using-templates-delegate-approach/

http://processwire.com/api/templates/

http://processwire.com/docs/tutorials/how-to-structure-your-template-files/

  • Like 2
Link to comment
Share on other sites

While I'm still new to the module development, I would remind you about the great api of ProcessWire. So maybe it would be better to think about a great set of helperfunctions, which reside in your module and let the template files just use those functions. Maybe take a look at the form api of processwire.

Link to comment
Share on other sites

Thanks all for the answers!

@nico: I didn't kwnow about this module, it seems great! I think it can be very very useful.

@kongondo: I know I can use subfolders, and I am already using the files in "templates" root as a sort of controllers and the files under "views" as...well, views :D

But still, if I am not missing something here, you need to have a file named as the template, in the "templates" folder, so here I am starting to have a lot of files.

For now I am prefixing the template names with a word indentifying the use of the template (for example "event_detail" or "event_list"), this way it seems to me more organized.

Anyway this is one of the very few (and really, not so important) complaint I have with PW, which by the way it's really a great tool!

Link to comment
Share on other sites

How many templates you have? I think maximum amount of template files I have had is around 20.

Its possible to use same template file for multiple templates - see the alternative template file setting.

  • Like 1
Link to comment
Share on other sites

In a site I am working on, I have already created some templates, fields and pages to manage a simple event management, with event list page, event detail page, list of categories, and so on.

Now I would like to put all this inside a module, so when I start a new site all I have to do is install the module and I am ready to work with the events.

Saving a site profile like Nico mentioned is a great option if you want exactly the same starting point for each site. If you want some more flexibility, there are a couple of other options. One is the new field migration tools built into the dev version of PW. There are template migration tools coming shortly. See here:

https://processwire.com/talk/topic/2117-continuous-integration-of-field-and-template-changes/page-6#entry68899

The other possibility is Migrator (https://github.com/adrianbj/ProcessMigrator), which can migrate a page tree and its fields and templates (and template files). With this option you can install the default PW profile and then add in additional components as required. If you think the components are generic enough that others might find them useful, you can even contribute them to https://github.com/adrianbj/ProcessWirePageLists/ and they will be available as an automatic install directly from Migrator.

  • Like 3
Link to comment
Share on other sites

.......But still, if I am not missing something here, you need to have a file named as the template, in the "templates" folder, so here I am starting to have a lot of files.

Yes, you are missing something cool  :-)....You do not need to have a template file named 'abc.php' to use with a template called 'abc'. You can tell PW that you will be using an alternative name for you template file. Have a look at the links I provided. For example, you can have one template file called 'main.php' for use with your template 'events'. Then, you can create a sub-folder called events, i.e. /site/templates/events in which you can place your related template files, e.g. 'details.inc' (or even details.tpl), 'list.inc'...etc...Hope I got you correctly..I have a feeling you already know all these and I am misunderstanding you...slow Friday here! :D

  • Like 4
Link to comment
Share on other sites

Thank you @adrian I will take a look at Field migration and Migrator, it seems interesting stuff!

@kongondo: you're approach of having a main .php file and other related files under a subfolder is a good one, it can be good to group together templates files that have similar functions, thank you very much!

Link to comment
Share on other sites

.....@kongondo: you're approach of having a main .php file and other related files under a subfolder is a good one, it can be good to group together templates files that have similar functions, thank you very much!

Only thing to add here is that it is only one of the various approaches possible. Work with what makes sense most to you and what you are currently developing :-)

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

×
×
  • Create New...