-
-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1.1 (or 2.0) feature idea: import blocks #256
Comments
Changes by jvanasco (@jvanasco):
|
1 similar comment
Changes by jvanasco (@jvanasco):
|
Michael Bayer (@zzzeek) wrote: when i first made the <%include> tag I considered the option to have a version that's a so-called "server side" include; instead of invoking the related template at runtime, it would pull it in at compile time. the concepts are straight from JSP which you can see here: http://stackoverflow.com/a/14763794/34549. is this essentially a server side include? |
jvanasco (@jvanasco) wrote: Yes. Their server-side-include variation of a "compile-time" inclusion is essentially what I'm talking about (the format The main goal is to be able define blocks in a namespace file, and import them as-is. Consider a namespace file that defines this:
The current Mako implementation doesn't let you recycle that into another template as inhertitable blocks. So my goal is that writing something like this:
Would import the code at compile-time from the namespace, and be equivalent to writing:
The workaround right now, is to make the header components separate, and then declare them on the parent files:
|
Michael Bayer (@zzzeek) wrote: all the mechanics of namespaces are fully at runtime. getting a namespace to mean something at compile time would be an entirely new set of features. I'd rather name it something else. but also I'm not really sold on server side includes anyway since Python itself has no similar concept, and neither do any modern template languages I'm familiar with. You don't see them used in modern JSP either. |
jvanasco (@jvanasco) wrote: "namespace" is just used as an example. The proposal that i've been digging through the internals to possibly implement isn't directly for global "compile time" SSIs. (i'm just going to use the phrase I am only focused on being able to import a fully defined inheritance block from another file as-is. This only relates to compile-time is because of Mako's implementation details on how it handles the <%block> template tags. In other templating systems, this could (potentially) be handled at runtime. I roughly solved my problem with a preprocessor that parses a custom tag. It's not an ideal solution because the preprocessor isn't aware of the TemplateLookup, and I had to kludge that in. I think it could be a valuable feature to future versions if TemplateLookup supported a preprocessor variant that can pass the TemplateLookup object. That could allow processors to be more aware of the Mako environment. If you think it's useful, I'd be willing to do a PR for something like this: This looks to be related to the compile time concept (though it may be entirely different); doing some testing I encountered this implementation detail: Given:
A child can overwrite However, a child can not declare a blocks with addressable blocks:
The header_sub block won't render in child.mako, or anything that inherits it. We have a lot of C > inherits > B > inherits A. Somehow we never encountered this before. |
Michael Bayer (@zzzeek) wrote:
OK this is how this works. You can make that header_sub render if you just did this:
which is like yuk, but there's a reason, which is because the blocks were meant to be cascaded like this:
where above, you get:
I'm not sure how to have it both ways unless we added more flags and switches to |
jvanasco (@jvanasco) wrote:
Wow. That IS weird, but I understand why it's necessary now. In any event, it works - so no complaints here. If I could figure out a way to describe this for the docs, I'd submit a PR. One day... A (better) way to write that and preserve the whitespace would be...
The call to |
Migrated issue, originally created by jvanasco (@jvanasco)
I don't know if I'd be able to pull this off, but wanted to share publicly for feedback.
I propose a new feature to "import" a block.
It might be invoked like:
Invoking this would (re)declare the block as a local function, generating the same code as if you copy/pasted the block between source files.
why?
if you do a large project with template inheritance, you inevitably end up having a lot of blocks that you'd like to re-use across components.
the implementation details for blocks/defs make it hard to recreate this behavior.
an example would be setting up a "header" on a site template. if you wanted to split out the "header" into it's own page, the current way would be...
library.mako:
page.mako:
site-template.mako:
that's a bit messy.
if the library.mako file looks like this:
then site-template would need to define a header block with a sub_header block for an override to happen from "page" -- but then to get nesting right one would need to do....
library
site-template
this gets increasingly complex.
so my proposal would be to allow for a
block
that is defined in one namespace, to be "imported" into a new namespace (vs being called as a regular def)The text was updated successfully, but these errors were encountered: