From: John Joyce Date: 2007-03-23T20:08:03+09:00 Subject: Re: Newbie templating question: how to combine multiple files > > Looking at the documentation for the ERB class, I can't find a simple > way of including another template inside a template file. I guess I > could simply do a require, but that will simply copy the entire > contents > of the file as ruby code. How do you usually subdivide your templates > into multiple files (e.g. header, variable content, footer, etc.) > using > ERB? Frank, this is exactly how templates are done. It's kind of breaking a page into separate files for commonly repeated elements, making them easier to change. Regardless of the language you use to do it, it's the same; you make a skeleton like this (this is pseudo code) general, this is all or almost every page on the site: 1.variables defining body id and page title, etc... to be substituted into all included files! 2.include (header) 3.content actually in here or include it from a file 4.include (footer) header: doctype and head section, up to opening tag of body element. usually include(navigation list) at the end of this file content: this one is obvious. footer: perhaps include(navigation list) again and style it differently here closing body element tag closing html element tag this is also the section to place javascript usually (makes pages seem like they load faster, browser reads javascript after rather than before content loads. depends on what the js does, but especially the place for site analysis scripts) navigation list: this is your main list of site navigation links. That's it basically. It can be more complex and sophisticated, like Rails or WordPress. For simple sites, templates are an easier way to maintain much of it. For bigger sites with more complex systems behind them, you find that templates are still there! So, one web page should consist of at least 4 files, or more. More means more complication of logic. The only parts that would be different for any page are the content. As you can start to see for bigger sites, content and title and link variables start to be obvious targets for a database, if the site is big enough. Don't bother with a database unless you know you have a lot of stuff for a site. Using a format like this you can later transfer it to a more complex system pretty easily if you need to.