The theme system in WordPress is one of its great strengths and something that is loved by most WP users. It’s a very good thing. However, different developers like setting up code in different ways.
While the theme system works well as advertised, it doesn’t work exactly the way I like to set up my web front-end code. Luckily, it is so flexible that I can easily implement my preferred system.
The standard theme set-up has a header file, footer file, sidebar file and index, single and page files that include the header, footer and sidebar. This is all well and good, but if you make a change to your overall layout, you often have to make it in 3 places (the index, single and page files).
I prefer a template approach, where I have a master template that has a few
I’ve set up my master template, with the holes and display logic I need to keep the site consistent and only have to make changes in one place. This contains the code that would normally be in the header file, the footer file an some of the index, page, single and sidebar files.
In the index, page, single and sidebar files, I buffer the output into variables instead of spitting the content out directly. Then at the end of each template file, I include my master template, which places each variable nicely into it’s home in the template and outputs the page.
This allows the template system to follow the theme convention of having different pages, while allowing me to control the master site code in a single place.
If you’re tired of editing several files when you want to make a change that (for example) affects both your “page” templage and your “single” template, you may want to consider this approach.
Excelent idea! Can you post or offer an example file for us to see how it is done? Thank you.
I may have misread, but why not just have an index.php and not use get_header or get_footer? You could then use condititionals (or the standard functions like get_sidebar) to bring in different parts of the page.
I’m not 100% sure I follow your comment either, but the ability to choose a different template on a per-page basis is still something I need.
Many theme authors use a “loop.php” filled with if statements to do something similar to what you do, Alex. I prefer the flexability of multiple files: single.php, index.php, home.php, page.php, etc.
Exactly, this gives me that benefit.
Is this like Smarty? The individual files deal with only retrieval of data. The data is actually displayed in the master template.
good idea (:
I also would appreciate seeing an example, a set of your main files as described with their holes, to fully appreciate your approach.
[…] Nachdem bei Joomla vor einiger Zeit die Diskussion losgegangen ist, wie Content und PrÃ¤sentation am besten voneinander getrennt werden kÃ¶nnen, und wie ein universelles, einfach handzuhabendes System aufgebaut werden muss, startet diese Diskussion jetzt auch bei WordPress. […]
1. lot of people prefer that index, page and single file looks different. So its not everytime necessary to put the same change in index, single and page.
2. if you like to have it in one file you can make only index and with if-statements etc. you can realize the output for single site, page or main site …
3. please not smarty! i saw smarty in action in xtcommerce, joomla and in serendipity. And smarty is for me pain in the neck. Comparing to this: the themesystem of WP is great and easy to understand … above all for people they are not programmers.
Sorry for my bad english and i hope you can understand me.
Totally agree with you, as “Ideologist” said, can you show us an example please.
While the system currently in use is a boon to those who know WP very well, it’s a huge mess to those of us who are less code-savvy. Just about every template is so unique that it makes it hard to transfer your own design ideas or changes from one to the other.
Me? I’m not a fan of the system. It’s too complex for most users to truly understand.
It’s an interesting approach to controlling content placement. I’ll keep this in mind while I’m redesigning my site — who knows, could make my life a whole lot easier.
Thanks for the insight.
I have been working out this same approach on my site recently. I want to minimize the number of templates I use, maximize the number of includes but still stay as flexible as possible.
As Randy mentioned above, I’ve seperated out the loop into an include file. Which gives me the ability to use different loops not just entire templates.
What I’m dealing with right now is setting up my templates so I can call specific categories and specific pages, as well as specific formatting by using logic. My goal is to have 1-2 main templates (index.php/page.php) take care of most of the site.
This is my index.php page right now,
Much cleaner than what I used to have before I removed the loop.
Oops, I guess you can’t use carat – code, here is a link. http://www.aaronspor[...]242006_a.txt
[…] Alex King’s “WP Themes vs. Templates” discusses the modular system of how WordPress Themes work with some good tips on creating a template form to work within your WordPress Theme. While the theme system works well as advertised, it doesn’t work exactly the way I like to set up my web front-end code. Luckily, it is so flexible that I can easily implement my preferred system. […]
Joomla does not use smarty. it uses pattemplate.
It is somewhat like Alex envisions. One singe file defines the look for the entire site. Much easier to edit than multiple pages in a wordpress setup. But to be fair, i havent found wordpress templates to be that bad either. smarty on the other hand makes me want to cry
i agree with you alex.
i’m not a good programmer, so…i’m not really understand with your tips on how you do it.
however, its open my eyes..because editing 3 files for any wp themes is too much confusing to me!!..hahaha