Selmer
Selmer copied to clipboard
Passing template variables to custom tags/filters?
Hello, Is this possible?
E.g.
(selmer/add-tag! :getter
(fn [args context-map]
(let [field (first args)]
(str (if (= (:javaType field) "boolean") "is" "get") (:name field)))))
... or using a filter instead of the tag. And in the template:
{% for field in model.fields %}
{% getter field %}
{% endfor %}
The tag will be passed a string literal, so if you wish to use the value then you would have to do that with a filter, e.g:
(add-filter!
:getter
(fn [field] (str (if (= (:javaType field) "boolean") "is" "get") (:name field))))
(render
"{% for field in model.fields %}
{{field|getter}}
{% endfor %}"
{:model {:fields [{:javaType "boolean"} {:javaType "integer"}]}})
Thanks!
Are there plans on adding the same functionality to custom tags as well (seeing how it already works for built-in tags)?
I'd have to look what would be involved in exposing that to custom tags, but it probably wouldn't happen in the near future. And since the filters can already be used for this purpose I don't think that's a pressing issue.
With PR: https://github.com/yogthos/Selmer/pull/304 you can now use resolve-arg
on the arg you want the template variable resolved.