Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!feeder.erje.net!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'context': 0.04; 'subject:using': 0.04; 'python:': 0.05; 'received:verizon.net': 0.07; 'statements,': 0.07; 'terry': 0.07; 'transform': 0.07; 'python': 0.08; 'expressions.': 0.09; 'preparation,': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:80.91.229.12': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'received:lo.gmane.org': 0.09; 'template.': 0.09; 'subject:python': 0.11; 'am,': 0.12; 'def': 0.15; "'a'": 0.16; 'approach,': 0.16; 'comparison.': 0.16; 'idea:': 0.16; 'ive': 0.16; 'reedy': 0.16; 'renders': 0.16; 'templating': 0.16; 'wouldnt': 0.16; 'looked': 0.16; 'syntax': 0.16; 'wrote:': 0.16; 'template': 0.19; 'jan': 0.19; 'appropriate': 0.20; 'posting': 0.20; 'header:In-Reply-To:1': 0.22; '(or': 0.23; 'calls.': 0.23; 'literal': 0.23; 'code': 0.25; 'statement': 0.25; 'tests': 0.25; 'code.': 0.26; "i'm": 0.27; 'function': 0.27; 'paul': 0.28; 'classes': 0.28; 'separate': 0.28; 'effect': 0.28; 'ignored.': 0.29; 'environment.': 0.30; 'bare': 0.30; 'filters,': 0.30; 'parent': 0.30; 'recursively': 0.30; 'class': 0.30; 'values': 0.32; 'expression': 0.32; 'pure': 0.32; 'usually': 0.32; 'there': 0.33; 'to:addr:python-list': 0.33; 'header:User-Agent:1': 0.34; 'idea': 0.34; 'improvements': 0.34; 'header:X-Complaints-To:1': 0.35; 'using': 0.37; 'but': 0.37; 'think': 0.38; 'received:org': 0.38; 'allows': 0.38; 'url:org': 0.38; 'subject:: ': 0.39; 'feed': 0.39; 'header:Mime-Version:1': 0.39; 'to:addr:python.org': 0.39; 'case': 0.39; 'thinking': 0.40; 'more': 0.60; 'below': 0.62; 'manager,': 0.62; 'here': 0.65; 'plus': 0.65; 'share': 0.65; 'kept': 0.68; 'legal': 0.70; 'concept': 0.72; 'bear': 0.80; 'balanced': 0.84; 'blow': 0.84; 'face,': 0.84; 'subject:Idea': 0.84; 'render': 0.91; 'templates,': 0.91 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Terry Reedy Subject: Re: Idea for pure-python templates using AST. Date: Tue, 16 Aug 2011 11:47:58 -0400 References: <2As2q.636$7r4.389@viwinnwfe02.internal.bigpond.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: pool-74-109-121-73.phlapa.fios.verizon.net User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 In-Reply-To: <2As2q.636$7r4.389@viwinnwfe02.internal.bigpond.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 64 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1313509709 news.xs4all.nl 23972 [2001:888:2000:d::a6]:36677 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:11569 On 8/16/2011 7:33 AM, Paul Wray wrote: > Hello all > > Ive had what I think is a great idea for pure-python templates (I can > almost hear the groans, bear with me...) > > For the impatient, proof of concept is at http://pastie.org/2379978 > demonstrating simple substitution, balanced tags using context manager, > subtemplates, and template inheritance. > > I'm posting here to get opinions on: > * the merits of the idea, (or otherwise!) > * whether there are any established/mature templating systems that use > this approach, or whether its come up before, > * ideas for improvements and advice on other aspects such as sandboxing > * of course, to share the idea in case others want to use it > > Background: Ive been working on an application that recursively renders > html/xml documents from heterogenoeus trees, with separate classes for > each document component. First I built my own renderer but was > dissatisfied with the repetitive code. Then looked at Mako and Jinja, > and used Jinja but was still disatisfied, because I still needed a > render method in each class to do preparation, and also the template > which was centrally managed by the Jinja loader and environment. I found > a way to call templates recursively via Jinja filters, but was not sure > that it wouldnt blow up in my face, so I also had separate calls to > render the children of each node, and then feed the value to the parent > template. I kept thinking that there must be a way to get the brevity > and clarity of declarative templates, plus the simplicity of pure python > loops, tests and function calls. > > The idea: > Python syntax allows a statement to be a bare literal or identifier. > These have no effect on the program. More generally, Python has expression statements, with the result of the expression ignored. These are usually function calls with side-effects. "print('x')" has no effect on the program and the return value is usually ignored. > So the function below is legal python: > > def myFunc(): > 'a' > x = 45 > 'b'; 'c'; x > > So is this (within the appropriate class context of course): > > def body(self, r): > '

'; self.heading; '

' > '
    ' > for itm in self.items: > '
  • '; itm; '
  • ' > '
' > > The idea is simply to use python ASTs to transform this code so that it > accumulates the values of the bare expressions. Interesting idea, though I have no experience for comparison. -- Terry Jan Reedy