This topic has been hidden because it was flagged for abuse.

Showing 1-16 of 16 messages
This message has been hidden because it was flagged for abuse.
Re: Where to put a custom Jinja2 filter? Ovidiu 11/8/11 1:39 AM
Hi Niklas,

Look at http://code.google.com/p/google-app-engine-samples/source/browse/trunk/python27/guestbook/guestbook.py

You can put that code below the jinja_environment definition and it should work.
Make sure you are using the same environment instance when you retrieve the templates.

Best,
Ovidiu
This message has been hidden because it was flagged for abuse.
Re: Where to put a custom Jinja2 filter? voscausa 11/9/11 2:18 AM
I had some problems with jinja filters and jinja includes. That did non work. 
Now I use template inheritance. Here is my code for initializing the environment. I use a global env.
And in the mainline I use : 
my_env = myEnv.reg_cms_function()  # init or get the global env and register the jinja functions

class myEnv:                                                # jinja environment global
    
    _my_env = None
    _flush_env = 'NO'
        
    @staticmethod
    def get(flush='NO'):
        if  myEnv._my_env == None or flush == 'YES' or myEnv._flush_env == 'YES' :
            if flush == 'YES' :
                myEnv._my_env = Environment(auto_reload=True, cache_size=0, line_statement_prefix='#', line_comment_prefix='##')
                logging.debug('create jinja environment, flush : %s' %(flush))
                myEnv._flush_env = 'YES'
            else :
                myEnv._my_env = Environment(auto_reload=False, cache_size=50, line_statement_prefix='#', line_comment_prefix='##')
                logging.debug('create jinja environment, flush : %s' %(flush))
                myEnv._flush_env = 'NO'
            
            myEnv._my_env.loader = ChoiceLoader([
                               FileSystemLoader(os.path.join(os.path.dirname(__file__), 'templates', 'NL-nld')),
                               FunctionLoader(load_Template)
                               ])
        return myEnv._my_env

    @staticmethod
    def reg_cms_functions(flush='NO'):

        env = myEnv.get(flush)
        env.globals.update(xlink=ExternalLink)
        env.globals.update(ilink=ImageLink)
        env.globals.update(classdiv=ClassDiv)
        env.filters['euros'] = Euros
        return env
This message has been hidden because it was flagged for abuse.
Re: Where to put a custom Jinja2 filter? solsTiCe d'Hiver 11/14/11 12:47 AM
You can find bits and pieces of documentation here and there, but a real example or documentation is needed.

Reference:
jinja2 factory to use custom filter
jinja2 from webapp2_extras
how to configure a custom template path for webapp2

Here is the missing example:

import webapp2
import re
from webapp2_extras import jinja2

def datetimeformat(value, format='H:M d/m/Y'):
    return value.strftime(re.sub('([aAbBcdfHIjmMpSUwWxXyYzZ%])', '%\\1', format))

def jinja2_factory(app):
    j = jinja2.Jinja2(app)
    j.environment.filters.update({
        'datetimeformat': datetimeformat,
        })
    return j

class RequestWrapper(webapp2.RequestHandler):
    @webapp2.cached_property
    def jinja2(self):
        return jinja2.get_jinja2(app=self.app, factory=jinja2_factory)

    def render_template(self, template_name, template_values):
        self.response.write(self.jinja2.render_template(template_name, **template_values))

class MainPage(RequestWrapper):
    def get(self):
         # your code here
         self.response.write(self.render_template('yourtemplate.html', {'value':'It works'})

curr_path = os.path.abspath(os.path.dirname(__file__))
config = {}
config['webapp2_extras.jinja2'] = {'template_path': os.path.join(curr_path, 'your', 'custom', 'path')}
app = webapp2.WSGIApplication(
        [ ('/index.html', MainPage), ],
   debug=DEBUG,
   config=config)

it's way more complicated than webapp and django template. Especially because jinja2 lacks many django filters.
I am still looking for a good way to do this for every script I have. I need to redefine all the custom filter in every script ???

Re: Where to put a custom Jinja2 filter? Kyle Finley 11/14/11 10:33 AM
solsTiCe d'Hiver,

You can also add filters through the config directly, if that helps:

import my_cust_filters

config = {
    'webapp2_extras.jinja2': {
        'template_path': os.path.join(curr_path, 'your', 'custom', 'path'),
        'filters': {
            'datetimeformat': datetimeformat,
            'custfilter1': my_cust_filters.custfilter1,
            'custfilter2': my_cust_filters.custfilter2,
        },
    },
}

application = webapp2.WSGIApplication(routes, debug=DEBUG, config=config)

- Kyle
Re: Where to put a custom Jinja2 filter? solsTiCe d'Hiver 11/15/11 3:43 AM
oh good ! thank you
Re: Where to put a custom Jinja2 filter? solsTiCe d'Hiver 11/15/11 6:02 AM
say, I want to use an "inline" Template to respond to a rpc/ajax query
so I previously use:
template.Template('{{ somedatetime|timesince }}', template.Context({'somedatetime', datetimevalue}))

I can now use jinja2.jinja2.Template with jinja2 imported from webapp2_extras but I don't get my custom filter defined in self.jinja2 or webapp2 app

and self.jinja2 does not have a Template class.

So ?


Re: Where to put a custom Jinja2 filter? Kyle Finley 11/15/11 10:44 AM
I'm not completely sure about "inline" templates. Maybe something like this would work:

class RequestWrapper(webapp2.RequestHandler):
    @webapp2.cached_property
    def jinja2(self):
        return jinja2.get_jinja2(app=self.app, factory=jinja2_factory)

    def render_string(self, template_string, template_values):
        self.response.write(self.jinja2.environment.from_string(template_string).render(**template_values))
        
class MainPage(RequestWrapper):
    def get(self):
         # your code here
         self.render_string('The date was {{ somedatetime|timesince }}', {'somedatetime': datetimevalue})


Here's the section of the source where get_template is called:

It seems like you should be able to call from_string similarly, but I haven't tested it.



Re: Where to put a custom Jinja2 filter? solsTiCe d'Hiver 11/15/11 12:38 PM
thank you
I shouls have found that. I was looking at environment and did not see the from_string method /o\
This message has been hidden because it was flagged for abuse.
Re: [appengine-python] Re: Where to put a custom Jinja2 filter? Kyle Finley 11/16/11 2:47 PM
Nick,

Once you get everything set up, I don't think that there's all that much boilerplate. The set up process can be a little confusing, however. Here's a sample application to get you started:


With this setup you would put your custom filters in filters.py. You then explicitly list the filters that you would like your templates to have access to. In the example our config is in main.py

import filters
config = {
    'webapp2_extras.jinja2': {
        'filters': {
            'timesince': filters.timesince,
            'datetimeformat': filters.datetimeformat,
        },
    },
}

We then use a BaseHandler to setup the bollerplate code for us. As long as our handlers extend the BaseHander all of the filters will be available to the template.

Here's an example that extends the BaseHander to render the pages_test_filter.html file:

Does that help clarify things?




--
You received this message because you are subscribed to the Google Groups "google-appengine-python" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-appengine-python/-/QmTWnWZsN7gJ.

To post to this group, send email to google-appe...@googlegroups.com.
To unsubscribe from this group, send email to google-appengine-python+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-appengine-python?hl=en.

This message has been hidden because it was flagged for abuse.
Re: [appengine-python] Re: Where to put a custom Jinja2 filter? Kyle Finley 11/17/11 10:54 AM
Nick, 

I agree it would be great to have a solution that made the transition easier. There's a project called coffin [ https://github.com/coffin/coffin ] that brings a lot of the feature of django templates to jinja2. It's designed to work with django, so django is very much integrated, but maybe there are elements that will help with your transition.

For timesince, localized, the tipfy wiki has a nice example:
You would just need to replace:
from tipfy.ext import i18n -> from webapp2_extras import i18n
and include babel [ http://babel.edgewall.org/ ].

I think once you start using jinja2 you'll really enjoy it. You'll find that some of the things that would have require a filter in django can now be done very easily. If you haven't already looked at the jinja2 site. Here's the list of built-in filters:

- Kyle



This message has been hidden because it was flagged for abuse.