Passing template variable as a filter to Image tags
See original GitHub issueHi,
Have you consider doing something like this:
{% for img in self.get_images %}
{% get_custom_filter forloop.counter as my_filter_var %}
{% image img my_filter_var %}
{% endfor %}
This is useful when we want to assign different filters depending on the forloop counter, or some other condition.
I’m working on a solution, but for now, my solution only accepts a variable or chained filters. Do you see this being integrated in wagtail?
Issue Analytics
- State:
- Created 8 years ago
- Reactions:3
- Comments:9 (3 by maintainers)
Top Results From Across the Web
Built-in template tags and filters - Django documentation
This document describes Django's built-in template tags and filters. ... use a literal string as a fallback value in case all passed variables...
Read more >Introduction to Custom Template Filters and Tags - Packt Hub
The template value and the variable name are passed to the ParseNode class. The render method of that class at first resolves the...
Read more >How to pass three or multiple arguments to custom template ...
filter (name='thumbnail') template tag. I am using image resize function have contain 2 args image object and size now i want to pass...
Read more >Custom Filters And Tags Reference - ActionKit Manual
Default: HTML-escape Django variables included by the template in var. HTML that's part of the template itself is passed through unmodified. {% include_tmpl...
Read more >can we pass objects in template tags - Google Groups
The token object seems to be a string!! but how to get some objects?? Christian Erhardt's profile photo ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I’ve also come up against this issue before, where I needed to output different sizes based on some criteria. A giant
if/elif
block in the template is very ugly. Repeat that a number of times if you need to reuse the logic, and accepting a variable for the image filter spec becomes much more desirable.If a developer abuses this feature, that is on their head. A note could be put in the docs mentioning that making arbitrary sizes will fill up the file system, but limiting the developers freedom hurts more than it helps from where I am coming from.
As an aside, this is actually possible in the Jinja version of the
image
tag. It accepts a string argument:{{ image(my_img, "max-200x100") }}
or{{ image(my_img, my_filter_var) }}
.Here’s a monkey patch to resolve image specs from a context variable.
https://gist.github.com/favoyang/0641c63fea72485f412ec97de7035691