Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing template variables to custom tags/filters? #78

Open
gushromp opened this issue Apr 8, 2015 · 4 comments
Open

Passing template variables to custom tags/filters? #78

gushromp opened this issue Apr 8, 2015 · 4 comments

Comments

@gushromp
Copy link

gushromp commented Apr 8, 2015

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 %}
@yogthos
Copy link
Owner

yogthos commented Apr 8, 2015

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

@gushromp
Copy link
Author

gushromp commented Apr 8, 2015

Thanks!

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

@yogthos
Copy link
Owner

yogthos commented Apr 8, 2015

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.

@didibus
Copy link
Contributor

didibus commented Jul 6, 2023

With PR: #304 you can now use resolve-arg on the arg you want the template variable resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants