From: Wai-Sun Chia Date: 2002-08-07T12:51:43+09:00 Subject: PATCH: PageTemplate [%include file %] tag This is a multi-part message in MIME format. --------------050701090007080600000006 Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Hello, Thanks to Alex Fenton whom has given me a hint of how to add INCLUDE tag for PageTemplate. Here's a patch for the original PageTemplate.rb. But there's a kludge; I can't seem to do a File.new as Ruby says it's a tainted operation; so I just do a .untaint on the argument...not very nice, unless someone can try to unkludge it... Perhaps Brian Wisti can add an [%include %] in the next PT release... ;-) *hint* *hint* Wai-Sun Chia wrote: > Thanks for the hint. I'll try it out right away.. > > f.lex wrote: > >>Just started out with PageTemplate 0.3.2 with mod_ruby 0.9.9 and ruby > >>1.7.2 and have some questions regarding PT.. > >> > >>Does PT support nested templates? i.e. templates within a template? > >>I was expecting PT to have an "include" kind of directive?? > > > > > > Hi > > > > No, the package doesn't have built-in support for 'include' type > directives. > > However, you can add this capability into the package by first > creating a > > custom glossary for your parser that will recognise 'include' tags. > There's > > a class called Syntax::Glossary in the package that shows how these > work. > > Then you'll need to create a subclass of the Syntax::Parser that > reads in > > file where it finds an 'include' tag. The 'compile' method of the parser > > handles the meaning of each individual type of tag, and you'll want > to alter > > it so that it can read in extra template files when it finds an > 'include'. > > Finally, you'll need to create a subclass of PageTemplate that uses the > > altered Syntax::Parser. > > > > The code I've got for this at the moment is slightly fiddly and > longwinded, > > but I've sent it over off-list in just in case it gives you a few ideas. > > > > best > > alex > > > > __ > > alex fenton > > http://www.pressure.to/cccp/ --still anxious-- > > > > > > > > > > > -- Wai-Sun "Squidster" Chia Consulting & Integration Linux/Unix/Web Developer Dude --------------050701090007080600000006 Content-Type: text/plain; name="pt-include.diff" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="pt-include.diff" --- PageTemplate.rb.orig Tue Aug 6 20:58:46 2002 +++ PageTemplate.rb Wed Aug 7 11:38:30 2002 @@ -778,7 +778,8 @@ style. 'ifbranch' => /^\s*else\s*$/, 'loopopen' => /^\s*in (\w+)\s*$/, 'loopclose' => /^\s*endin\s*$/, - 'loopbranch' => /^\s*no\s*$/ + 'loopbranch' => /^\s*no\s*$/, + 'include' => /^\s*include ([\w.\/\\]+)\s*$/ ) ######################################################################## @@ -951,6 +952,8 @@ file which contains the original ((|sour if directive == "value" command = ValueCommand.new(value) + elsif directive == "include" + command = includeFile(filename, value) elsif directive =~ /^(\w+?)open$/ mainBlock = nil branchBlock = nil @@ -1018,6 +1021,17 @@ file which contains the original ((|sour # Return compiled BlockCommand return commands + end + + private + def includeFile(oldFile, incFile) + oldFile.reverse =~ /^[^\\\/]*(.*)/ + newFile = $1.reverse << incFile + file = File.open(newFile.untaint) # sq: $SAFE=1 -> $SAFE=0 + source = file.readlines + parsed = parse(source, newFile) + commands = compile(parsed, newFile) + return commands end private --------------050701090007080600000006--