Thank heavens that Andy put lots of snippets of code in his examples. Looking at other people's code is very much part of my learning process.
I'm new to tt2, and new to OO programming in general. There is a paradigm shift that I'm still wrapping my mind around. Questions: There is a ton of perl code out there to learn from. Sysadmins have littered the net with scripts. Web spinners have countless cgi scripts posted. Q1: Where do the templates hide? That is, where can I go to see entire small to medium sized projects, as opposed to snippets? (Ok, where can I get some one else's round rock to use as start to make my wheel?) Q2: In several places browsing the list archives, I see reference to separating the logic from the content. I've been able to do this somewhat. My content div is only content, with a call to header and footer. But my sidenav.inc file is an awful mix of template code with chunks of display. The net effect is cluttered. Does anyone have a pointer to how to make this separation cleaner? Q3. How do you make templates that are both easy to read and that create output that's easy to read. I've tried various combinations of the space slurping options, but I end up with too much or too little space, and awful formatting. I can see one way would be to build the entire page as a single string, do a clear, then output the string. This means that I have to track the formatting inside the template logic, which seems awkward. Alternately, I have to create a pretty printer for each style of file. Set your template to look pretty, then run the output through pretty. This is better, but doing the pp code could take longer than doing the template. Here's an example: The purpose of this code is to turn a directory structure into a menu. E.g. If I move a template file in the tt2 tree, then the resulting menu for the web site does the right thing. This way, the content directory is a bunch of static divs. Most of the appearance is controlled by the style sheet. This gives the same effect as the TT2 web site, where clicking on any menu item expands just that item, so that at any point you can see the siblings of all path elements between you and the top. Sort of a bread-slice effect instead of a bread crumb effect. The only caveats: For any directory there must be a file IN it with the same name Eg. Foo must contain Foo.tt2. If you want menu items in sequence other than alphabetical, you can prepend a digit_ to the front of the name so that alphabetical order is consistent with desired order. Since there is also a top level nav to get to this part of the tree, there is a reference to "Section" which is the current top level division of the site. You will also see chunks of debugging stuff. I wanted the output near the item it was working with. I'm sure there's a better way to do this, but I've not found it yet. It's a bad example. The code is clunky. It's ugly. It's mother dresses it funny. How should it be done? <div id="vnavbox"> [% USE String %] [% USE dir = Directory("tt2/" _ Section ) %] [% #The next two lines figure out how to make the links # to the right page as relative links. depth = template.name.split('/') ; lrtop = String.new('../').repeat(depth.size - 1) ; %] [% Level = 0 #Formatting can depend on level in the tree. %] [% INCLUDE subdir %] [% BLOCK subdir %] [% Level = Level + 1 %] [% PROCESS set_html_tags %] [% FOREACH entry = dir.list %] [%#<p>$Level $entry.name</p>a%] [% plainentry = String.new(entry.path).shift('tt2/') %] [% INCLUDE makelink %] [%# The logic in the next few lines determines whether we scan directories. In essence we want to see the siblings of every path element down to the present location. To do that we compare the path for the entry to the path of the template. The direntry starts at a different point, so we have to prune the leading tt2 off of it. %] [% IF entry.isdir %] [% IF template.name.match(plainentry) %] [% entry.scan %] [% INCLUDE subdir dir=entry %] [% END %] [% END %] [% END %] [% END %] [% BLOCK makelink %] [%# We generate both the link name and the link path from the directory path of the item we're looking at%] [% truepath = entry.path.replace('tt2/','') # Take tt2 off the front end of the path%] [% truepath = truepath.replace('.tt2','.html') # Change the .tt2 at the end to .html %] [% linkname = entry.name.replace('^[0-9_]*','') #Filenames may have a number_ prefix to force order %] [% linkname = linkname.replace('_',' ') #Make the link more readable %] [% linkname = linkname.replace('\.tt2$','') #Trim the .tt2 %] [% IF template.name == plainentry %] [% class = 'class="current"' #Set how the current page in the menu is highlighted %] [% ELSE %] [% class = '' %] [% END %] [%#<p>pe: $plainentry <BR> tn:$template.name<p>%] [% IF entry.isdir %] [% link = lrtop _ truepath _ '/' _ entry.name _ '.html' %] [%# <p>D: tp $truepath<br> en=$entry.name</p>%] [% ELSE %] [% link = lrtop _ truepath %] [%# <p>F:tp= $truepath </p>%] [%# We want the directory item itself to have a page, but we don't want to show that filename in the tree. So we abort creating a link in the menu for a file name that is the same as it's parent name. This why the IF section above duplicates the last path element with a .html for directories. %] [% plainfile = entry.name %] [% lastchunk = entry.path.split('/').last(2).first %] [%# <p>lc: $lastchunk en: <br>$entry.name </p> %] [% IF lastchunk == entry.name.replace('\.tt2','') %] [% RETURN %] [% END %] [% END %] [%# IF plainfile != lastdir; %] $OTag<a href=$link $class >$linkname </a>$CTag [%# END %] [% END %] [% BLOCK set_html_tags %] {%# This section assigns a different class to each menu level. Format using the style sheet. %] [% SWITCH Level %] [% CASE 0 %] [% OTag='<p class="L1">'; CTag='</p>'; %] [% CASE 1 %] [% OTag='<p class="L1">'; CTag='</p>'; %] [% CASE 2 %] [% OTag='<p class="L2">'; CTag='</p>'; %] [% CASE 3 %] [% OTag='<p class="L3">'; CTag='</p>'; %] [% CASE 4 %] [% OTag='<p class="L4">'; CTag='</p>'; %] [% CASE 5 %] [% OTag='<p class="L5">'; CTag='</p>'; %] [% CASE 6 %] [% OTag='<p class="L6">'; CTag='</p>'; %] [% END %] [% END %] </div> _______________________________________________ templates mailing list templates@template-toolkit.org http://mail.template-toolkit.org/mailman/listinfo/templates