Giter Site home page Giter Site logo

Macros about tera HOT 3 CLOSED

keats avatar keats commented on July 23, 2024
Macros

from tera.

Comments (3)

Keats avatar Keats commented on July 23, 2024

@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.

andrelmartins avatar andrelmartins commented on July 23, 2024

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.

Keats avatar Keats commented on July 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.