Version 5 of tmpl_parser

Updated 2008-05-20 19:34:19 by escargo

Description

The engine works by converting a template (which is a string, or a file) into a Tcl script, and then running it. Each line of text encountered will be returned as is. Exception is text between <% ... %> which is treated as Tcl code (the eval happens in a regular interp).

Why not expand?

expand is a simple and snugly for Tcl language, be mounted in tcllib. You can use it as quick as flash.

But, that library has a problem, when to really use. We cannot write easily following signs

 [ ] { }

Why not TemplaTcl?

TemplaTcl is a powerful and cool template engine.

But now, we cannot use it on Tcl8.4 :(

tmpl_parser is

a simple and fast template engine. That still not have function to read a file, create a parser object.

But we can use it instant, only write a command require or source.

Usage

First, let's create a template file (templtest.tmpl):

 <table><%
   for {set i 0} {$i < 4} {incr i} { %>
   <tr>
     <td><%= $i %></td>
   </tr><% 
   } %>
 </table>

 <ul>
   <% foreach item $cityList {
     %><li><%= $item %>
   <% } %>
 </ul>

The above template (in HTML language) generates a 4-row table and an unordered list, taking values from a $cityList variable, that we're going to define. First let's load the code:

 source tmpl_parser.tcl

reading template file.

 set fd [open templtest.tmpl r]
 set tmpl [read $fd]
 close $fd

now create a parser instance:

 set parser [::tmpl_parser::tmpl_parser $tmpl]

set the variables used in our template:

 set cityList {Ragusa Ravenna Rieti Rimini Rome Rovigo}

and render the template:

 puts [eval $parser]

Here's the output that gets produced:

 <table>
   <tr>
     <td>1</td>
   </tr>
   <tr>
     <td>2</td>
   </tr>
   <tr>
     <td>3</td>
   </tr>
   <tr>
     <td>4</td>
   </tr>
 </table>

 <ul>
   <li>Ragusa
   <li>Ravenna
   <li>Rieti
   <li>Rimini
   <li>Rome
   <li>Rovigo
 </ul>

tmpl_parser code

 # tmpl_parser.tcl
 #
 #  Tcl embeddedd script parser(a template engine)
 #
 #  This module comverts Tcl embedded scripts into a Tcl normal script(parser),
 #  after you just have to do eval command for the generated parser.
 #
 # Copyright (c) 2007 by Kanryu KATO<[email protected]>
 # licensed on Tcl License.
 
 package require Tcl 8.3
 package provide tmpl_parser 0.1
 
 namespace eval ::tmpl_parser {
 	namespace export tmpl_parser
 }
 
 proc ::tmpl_parser::tmpl_parser {tmpl} {
 	# Tcl embedded tags
 	# [[outer <%...inner...%> outer]] <-$tmpl
 	#  [	=	 ]	   <-$token
 	#	cd ef	hi j	<-indexes
 	set parser { {set _o {}} }
 	while {[set i [string first %> $tmpl]] != -1} {
 		set h [expr $i-1]
 		set j [expr $i+2]
 		set token [string range $tmpl 0 $h]
 		set d [string first <% $token]
 		set c [expr $d-1]
 		set e [expr $d+2]
 		set f [expr $d+3]
 		
 		# outer
 		lappend parser [escaped_parse [string range $token 0 $c]]
 		switch [string index $token $e] {
 			"=" {
 				# normal expression (e.g.  Thanks <%=$count%> accesses!)
 				lappend parser [normal_parse [string range $token $f end]]
 			}
 			":" {
 				# numeric expression (e.g. <%:$i+2000%>)
 				lappend parser [numeric_parse [string range $token $f end]]
 			}
 			default {
 				# enbedded Tcl command is passed through listing
 				lappend parser [string range $token $e end]
 			}
 		}
 		# after "%>"
 		set tmpl [string range $tmpl $j end]
 	}
 	#last outer
 	lappend parser [escaped_parse $tmpl]
 	lappend parser {join $_o ""}
 	
 	return [join $parser "\n"]
 }
 
 proc ::tmpl_parser::escaped_parse {str} {
 	set str [string map {\" \\\" \{ \\\{ \} \\\} \\ \\\\} $str]
 	return "lappend _o \"$str\""
 }
 
 proc ::tmpl_parser::normal_parse {str} {
 	return "lappend _o $str"
 }
 
 proc ::tmpl_parser::numeric_parse {str} {
 	return "lappend _o [expr $str]"
 }