Selmer icon indicating copy to clipboard operation
Selmer copied to clipboard

Passing template variables to custom tags/filters?

Open gushromp opened this issue 9 years ago • 4 comments

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 %}

gushromp avatar Apr 08 '15 09:04 gushromp

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"}]}})

yogthos avatar Apr 08 '15 12:04 yogthos

Thanks!

Are there plans on adding the same functionality to custom tags as well (seeing how it already works for built-in tags)?

gushromp avatar Apr 08 '15 13:04 gushromp

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.

yogthos avatar Apr 08 '15 13:04 yogthos

With PR: https://github.com/yogthos/Selmer/pull/304 you can now use resolve-arg on the arg you want the template variable resolved.

didibus avatar Jul 06 '23 06:07 didibus