ventojs / vento

🌬 A template engine for Deno & Node
https://vento.js.org/
MIT License
212 stars 12 forks source link

Feature request: filter blocks #14

Closed pluiedev closed 1 year ago

pluiedev commented 1 year ago

Currently, there's no real easy way to apply a filter to a text block in Vento, which makes things like using Markdown for long body text quite painful. The best solution I've thought of so far is to assign text to a variable first and then printing it with filters:

{{ set text }}
    Something something main text, [some link](https://vento.js.org),
    some pictures ![some alt text](https://some.site/img/271a237b.png)
{{ /set }}
{{ text |> md }}

But this gets annoying if you need to do this often, like I do. It would be nicer if there's a tag that directly applies some text to some filters and then prints them, with filter chaining of course:

{{ filter strip_indent |> md }}
    Something something main text, [some link](https://vento.js.org),
    some pictures ![some alt text](https://some.site/img/271a237b.png)
{{ /filter }}

There are some drawbacks to this approach - after all, Vento is supposed to be a lot simpler than similar template engines, so a new tag isn't always the brightest idea.

Maybe we should also allow functions that are supposed to take text blocks to have a similarly simple syntax? This is also something you have to do at the moment, which resembles this problem a lot:

{{ function func(content, opts) }}
    {{# some templated stuff using `content` #}}
{{ /function }}

{{ set content }}
    Foo bar baz
    Quux lorem ipsum
{{ /set }}
{{ func(content, { a: 1, b: 2 }) }}

I'm not exactly sure what's the best approach here, but it's something I run into so often that I wrote my own helper tag to make my life easier. Hope we can have something like that upstreamed.

oscarotero commented 1 year ago

mm, ok, it makes sense. Maybe a echo (or print) tag?

{{ echo |> strip_indent |> md }}
    Something something main text, [some link](https://vento.js.org),
    some pictures ![some alt text](https://some.site/img/271a237b.png)
{{ /echo }}
oscarotero commented 1 year ago

I'm thinking of maybe echo overlaps raw (https://vento.js.org/syntax/raw/)

So I propose to replace raw with echo (both works similar but echo would allow to use filters). Thus, we don't have to add another tag.

oscarotero commented 1 year ago

New version released (0.8.0). Now you can do:

{{ echo |> md }}
    Something something main text, [some link](https://vento.js.org),
    some pictures ![some alt text](https://some.site/img/271a237b.png)
{{ /echo }}