stencilproject / Stencil

Stencil is a simple and powerful template language for Swift.
https://stencil.fuller.li
BSD 2-Clause "Simplified" License
2.34k stars 221 forks source link

Shorthand for `filter` tag #244

Open ilyapuchka opened 5 years ago

ilyapuchka commented 5 years ago

I found myself using filter indent quite often in one of the templates and it would be nice to be able to use

{% indent:4," ",true %}...{% endindent %}

instead of

{% filter indent:4," ",true %}...{% endfilter %}

This will not just make it shorter but will provide more context in the closing tag.

yonaskolb commented 5 years ago

Where do you usually use these indents? Around include blocks or in other places as well?

I only use them around include so would love to have a way of easily indenting the contents of those

ilyapuchka commented 5 years ago

@yonaskolb I'm not using includes in this particular template as it is relatively short (it generates implementations for test cases) and use it to indent sections of generated code with several levels of indentations so there are quite a few indent filters.

For include node we can add support for applying filter to its content so that it will be similar to what filter node does. Like this:

{% include "some.stencil" | indent:4, " ",true %}

It's unfortunate that we don't separate context variable and template name with any keyword, like {% include "some.stencil" with value %}, if we were we would be able to apply filters to both variable and included template with a better syntax, i.e.

{% include "some.stencil" | uppercase with value | lowercase %}

instead of what we can have now:

{% include "some.stencil" | uppercase value | lowercase %}

But let's discuss that in another issue?

ilyapuchka commented 5 years ago

For include tag we will need to add brackets parsing so that we can parse this {% include template|lowercase %} and {% include (template|lowercase) | uppercase %} correctly (first only applies filter on template name variable, second applies lowercase filter on template name variable and uppercase filter on template content). It can probably work the same way with sub context, i.e {% include (template|lowercase child|mapValues:lowercase) | uppercase %}