Keeping your Drupal theme organized.
Tue, May 11, 2010 by Erico Nascimento
When working with large websites and web-applications the number of files on your theme folder can grow quickly, and if you're not organized it can get very messy . As a result you’ll find yourself spending a lot more time looking for bits of code every time you have to change or update something.
Here are some tips to help keep you organized.
File Structure:
On Drupal a couple of files have to be on your theme rood folder:
- - page.tpl.php
- - node.tpl.php
- - template.php
- - themename.info
- - comment.tpl.php
If your file doesn't have to be on the theme root folder, it should be organized with similar files on subfolders. Here is what we typically use:
- - css (folder)
- - js (folder)
- - flash (folder)
- - images (folder)
- - templates (folder)
- - views (folder)
Naming Conventions:
When naming your TPLs alway be very descriptive and try to group files that theme similar items together. For example, all forms should be form_nameoftheform.tpl.php and all blocks should be block_nameoftheblock.tpl.php
Similar convention can be used for images. icon_nameoftheicon.gif, background_areawhereyouuseit.jpg, header_nameoftheheader.png etc.
Note that I always use the underscore ( _ ) instead of a dash ( - ) on my filenames. Whichever you choose to use just keep it consistent.
CSS :
It is probably a good idea to have multiple style sheets divided according to each section of the site they belong. This will not only help keep you organized, but it will increase the speed on the page load. Eg: you don’t have to load the user profile css on every page if you wont be using it.
page.tpl :
If you have to add javascript or CSS to every page on your site, you should do it on the Info file
If you see yourself writing a long code to something specific like a navigation, or a footer, you should have that code on a separate template file.
These were some very simple things any themer, beginner or advanced, can do to help save a lot of time in the long run.
Do you have your own style of staying organized? Share some tips with us!
Have a happy theming!
Erico Nascimento
Erico Nascimento posted on May 12, 2010 5:17 pm
@ Drupal Theme Garden
The views folder is a personal preference. You could have your views template files with the other templates but I find it easier to have them on a separate folder so I know exactly where to go when I'm theming a view.
@peach - all drupal themes
thanks for the tip. I'll make the correction on the post.
Anja posted on May 12, 2010 11:57 am
For a big theme I usually have something like this:
theme
--css
--js (when needed)
--img
--tpl
----blocks
----cck (when needed)
----page (for all page.tpl.phps)
----node (for all node.tpl.php)
----views (for all views templates)
template.php
theme.info
If I only have one or two page and node templates I will throw all of it within the tpl folder. This can always be changed later, just clear your caches and you're done.
And since usually my themes are developed as subthemes of ninesixty and I use admin themes my theme folder looks like this:
sites/all/themes:
frontend (folder)
--ninesixty
--mytheme (subtheme of ninesixty)
backend (folder, example)
--tao
--rubik (subtheme of tao)
Drupal Theme Garden posted on May 12, 2010 10:13 am
Thanks for this post.
But I don't understand "views (folder)"? Can you be more descriptive, please?
Ivan Zugec posted on May 11, 2010 10:26 pm
Another thing I do is create a folder of the each views within the views directory.
If I have created a views called "blog" I create a folder called with in the views directory called blog and place the templates in that folder (views/blog).
A lot easier to find views templates that way.
Erico Nascimento posted on May 11, 2010 9:29 pm
@Helior Colorado
Thanks for that. Will try to implement the new organization and group my node and page tpls on my next project.
Helior Colorado posted on May 11, 2010 8:57 pm
Here's a helpful little tip, the only files that are required in the theme's root is the .info file and template.php. This is cool because you can put all your .tpl.php files in a templates/ directory and Drupal will still discover them.
Post new comment