Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Get expressions from template
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  4 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
slide  
View profile  
 More options Sep 12 2012, 5:20 am
From: slide <slide.o....@gmail.com>
Date: Wed, 12 Sep 2012 02:20:46 -0700 (PDT)
Local: Wed, Sep 12 2012 5:20 am
Subject: Get expressions from template

I am using Mako inside of an application to provide templated generation of
XML files. I would like to retrieve the expression values from a given
template to allow users to override the value before it is rendered.

For instance if I had the following

<Test>
    <foo name="Something" value="${SOMEVAR}"/>
    <foo name="SomethingElse" value="${OTHERVAR}"/>
</Test>

I would like to retrieve SOMEVAR and OTHERVAR. I noticed they are replaced
by something like this in the templte

SOMEVAR = context.get('SOMEVAR', UNDEFINED)

so it seems like there might be a way to hook into the parsing stage?

Thanks,

slide


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Michael Bayer  
View profile  
 More options Sep 12 2012, 10:13 pm
From: Michael Bayer <mike...@zzzcomputing.com>
Date: Wed, 12 Sep 2012 22:13:49 -0400
Local: Wed, Sep 12 2012 10:13 pm
Subject: Re: [Mako] Get expressions from template

On Sep 12, 2012, at 5:20 AM, slide wrote:

> I am using Mako inside of an application to provide templated generation of XML files. I would like to retrieve the expression values from a given template to allow users to override the value before it is rendered.

> For instance if I had the following

> <Test>
>     <foo name="Something" value="${SOMEVAR}"/>
>     <foo name="SomethingElse" value="${OTHERVAR}"/>
> </Test>

> I would like to retrieve SOMEVAR and OTHERVAR. I noticed they are replaced by something like this in the templte

> SOMEVAR = context.get('SOMEVAR', UNDEFINED)

> so it seems like there might be a way to hook into the parsing stage?

its a little odd to tell exactly who/when knows what in this scenario.   It seems like there is some concept of templates coming from somewhere with variables declared in them, and some other code needs to call upon these templates, and needs to "know" what variables will be called.

That is, i need something to happen in response to a variable being present in a template.

Usually, when I have to do this kind of thing it's with Python string templates, but same idea, I use an object with __getattr__ or __getitem__ on it.

That is, template:

variable one: ${namespace.FOO}
variable two: ${namespace.BAR}

usage:

class MyNamespace(object):
    def __getattr__(self, key):
        print "namespace attribute %s called" % key
        return "HELLO!"

template.render(namespace=MyNamespace())

This is kind of an entirely different approach to what you're looking for, but is very simple and performant.

Otherwise, you truly need to know the names before anything is rendered, yes you'd have to dig into Lexer, lex the template, get the parsetree back, walk through it and look for undeclared_identifiers() stuck on nodes.   Take a look at lexer.py and parsetree.py to see how that works, a quick lex looks like:

from mako.lexer import Lexer
parsetree = Lexer("some template").parse()

def find_undeclared(node):
    stack = [node]
    while stack:
        node = stack.pop(0)
        stack.extend(node.get_children())
        if hasattr(node, "undeclared_identifiers"):
            for ident in node.undeclared_identifiers():
                yield ident


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
slide  
View profile  
 More options Sep 12 2012, 11:31 pm
From: slide <slide.o....@gmail.com>
Date: Wed, 12 Sep 2012 20:31:55 -0700 (PDT)
Local: Wed, Sep 12 2012 11:31 pm
Subject: Re: [Mako] Get expressions from template

That doesn't sound too bad, I appreciate the pointers and will give it the
old college try as they say.

slide


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
slide  
View profile  
 More options Sep 14 2012, 2:54 am
From: slide <slide.o....@gmail.com>
Date: Thu, 13 Sep 2012 23:54:18 -0700 (PDT)
Local: Fri, Sep 14 2012 2:54 am
Subject: Re: [Mako] Get expressions from template

FYI, your solution worked perfectly. I was able to get the info I needed
from the template with the code above. Thanks again for the help, its very
much appreciated!

slide


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »