Comments (3)
@SergioBenitez @andrelmartins if you are interested.
I'll try to implement that soon since I think it's the last big feature missing but I'm moving continents next week so will be a bit busy for a while.
from tera.
Here's an alternative syntax suggestion.
Import
An alternative to allowing per macro renames, would be to require namespace naming. I think this aligns better with the per-file namespacing and avoids having to rename a lot of macros, at the expense of a more verbose call site (see below). Of course, it would be an error to have more than one import with the same namespace name in the same template file.
Short form (naming required):
{% import "macros.html" as macros %}
Long form:
{% import "macros.html" as mymacros : macro1, macro2 %}
Or, alternatively, using the "from" syntax:
{% from "macros.html" as mymacros import * %}
{% from "macros.html" as mymacros import macro1, macro2 %}
Macro use
At the call site, it would be required to prefix the macro name with the namespace:
{{ mymacros::macro1(arg1, arg2) }}
Alternatives
A slight variant would be to allow one to omit the namespace naming and then macros from that import would fall into a "root" namespace and not require the namespace prefix for macro use. That complicates the code a bit more and re-introduces a more limited version of macro name collision. I would lean towards requiring namespace naming.
Extra features
A more pie in the sky feature would be to allow registering Rust functions as macros. Something like this:
let mymacros = MacroSet::new();
mymacros.add(args1, func1);
mymacros.add(args2, func2);
tera.import_macros(mymacros);
where "args1" would be a list of context names and the add function would be something like:
pub fn add<F>(&mut self, args: Vec<String>, usermacro: F)
where F: Send + Sync + 'static + Fn(&Context) -> TeraResult<String>
from tera.
I like the short form, not so much the others (if i import something by name, I don't want to have to prefix it). Only having the short form should solve most (all?) of the naming collisions.
I'm curious if @mitsuhiko has some insight on that or some things he would like to change from Jinja2
from tera.
Related Issues (20)
- i want to add errors / success flash messages globals HOT 3
- How to specify the number of cycles? HOT 2
- `if` statement with parens fails to parse HOT 3
- How to split a string into a loopable array HOT 1
- error locations and IDE diagnostics support HOT 4
- Allow scoping context in block HOT 9
- Round filter does not return int with precision = 0
- Allow only pulling in specific builtins HOT 4
- [bug] for loop `else` doesn't strip whitespaces HOT 6
- jinja2 lstrip_blocks behavior HOT 1
- [FEATURE REQUEST] Support comparison operators for strings HOT 3
- Is there a function similar to minijinja's render_block? HOT 3
- Escaping is not applied at the expected level HOT 5
- Ability to build an object or hashmap? HOT 8
- error while importing HOT 2
- error while importing HOT 1
- Filter sections but set the result in a variable HOT 1
- Excessive memory usage of Tera HOT 3
- linebreaksbr is not safe - add `html_encode` filter? HOT 1
- `default` filter: make the `value` optionnal and add `d` alias HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from tera.