
john.durkin at gmail
Oct 7, 2008, 3:24 PM
Post #7 of 28
(7338 views)
Permalink
|
|
Re: Things you wish you'd done in your templates
[In reply to]
|
|
Maybe this is super-obvious - but it wasn't happening in the first bric system that I inherited... I like to use templates to produce files that I include on the page with php (like a header.inc file, for instance), so that when you need to change something sitewide you don't have to republish every single story... just republish the story that controls the include file. Of course sometimes a sitewide republish can't be helped... but when you can do so, using include files can save you a ton of time and headache. You can also publish a story that controls an include file with soap from the server's crontab, then you can create a frequently updated module that lives on the page that doesn't load your production webserver down with a db query every pageload. Instead, your template can query the db at publish time. This is good for things like 'most emailed stories!'... jd On Tue, Oct 7, 2008 at 3:02 PM, Phillip Smith <phillip [at] communitybandwidth > wrote: > > On 7-Oct-08, at 4:54 PM, Matt Rolf wrote: > > >> On Oct 7, 2008, at 4:47 PM, Schults, Chris wrote: >> >> After six-or-so Bricolage sites, I've come to love <%init> blocks more >>>> and more. If you use them to get most of the line-noisy perl out of >>>> >>> the >>> >>>> way and assign values to friendly-looking variables like $text and >>>> $sidebar, the resulting template is vastly easier to edit for >>>> aesthetics, because it still looks like HTML. >>>> >>>> Also, you can stick the <%init> block at the bottom, so what you see >>>> when you start editing looks more reasonably like the template's >>>> output. >>>> >>> >>> I second what Bret said. When I moved from working with Bricolage at >>> Grist to PCC Natural Markets, I learned about and started using <%init> >>> blocks, which made a big difference. >>> >>> And as Simon wrote, better use of utility templates. I wish I had spent >>> more time thinking about how I'd use them. >>> >> >> All these responses have been good! Using more <%init> blocks is not >> something I'd thought of. >> >> And utility templates = friend. >> > > > > Great question / thread! :-) > > I recently engaged in the exercise of standardizing my templates too, here > are the tricks I came up with: > > + Per the notes above, put the <%init> block immediately following the > HTML, and use the <%init> block to set as many useful, and easy-to-ready, > variables as possible. > > + I then use several <%method> blocks to provide some other useful magic > that is passed UP the chain into the autohandler, e.g.: > > <%method .head> > ... for stuff that I want to inject into the <head> tag, e.g.: special > javascript, etc. > </%method> > > <%method .body></%method> for attributes that I want to add to the <body> > tag, e.g.: > > <%method .body>onload="load()" onunload="GUnload()"</%method> for a page > that contains a Google map, etc. > > <%method.rss></%method> -- per David's examples of how to manipulate the > RSS feed link, etc. > > + Also, I usually just include a <%cleanup></%cleanup> block in my default > template to remind myself to do any necessary clean-up tasks, i.e., publish > another, etc. > > + And, finally, I also include a <%doc> section to provide any necessary > commentary on the functionality of that specific template > > I find that having these all there from the get-go (in a default template > that I start with) helps me remember to do all the necessary work that makes > passing off the project, or remembering the details myself later, much > easier. > > And a couple of CSS tricks that I rely on are putting an ID and classes on > the body tag, which allows styling at a section-by-section, or page-by-page > level. > > Brad Harder put together a helpful template (included below) that sets the > class of the body to be a category list, e.g.: <body class="parent child > child child">, which provides a great set of hooks for section and > sub-section specific styles. And I usually set the ID to be the story slug, > or story "ID" in Bricolage, which allows very fine-grained control (if > necessary), e.g.: > > body#ID h1 { style for h1 of a story with this body ID} or body.CLASS h1 > {style for h1 of stories in category X} > > I use it like so (from the autohandler): > > <%init> > my $cat_uri = $burner->get_cat->get_uri; > my $bodyclass = $m->comp("/util/mk_class.mc"); > $bodyclass = "front" if $cat_uri eq '/'; > </%init> > > And the body tag appears like: > > <body <& SELF:.body &> id="<% $story->get_slug || $bodyclass %>" class="<% > $bodyclass %>"> > > The template follows: > > <%doc> > > =head1 NAME > > mk_class.mc - Outputs a class tag based on the current directory tree. > > =head1 SYNOPSIS > > my $bodyclass = $m->comp("/util/mk_class.mc"); > > <% $bodyclass %> > > =head1 DESCRIPTION > > Outputs a list of path fragments based on the current directory path, e.g.: > /dir1/dir2/dir3/ would produce dir1 dir2 dir3. > > This can be used in the body tag to assist with stylesheet-based layout on > a per-section basis. > > =head1 AUTHOR > > Brad Harder <brad [at] methodlogic> > > =head1 COPYRIGHT AND LICENSE > > Copyright 2008 by Brad Harder and Method Digital Logic > > This library is free software; you can redistribute it and/or modify it > under > the terms of the GNU Lesser General Public License as published by the Free > Software Foundation, version 2.1 of the License. > > This library is distributed in the hope that it will be useful, but WITHOUT > ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or > FITNESS > FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for > more > details. > > You should have received a copy of the GNU Lesser General Public License > along > with this library (see the the license.txt file); if not, write to the Free > Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA > 02111-1307 > USA. > > =cut > > </%doc> > > <%perl> > my $cat = $burner->get_cat(); > > my $return_class_string=$cat->get_uri(); > $return_class_string=~tr/A-Z/a-z/; > $return_class_string=~s/\// /g; > $return_class_string=~s/^ *//; > $return_class_string=~s/ *$//; > > return($return_class_string); > </%perl> > > > Hope that helps, and hope someone is documenting all this on the Wiki! ;-) > > -- > Phillip Smith, > Simplifier of Technology > Community Bandwidth > http://www.communitybandwidth.ca > > > >
|