Account Options

  1. Sign in
The old Google Groups will be going away soon.
Switch to the new Google Groups.
Google Groups Home
« Groups Home
Message from discussion pyTenjin Feature Requests
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
 
Steve  
View profile  
 More options Jun 6 2009, 11:39 am
From: Steve <unetright.thebas...@xoxy.net>
Date: Sat, 6 Jun 2009 08:39:52 -0700 (PDT)
Local: Sat, Jun 6 2009 11:39 am
Subject: Re: pyTenjin Feature Requests
I forgot to mention two small modifications that I used:

> > How to create your own Template class and convert "@var"
> > into "_context.get('var', None)" automatically?
> >    def _expand(code):
> >        """expand '@var' into '_context.get("var", None)'"""
> >        return re.sub(r"@(\w+)", r"_context.get('\1', None)", code)

> >    class MyTemplate(tenjin.Template):

> >        def add_stmt(self, buf, code):
> >            tenjin.Template.add_stmt(self, buf, _expand(code))

> >        def add_expr(self, buf, code, flag_escape=None):
> >            tenjin.Template.add_expr(self, buf, _expand(code), flag_escape)
> >    <p>Hello #{@user or 'guest'}!<p>
> >    <?py if @message: ?>
> >    <p>#{@message}</p>

I actually used this instead:

_expand_match = re.compile(r'@(\w+)')
def _expand(code):
    """expand '@var' into '_context.get("var", '')'"""
    return _expand_match.sub(r"_context.get('\1', '')", code)

The first difference is that the match pattern is compiled once
instead of on every invocation.  The second difference is that when
the context key isn't found, I return the emptry string '' instead of
None.  This is a little less efficient for boolean evaluation but it
makes <p>#{@message}</p> work cleaner when message doesn't exist.

Thanks,
Steve


 
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.