1.1 (or 2.0) feature idea: import blocks
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 459
- Forks
- 90
- PR merge metrics
- No merged PRs in 30d
Description
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:
<%block import="namespace.function"></%block>
<%block file="file.mako" name="function"></%block>
<%block_import file="file.mako" name="function"></%block>
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.
- you can call a block like a def, but it then works like a def
- you can include a file to place a block, but it's not over-writeable .
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:
<%block name="header">library header</%block>
page.mako:
<%block name="header">page header</%block>
site-template.mako:
<%namespace name="t_library" file="library.mako"/>
<%block name="header">${t_library.header()}</%block>
that's a bit messy.
if the library.mako file looks like this:
<%block name="header">
library header
<%block name="sub_header">library subheader</%block>
</%block>
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
<%def name="_header()">
library header
</%def>
<%def name="_subheader()">
library subheader
</%def>
<%block name="header">
${_header()}
<%block name="sub_header">${_subheader()}</%block>
</%block>
site-template
<%block name="header">
${t_library._header()}
<%block name="sub_header">${t_library._subheader()}</%block>
</%block>
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)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reviewing Mako's existing block, def, namespace, and include behavior described here, then determine whether imported blocks should preserve override and nesting semantics. Done would require agreed syntax and semantics, implementation scope, and tests; this issue names no files or tests and has no recent activity.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- web-dev
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100