Message from discussion
Idea for pure-python templates using AST.
Path: g2news1.google.com!news4.google.com!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!194.109.133.85.MISMATCH!newsfeed.xs4all.nl!newsfeed6.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path: <python-python-l...@m.gmane.org>
X-Original-To: python-l...@python.org
Delivered-To: python-l...@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-l...@python.org
From: Terry Reedy <tjre...@udel.edu>
Subject: Re: Idea for pure-python templates using AST.
Date: Tue, 16 Aug 2011 11:47:58 -0400
References: <2As2q.636$7r4....@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....@viwinnwfe02.internal.bigpond.com>
X-BeenThere: python-l...@python.org
X-Mailman-Version: 2.1.12
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <http://mail.python.org/mailman/options/python-list>,
<mailto:python-list-requ...@python.org?subject=unsubscribe>
List-Archive: <http://mail.python.org/pipermail/python-list>
List-Post: <mailto:python-l...@python.org>
List-Help: <mailto:python-list-requ...@python.org?subject=help>
List-Subscribe: <http://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-requ...@python.org?subject=subscribe>
Newsgroups: comp.lang.python
Message-ID: <mailman.74.1313509709.27778.python-l...@python.org>
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: ab...@xs4all.nl
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):
> '<h1>'; self.heading; '</h1>'
> '<ul>'
> for itm in self.items:
> '<li>'; itm; '</li>'
> '</ul>'
>
> 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