mò ì½÷Ec @smdZdkZdkZdkZdefd„ƒYZdefd„ƒYZdefd„ƒYZeZ defd „ƒYZ d „Z d „Z d „Z ed joÂead„Zde fd„ƒYZde fd„ƒYZeeeddddddƒƒdƒde fd„ƒYZdefd„ƒYZeeeddddddƒƒd ƒd!e fd"„ƒYZeeed#d$ƒƒd%ƒeZyd&e fd'„ƒYZWnej o eZnXepd(GHeane d)„ƒZeed*ƒd+ƒe dd,„ƒZeed-ƒd.ƒe dd/„ƒZeedd0ƒd1d0ƒto d2GHqindS(3sA lightweight python templating engine. Templet version 2 beta. Supports two templating idioms: 1. template functions using @stringfunction and @unicodefunction 2. template classes inheriting from StringTemplate and UnicodeTemplate Each template function is marked with the attribute @stringfunction or @unicodefunction. Template functions will be rewritten to expand their document string as a template and return the string result. For example: @stringtemplate def myTemplate(animal, thing): "the $animal jumped over the $thing." print myTemplate('cow', 'moon') The template language understands the following forms: $myvar - inserts the value of the variable 'myvar' ${...} - evaluates the expression and inserts the result ${{...}} - executes enclosed code; use 'out.append(text)' to insert text $$ - an escape for a single $ $ (at the end of the line) - a line continuation Template functions are compiled into code that accumulates a list of strings in a local variable 'out', and then returns the concatenation of them. If you want do do complicated computation, you can append to 'out' directly inside a ${{...}} block. Another alternative is to use template classes. Each template class is a subclass of StringTemplate or UnicodeTemplate. Template classes should define a class attribute 'template' that contains the template code. Also, any class attribute ending with '_template' will be compiled into a template method. Use a template class by instantiating it with a dictionary or keyword arguments. Get the expansion by converting the instance to a string. For example: class MyTemplate(templet.Template): template = "the $animal jumped over the $thing." print MyTemplate(animal='cow', thing='moon') Within a template class, the template language is similar to a template function, but 'self.write' should be used to build the string inside ${{..}} blocks. Also, there is a shorthand for calling template methods: $ - shorthand for '${{self.sub_template(vars())}}' This idiom is helpful for decomposing a template and when subclassing. A longer example: import cgi class RecipeTemplate(templet.Template): template = r''' $dish $ $ ''' header_template = r'''

${cgi.escape(dish)}

''' body_template = r'''
    ${{ for item in ingredients: self.write('
  1. ', item, ' ') }}
''' This template can be expanded as follows: print RecipeTemplate(dish='burger', ingredients=['bun', 'beef', 'lettuce']) And it can be subclassed like this: class RecipeWithPriceTemplate(RecipeTemplate): header_template = "

${cgi.escape(dish)} - $$$price

" Templet is by David Bau and was inspired by Tomer Filiba's Templite class. For details, see http://davidbau.com/templet Templet is posted by David Bau under BSD-license terms. Copyright (c) 2007, David Bau All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of Templet nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. Nt_TemplateBuildercBsOtZeideieiBeiBƒZdd„Z dd„Z dd„Z RS(Ns\$( # Directives begin with a $ \$ | # $$ is an escape for $ [^\S\n]*\n | # $\n is a line continuation [_a-z][_a-z0-9]* | # $simple Python identifier \{(?!\{)[^\}]*\} | # ${...} expression to eval \{\{.*?\}\} | # ${{...}} multiline code to exec <[_a-z][_a-z0-9]*> | # $ method call )(?:(?:(?<=\}\})|(?<=>))[^\S\n]*\n)? # eat some trailing newlines cCs!||||_|_|_dS(N(tconstpattemitpattcallpattself(RRRR((t templet.pyt__init__†stcs®|iƒ}|o|diƒ o |d=ng}|D]4}|iƒo!|t|ƒt|iƒƒq;q;~}t|ƒo t |ƒ‰di ‡‡d†|DƒƒS(sERemoves any leading empty columns of spaces and an initial empty lineis c#s x|]}ˆ|ˆVqWdS(N(t[outmost-iterable]tltspacestmargin(RR (R R (RtsN( tstrt splitlinestlineststript_[1]R tlstriptlentlspacetminR tjoin(RR R RRR RR ((R R Rt __realign‰s  HcCs±g}x›t|ii|i|ƒƒƒD]x\}}|ddjo-|o"|i ||i t |ƒƒq q(| p |i dƒo$|idjotd|ƒ‚q(|idƒoq(q(|djo|i ||idƒq(|i dƒo$|i |i|dd !|ƒƒq(|i d ƒo#|i ||i|d d !ƒq(|i dƒo#|i ||i|d d !ƒq(|i ||i|ƒq(Wdi|ƒS( Niits %s: <%s %s>texecc sf|oG|ig}|iƒD]"}|d|jo ||qq~ƒn||d<ˆˆ|UdS(NiR( t_TemplateMetaClass__dicttkwtupdateRt iteritemsR!RRtglobals(RR6R7R!R(RR:(RtexpandªsG (tsystmodulestclsR-t__dict__R:R,tnR)R/t_TemplateMetaClass__builderR+R RR'R;(R>R R@RR:R)R;((RR:Rt __compile¥s !cGs…xn|iiƒD]]\}}|djp|idƒo4t|tƒo t|||i ||ƒƒqmqqWt i ||ŒdS(NR t _template( R>R?titemstattrtvalR*t isinstancet basestringtsetattrt_TemplateMetaClass__compilettypeRtargs(R>RLRERF((RR°s  ((R,R-RRARJR(((RR3¡s tStringTemplatecBs/tZdZeZd„Zd„Zd„ZRS(s)A base class for string template classes.cOsg|_|i||ŽdS(N(RtoutR RLR7(RRLR7((RR»s cGs5|iig}|D]}|t|ƒq~ƒdS(N(RRNtextendRRLtaR (RRLRRP((Rtwrite¿scCsdi|iƒS(NR(RRRN(R((Rt__str__Âs(R,R-t__doc__R3t __metaclass__RRQRR(((RRM·s   tUnicodeTemplatecBs8tZdZeZd„Zd„Zd„Zd„ZRS(s*A base class for unicode template classes.cOsg|_|i||ŽdS(N(RRNR RLR7(RRLR7((RRÌs cGs5|iig}|D]}|t|ƒq~ƒdS(N(RRNRORRLRPtunicode(RRLRRP((RRQÐscCsdi|iƒS(Nu(RRRN(R((Rt __unicode__ÓscCst|ƒidƒS(Nsutf-8(RVRtencode(R((RRRÖs( R,R-RSR3RTRRQRWRR(((RRUÈs    cBsçei|ƒ}ei|iih}}d|jod|i }nd|d|i f}d|i ei |Œfd|ed|d||fƒi|i|dƒd |g}ed i|ƒ|d ƒ}|||U||i S( NR4s<%s>s%s: <%s>s def %s%s:s %s = []s%s.append(%%s)s%s.append(%s(%%s))t s return "".join(%s)s R5(tinspectt getargspectfuncRLR<R=R-R?R:tlocalsR,R)t formatargspectlistnameRt stringtypeR+RSRR/R(R\R_R`RRLR)R:R]((Rt_templatefunctionÙs */ cCst|ddddƒS(s0Function attribute for string template functionsR_RNR`R N(RaR\(R\((RtstringfunctionéscCst|ddddƒS(s1Function attribute for unicode template functionsR_RNR`RVN(RaR\(R\((Rtunicodefunctioníst__main__cCs*||jodt|ƒGHtandS(Nserror - got: %s(texpectedtactualR%tFalsetok(RfRe((Rtexpectôs tTestAllcBstZdZdZRS(sA test of all the $ formssã Bought: $count ${name}s$ at $$$price. ${{ for i in xrange(count): self.write(TestCalls(vars()), "\n") # inherit all the local $vars }} Total: $$${"%.2f" % (count * price)} (R,R-RSR (((RRjùs t TestCallscBstZdZdZRS(sA recursive tests=$name$i ${*[TestCalls(name=name[0], i=n) for n in xrange(i)]}(R,R-RSR (((RRks tcountitnames template calltpricef1.23sÎBought: 5 template calls at $1.23. template call0 template call1 t0 template call2 t0 t1 t0 template call3 t0 t1 t0 t2 t0 t1 t0 template call4 t0 t1 t0 t2 t0 t1 t0 t3 t0 t1 t0 t2 t0 t1 t0 Total: $6.15 tTestBasecBstZdZRS(NsM $ $ (R,R-R (((RRost TestDerivedcBstZdZdZRS(Ns$names${TestAll(vars())}(R,R-t head_templatet body_template(((RRpsif2.8799999999999999sÊtemplate call Bought: 4 template calls at $2.88. template call0 template call1 t0 template call2 t0 t1 t0 template call3 t0 t1 t0 t2 t0 t1 t0 Total: $11.52 t TestUnicodecBstZdZRS(Nu Ï€ = $pi (R,R-R (((RRs#stpif3.1400000000000001u Ï€ = 3.14 t TestErrorcBstZdZRS(NsCost of an error: $0(R,R-R (((RRu,ssTestError failedcCsdS(s Hello $name.N((Rm((Rt testBasic3stHenrys Hello Henry.cCsdS(sD ${{ if count == 0: return '' }} $a${testReps(a, count - 1)}N((RPRl((RttestReps7stfootfoofoofoofoofoocCsdS(uD ${{ if not count: return '' }} ★${testUnicode(count - 1)}N((Rl((Rt testUnicode>si u★tOK( RSR<R.RZtobjectRRKR3RMtTemplateRURaRbRcR,tTrueRhRiRjRkR RoRpRsRVRgtgoterrorRuR(RvRxR{(RRcRaRbRjRiR~R3RMRxR.RZR{RuR<RUR€RoRsRpRkRv((Rt?wsR&      !!