Jump to content

File / Template structure


beto
 Share

Recommended Posts

I wonder how different people handles different "file structures" for templates.

I know that you can only put php code inside a template files, and i have thought that when you have to create a lot of unique "pages" with different php code it will take time to create a template file for each page.

Not only that, but eventually, if you create a lot of templates, the templates folder would get very crowded. I was looking for a better way to organize my php files and found this solution. Instead of creating a template for each unique page with custom php code, I just create a single template named "include". This template just has two fields: title and include_path.

The code of this template is just: <?php include $page->include_path; ?>

In the "include_path" field I set the relative path for including other php files, and this method gives me a more organized collection of php files nested in various folders.

For example, for a form called "reservation", I would just create a page with the template "include", and the field "include_path" set to "./php/forms/reservation.php".

An example of this file structure:

site/

--templates/

-----/styles

-----/scripts

-----/php

--------/forms

--------/processes

--------/catalogs

--------/galleries

-----head.inc

-----foot.inc

-----home.php

-----blog-post.php

-----include.php

Do anyone know if there is a better way to handle php files? o anybody would share their file structure approach?

  • Like 1
Link to comment
Share on other sites

Do anyone know if there is a better way to handle php files? o anybody would share their file structure approach?

I think you've got a good method there, but just be mindful of security. Any time you let paths and files be specified from the admin, you've really got to trust whoever will be populating those paths/files.

When it comes to template files, it's best if you keep all the shared stuff out of them and include that separately. For instance, the basic profile includes head.inc and foot.inc, which represent the markup common to all pages. But you can take that much further, delegating to separate includes for other elements when it makes sense (sidebar, navigation, etc.)

I also like an approach where you setup some common features in your template that you can turn on or off with checkboxes on the page. For instance, you might have a checkbox that turns on/off the sidebar or comments, etc. And the detection and display of those things would likely be in your shared include files like head.inc or foot.inc. So when it gets down to the actual template file, it doesn't have to focus on anything other than what's completely unique to that template. Even on very large sites, its rare that you should need a lot of templates if you are using them as types and controllers rather than using them to represent minor markup changes. But there are a whole lot of approaches that you can take, and what is best for one may not be for another.

  • Like 3
Link to comment
Share on other sites

Thanks Ryan. I am still a newbie to ProcessWire, and developing my first websites with it.

I would really like to take a look at the file structures from different developers.

Anyone would like to share file structures?

Link to comment
Share on other sites

You've already got my two common patterns: the basic profile and the blog profile. :) processwire.com and modules.processwire.com are both built in a manner very similar to the basic profile (seeing as they essentially are the basic profile). Though I did modify them a bit: rather than using separate head/foot includes, they include a "main.inc" which has the entire markup template in it. main.inc also includes sidebar.inc to cover the navigation and widgets that get displayed in the sidebar on any page.

  • 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

×
×
  • Create New...