how to get a layered context.namespaces.values()?

25 views
Skip to first unread message

Jinghui Niu

unread,
Feb 21, 2017, 1:43:57 AM2/21/17
to Mako Templates for Python
I've learned that the returned value for context.namespaces.values() is a dictionary-like object, which doesn't remember to which layer of the inheritance chain itself is attached. Is there a way to group all those TemplateNamespaces into different layers of inheritance?

Here is an example:
basepage.mako ( namespace_attachedTo_basepage001.mako, namespace_attachedTo_basepage002.mako )
      |
      |
childpage.mako ( namespace_attachedTo_childpage.mako )

I would like to group the first two TemplateNamespaces into one layer, and the third TemplateNamespace into the second layer.


Is there a way to do this in Mako? Thanks.

mike bayer

unread,
Feb 21, 2017, 11:33:02 AM2/21/17
to mako-d...@googlegroups.com
It likely pulls all the namespaces in without recording this information
so this would not be possible.

But also, Mako's template structure is not organized for deep
introspection. This is not really something one should need if using
the template system normally. I doubt Mako is well organized for
"meta" templating as it seems you're trying to do here.



>
> --
> You received this message because you are subscribed to the Google
> Groups "Mako Templates for Python" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to mako-discuss...@googlegroups.com
> <mailto:mako-discuss...@googlegroups.com>.
> To post to this group, send email to mako-d...@googlegroups.com
> <mailto:mako-d...@googlegroups.com>.
> Visit this group at https://groups.google.com/group/mako-discuss.
> For more options, visit https://groups.google.com/d/optout.

Jinghui Niu

unread,
Feb 21, 2017, 1:34:14 PM2/21/17
to Mako Templates for Python
Thanks Mike! I'm not quite sure if I fully understand 'meta templating' so please allow me to explain. In my case I have each templates(including template namespaces) with their own set of .css and .js in the header. The order is important as switching around usually brings out unexpected effects. If a namespace is included in the child page, I want its accompanying .css and .js also coming after its father's. But now, all the namespace either come before of all after the inheritance chain's .css and .js, creating a uncontrollable sequence.
Is this what you meant 'meta templating'? Or it's something still achievable in Mako? Thanks.


On Tuesday, February 21, 2017 at 8:33:02 AM UTC-8, mike bayer wrote:


On 02/21/2017 01:43 AM, Jinghui Niu wrote:
> I've learned that the returned value for context.namespaces.values() is
> a dictionary-like object, which doesn't remember to which layer of the
> inheritance chain itself is attached. Is there a way to group all those
> TemplateNamespaces into different layers of inheritance?
>
> Here is an example:
> basepage.mako ( namespace_attachedTo_basepage001.mako,
> namespace_attachedTo_basepage002.mako )
>       |
>       |
> childpage.mako ( namespace_attachedTo_childpage.mako )
>
> I would like to group the first two TemplateNamespaces into one layer,
> and the third TemplateNamespace into the second layer.
>
>
> Is there a way to do this in Mako? Thanks.

It likely pulls all the namespaces in without recording this information
so this would not be possible.

But also, Mako's template structure is not organized for deep
introspection.   This is not really something one should need if using
the template system normally.   I doubt Mako is well organized for
"meta" templating as it seems you're trying to do here.



>
> --
> You received this message because you are subscribed to the Google
> Groups "Mako Templates for Python" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to mako-discuss...@googlegroups.com

mike bayer

unread,
Feb 21, 2017, 2:52:56 PM2/21/17
to mako-d...@googlegroups.com


meta templating means you are doing something like this:

% for ns in context.namespaces.values():
${ns.thing()}
% endfor

that is, the above example doesn't even know what namespaces are in use.
Mako was developed many years ago with simpler ideas in mind:

${namespace1.thing()}
${namespace2.thing()}


and is no longer actively developed to support "meta" features like
this. If you really need special levels of dynamism you might need to
use normal Python code in conjunction with attributes set up in the
templates (e.g. in the <%attr> section or similar).
> > an email to mako-discuss...@googlegroups.com <javascript:>
> > <mailto:mako-discuss...@googlegroups.com <javascript:>>.
> > To post to this group, send email to mako-d...@googlegroups.com
> <javascript:>
> > <mailto:mako-d...@googlegroups.com <javascript:>>.
> <https://groups.google.com/group/mako-discuss>.
> > For more options, visit https://groups.google.com/d/optout
> <https://groups.google.com/d/optout>.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Mako Templates for Python" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to mako-discuss...@googlegroups.com
> <mailto:mako-discuss...@googlegroups.com>.
Reply all
Reply to author
Forward
0 new messages