question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing template variable as a filter to Image tags

See original GitHub issue

Hi,

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:open
  • Created 8 years ago
  • Reactions:3
  • Comments:9 (3 by maintainers)

github_iconTop GitHub Comments

3reactions
mx-mothcommented, Feb 3, 2016

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) }}.

2reactions
favoyangcommented, Jan 21, 2021

Here’s a monkey patch to resolve image specs from a context variable.

https://gist.github.com/favoyang/0641c63fea72485f412ec97de7035691

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found