Thanks Armin.
> env.globals['m'] = sys.modules['application.models']
Flexibility and sandbox seems to be trade-off.
So, I'll design the module carefully.
Best regards,
Junya
On 9ζ12ζ₯, εεΎ11:14, Armin Ronacher <[EMAIL PROTECTED]> wrote:
> Hi,
>
> Junya Hayashi wrote:
> > But I couldn't find the good strategy of doing like this with Jinja1
> > or Jinja2.
> > I know that I can define "globals" for passing arbitrary python
> > objects to templates,
> > but once I put some objects on globals, it will be loaded everywhere.
>
> There are many ways to do that sort of thing, and Jinja2 doesn't really
> recomment one in the documentation. The easiest way would be putting the
> module of your models into the globals:
>
> env.globals['m'] = sys.modules['application.models']
>
> Assuming your application is already fully imported if you render a
> template, your macro can do something like this::
>
> {% macro recent_news(num) %}
> {% set news = m.NewsModel.select(...).limit(num) %}
> ...
> {% endmacro %}
>
> And then like you did with mako::
>
> {% import 'news/widget.html' as news %}
> ${news.recent_news(5)}
>
> Regards,
> Armin
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"pocoo-libs" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/pocoo-libs?hl=en
-~----------~----~----~----~------~----~------~--~---