Include with context

243 views
Skip to first unread message

Andrew Pashkin

unread,
Apr 2, 2016, 10:55:01 AM4/2/16
to Mako Templates for Python
Hello, everybody!

I have such usecase - in one file I have some variable definitions in Python blocks, which are required by other templates. When I use include tag, it only includes def's and block's, but doesn't includes variables. In Jinja2 there is a such functionality as "import with context", which makes possible to import templates with their variables into a current context. Is there a similar thing in Mako?

Jonathan Vanasco

unread,
Apr 2, 2016, 2:18:18 PM4/2/16
to Mako Templates for Python
the percent blocks `<% foo = 1 %>` are evaluated at runtime

the percent-exclamation blocks <%! foo = 1 %>` are evaluated at compile time and the variables become part of the namespace's `attr` attribute.

see http://docs.makotemplates.org/en/latest/namespaces.html#version-one-use-namespace-attr


Andrew Pashkin

unread,
Apr 3, 2016, 4:45:50 PM4/3/16
to Mako Templates for Python
Is it possible to import variables in a current namespace? As import="*" does?

Andrew Pashkin

unread,
Apr 3, 2016, 5:22:35 PM4/3/16
to Mako Templates for Python
On Saturday, April 2, 2016 at 9:18:18 PM UTC+3, Jonathan Vanasco wrote:
Another feature, that I'm seeking for is ability to make current context available for a template tat is being included. So if I have "includes.mako" and "main.mako" I want to make context of "main.mako" available for "includes.mako" and then make resulting context of "includes.mako" be available to "main.mako". Jinja2 makes it possible with import with context functionaliy. Is it possible? 

Jonathan Vanasco

unread,
Apr 5, 2016, 1:16:11 PM4/5/16
to Mako Templates for Python
Read the docs on Synatx, Namespace and Inheritance.

The <%include> tag supports arguments  http://docs.makotemplates.org/en/latest/syntax.html?highlight=include#include
The <%namespace> tag supports and import http://docs.makotemplates.org/en/latest/syntax.html?highlight=include#namespace

Namespace: http://docs.makotemplates.org/en/latest/namespaces.html
Inheritance: http://docs.makotemplates.org/en/latest/inheritance.html

IIRC, either the Defs&Blocks or Runtime doc sections go into some interplay of argument imports

Per Lundberg

unread,
Apr 16, 2016, 3:50:13 AM4/16/16
to Mako Templates for Python
Hi,

I've been trying to do something similar to what Andrew was asking for:

<%!

def new_style_struct(name, is_inherited, gecko_name=None, additional_methods=None):
    global THIS_STYLE_STRUCT

    style_struct = StyleStruct(name, is_inherited, gecko_name, additional_methods)
    STYLE_STRUCTS.append(style_struct)
    THIS_STYLE_STRUCT = style_struct
    return ""


%>

<%include file="foo.mako" args="helpers=self" />

I have tried adding the new_style_struct to the list of args, but it just won't work. Also, calling it like ${helpers.new_style_struct} (when having a <%page> tag to import it) doesn't work either.

How can you call compile-time-defined methods from a template being included? (%def methods _work_, but I'd rather not have to refactor the whole thing just to be able to extract parts of an overly large template into smaller helper files.)

Many thanks in advance. I've read the documentation, but just haven't gotten it working. Have also tried exposing globals=globals() or locals=locals() but it didn't help much either.

Best regards,
Per

Jonathan Vanasco

unread,
Apr 23, 2016, 2:39:57 PM4/23/16
to Mako Templates for Python
If you can make a self-contained example (the .py + 2 .mako; attached or on github's gist) I can give it a shot.  i've abused mako in many ways and am somewhat familiar with the core.

to be honest though, mako is very limited in certain things when it comes to inheritance and includes.  I found one of the easiest methods to get around stuff like this has been using external scripts to merge the body of the includes into the template itself (and run apps using the merged directory).
Reply all
Reply to author
Forward
0 new messages