Include with context

Skip to first unread message

Andrew Pashkin

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

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.


Andrew Pashkin

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

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

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
The <%namespace> tag supports and import


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

Per Lundberg

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

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):

    style_struct = StyleStruct(name, is_inherited, gecko_name, additional_methods)
    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,

Jonathan Vanasco

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
0 new messages