Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #11569

Re: Idea for pure-python templates using AST.

From Terry Reedy <tjreedy@udel.edu>
Subject Re: Idea for pure-python templates using AST.
Date 2011-08-16 11:47 -0400
References <2As2q.636$7r4.389@viwinnwfe02.internal.bigpond.com>
Newsgroups comp.lang.python
Message-ID <mailman.74.1313509709.27778.python-list@python.org> (permalink)

Show all headers | View raw


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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Idea for pure-python templates using AST. "Paul Wray" <paul.wray@det.nsw.edu.au> - 2011-08-16 21:33 +1000
  Re: Idea for pure-python templates using AST. aspineux <aspineux@gmail.com> - 2011-08-16 05:32 -0700
    Re: Idea for pure-python templates using AST. anand jeyahar <anand.ibmgsi@gmail.com> - 2011-08-16 21:46 +0530
    Re: Idea for pure-python templates using AST. Paul Wray <paulwray111111@gmail.com> - 2011-08-16 18:10 -0700
  Re: Idea for pure-python templates using AST. Terry Reedy <tjreedy@udel.edu> - 2011-08-16 11:47 -0400
  Re: Idea for pure-python templates using AST. Chris Angelico <rosuav@gmail.com> - 2011-08-16 17:14 +0100
    Re: Idea for pure-python templates using AST. Paul Wray <paulwray111111@gmail.com> - 2011-08-16 16:57 -0700
      Re: Idea for pure-python templates using AST. Chris Angelico <rosuav@gmail.com> - 2011-08-17 01:03 +0100
  Re: Idea for pure-python templates using AST. Irmen de Jong <irmen@-NOSPAM-xs4all.nl> - 2011-08-16 21:23 +0200
    Re: Idea for pure-python templates using AST. Paul Wray <paulwray111111@gmail.com> - 2011-08-16 18:02 -0700
  Re: Idea for pure-python templates using AST. Tim Roberts <timr@probo.com> - 2011-08-16 17:39 -0700

csiph-web