An idea - request for comments

Mark Hammond MHammond at skippinet.com.au
Wed Aug 4 01:13:53 CEST 1999


Ian Clarke wrote in message <37A6A2DB.9B11FB01 at strs.co.uk>...

>It is to create a Python web server which automatically parses embedded
>Python in HTML (there is a Perl extension that does this too).  So, for
>example, the following HTML code:

Attached is some code I wrote for exactly this purpose.  I looked into Zope
and it's DocTemplate class, but it had some restrictions that I didnt like
for my purpose.  I wanted something closer to what you describe.

I stole my implementation idea from Microsoft's ASP.  <%  and <%= tags
delimit Python code.  There is no requirement on the text outside the <%
tags, so HTML (or indeed many other things) work fine.

<%= indicates a Python expression to be evaluated and the result placed in
the output stream.
<% indicates a Python block to be executed.  A "write" function can be
called by this code to insert text into the output stream.

The application provides a dictionary to this tool - this dictionary is the
"namespace" made available to the executed code.  there is no support for
rexec, as I trust the users running this :-)

A special token <% #end %> is used to overcome Python's white-space in this
environment.  Indentation is automatically applied to subsequent code
blocks.

Example text may be:
<% for i in range(3): %>
<%= "This is loop number %d" % i %>
<% #end %>
<% import string # Keep newline in token so not in output!
%><% words = string.split("hi there") %>
<% for word in words: %><%= word %><% #end %>

Note that the second line of the example is not indented, but magically
appears in the for loop.  The <% #end %> token ends the block, meaning the
import is not indented.

Attached for your amusement.

Mark.



begin 666 doctemplate.py
M:6UP;W)T('-Y<RP@<W1R:6YG+"!T<F%C96)A8VL-"FEM<&]R="!R90T*#0IR
M94)L;V-K4W1A<G0@/2!R92YC;VUP:6QE*'(B+BHZ7',J*",N*BD_)"(I(",@
M36%T8V@@86YY=&AI;F<@96YD:6YG(&EN("(Z(BP at 97AC;'5D:6YG(&-O;6UE
M;G1S+ at T*9&5B=6=,979E;" ](# -"@T*9&5F($AA;F1L945X8V5P=&EO;B at I
M. at T*"70L('8L('1B(#T@<WES+F5X8U]I;F9O*"D-"@ER971U<FX@(B-%<G(@
M)7,Z("5S(B E("AT+"!V*0T*#0ID968 at 0VAU;FM&:6QE*&8I. at T*"6-H=6YK
M<R ](%M=#0H)9&%T82 ](&8N<F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'-E)RPG96QI9B==. at T*
M"0D)"0DC($ET(&ES(&$@8FQO8VL@<W1A<G0-"@D)"0D)8VAU;FMS+F%P<&5N
M9"AI;F1E;G0@*R!C;V1E("L@(EQN(BD-"@D)"0D):6YD96YT3&5V96P@/2!I
M;F1E;G1,979E;" K(#$-"@D)"0EE;'-E. at T*"0D)"0EI;F1E;G0@/2 B(" @
M("(@*B H:6YD96YT3&5V96PM,2D-"@D)"0D)8VAU;FMS+F%P<&5N9"AI;F1E
M;G0@*R!C;V1E("L@(EQN(BD-"@D)"65L<V4Z#0H)"0D):68@<W1R:6YG+FQS
M=')I<"AC;V1E*5LZ-%T]/2(C96YD(CH-"@D)"0D):6YD96YT3&5V96P@/2!I
M;F1E;G1,979E;" M(#$-"@D)"0D):6YD96YT(#T@(B @(" B("H@:6YD96YT
M3&5V96P-"@D)"0EC:'5N:W,N87!P96YD*&EN9&5N=" K(&-O9&4@*R B7&XB
M*0T*"0D)"0T*"0EL87-T4&]S(#T at 96YD4&]S*S(-"@D)<W1A<G10;W,@/2!S
M=')I;F<N9FEN9"AD871A+" B/"4B+"!L87-T4&]S*0T*"2, at 06YD(&5V97)Y
M=&AI;F<@=&\@=&AE(&5N9"!I<R!L:71E<F%L#0H):6YD96YT(#T@(B @(" B
M("H@:6YD96YT3&5V96P-"@EC:'5N:W,N87!P96YD*&EN9&5N=" K("=W<FET
M92 at E<RDC9V5N7&XG("4 at 8&1A=&%;;&%S=%!O<SI=8"D-"@ER971U<FX at 8VAU
M;FMS#0H-"F1E9B!?7V5V86PH(&5X<'(L(&<L(&PI. at T*"71R>3H-"@D)<F5T
M(#T at 979A;"AE>'!R+"!G+"!L*0T*"0ER971U<FX@<W1R*')E="D-"@EE>&-E
M<'0Z#0H)"70L('8L('1B(#T@<WES+F5X8U]I;F9O*"D-"@D)<')I;G0@(D5X
M<')E<W-I;VX@)R5S)R!F86EL961<;B E<SH@)7,B("4@*'-T<FEN9RYS=')I
M<"AE>'!R*2P@="P@=BD-"@D):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FEN=" B)3-D
M/B5S(B E("AL:6YE;F\L(&QI;F4I#0H)<')I;G0@(CTB("H at -3 -"@T*9&5F
M($5X96-U=&5496UP;&%T92@@9BP@;F%M97-P86-E+"!R97)A:7-A8FQE17AC
M97!T:6]N<R ](%M=("DZ#0H):68@;F]T(&YA;65S<&%C92YH87-?:V5Y*")?
M7V5V86PB*3H-"@D);F%M97-P86-E6R=?7V5V86PG72 ](%]?979A; T*"6-H
M=6YK<R ]($-H=6YK1FEL92AF*0T*"7-O=7)C92 ]('-T<FEN9RYR97!L86-E
M*'-T<FEN9RYJ;VEN*&-H=6YK<RP@(B(I+" B7')<;B(L(")<;B(I#0H):68@
M9&5B=6=,979E;#XQ. at T*"0E$=6UP0V]D92AS;W5R8V4I#0H)=')Y. at T*"0EC
M;V1E(#T at 8V]M<&EL92AS;W5R8V4L("(\<V-R:7!T/B(L(")E>&5C(BD-"@EE
M>&-E<'0 at 4WEN=&%X17)R;W(L(&1E=&%I;',Z#0H)"6UE<W-A9V4L("AF:6QE
M;F%M92P@;&EN96YO+"!O9F9S970L('1E>'0I(#T at 9&5T86EL<PT*"0E$=6UP
M0V]D92AS;W5R8V4L(&QI;F5N;RTU+"!L:6YE;F\K-2D-"@D)<')I;G0@(DQI
M;F4B+"!L:6YE;F\L(BP at 0V]L(BP@;V9F<V5T+" B.B(L(&UE<W-A9V4-"@D)
M<F5T=7)N#0H)=')Y. at T*"0EE>&5C(&-O9&4@:6X@;F%M97-P86-E#0H)97AC
M97!T. at T*"0EI9B!S>7,N97AC7W1Y<&4@:6X@<F5R86ES86)L945X8V5P=&EO
M;G,Z#0H)"0ER86ES90T*"0EI9B!D96)U9TQE=F5L. at T*"0D)=')A8V5B86-K
M+G!R:6YT7V5X8R at I#0H)"7!R:6YT(")%>&5C=71I;VX at 9F%I;&5D(@T*"0EP
M<FEN=" B)7,Z("5S(B E("AS>7,N97AC7VEN9F\H*5LP72P@<WES+F5X8U]I
M;F9O*"E;,5TI#0H-"@T*=&5S=" ]("(B(EP-"CPE(&9O<B!I(&EN(')A;F=E
M*#,I.B E/CPE<')I;G0@:24^/"4C96YD)3X-"DQI=&5R86P\)2!I;7!O<G0@
M<W1R:6YG( T*)3X\)3T at 9F]O( T*)3X\)2!W;W)D<R ]('-T<FEN9RYS<&QI
M="@B:&D@=&AE<F4B*2 -"B4^/"4 at 9F]R('=O<F0@:6X@=V]R9',Z("4^/"4]
M('=O<F0@#0HE/CPE("-E;F0@#0HE/DQA<W0 at 3&ET97)A; T*/"4@:68 at 9FQA
M9SH@)3Y)=',@=')U93PE(&5L<V4Z("4^271S(&9A;'-E/"4C96YD)3X-"B(B
M(@T*:68 at 7U]N86UE7U\]/2=?7VUA:6Y?7R<Z#0H)9&5B=6=,979E;#TR#0H)
M:6UP;W)T(&-3=')I;F=)3PT*"61I8W0@/2![?0T*"61I8W1;)V9O;R==(#T@
M)V9O;R!W87,@:&5R92<-"@ED:6-T6R=F;&%G)UT@/2 Q#0H)9&EC=%LG=W)I
M=&4G72 ]('-Y<RYS=&1O=70N=W)I=&4-"@E%>&5C=71E5&5M<&QA=&4H8U-T
><FEN9TE/+E-T<FEN9TE/*'1E<W0I+"!D:6-T*0T*
`
end





More information about the Python-list mailing list