Skip to content

Add an as X option to the for template tag that captures loop output as a list #176

Description

@meesterguyman

Code of Conduct

  • I agree to follow Django's Code of Conduct

Feature Description

It's currently possible to capture the output of many template tags by appending as X at the end. This captures the output as a singular value for use later in the template. I would like to propose that we add a similar option to the for template tag and capture the output of the loop as a list of individual values, each of which can be further manipulated and passed to other templates for display.

Problem

In generating responsive columns for newsletters, we're passing in a queryset of posts. I would like to be able generate the display contents for each column within the template, save the column displays as a list of displays, inject it into template context, and then pass this list into a columns.html template, which would be responsible for arranging these into a standardized column format. I would expect this to look something like the following:

{% for post in posts as post_display_list %}
  <div class="photo">{{ post.photo }}</div>
  <div class="title">{{ post.title }}</div>
  <div class="desc">{{ post.desc }}</div>
{% endfor %}
{% include 'newsletters/layout/columns.html' column_contents=post_display_list column_widths=200 %}

Presently, the only way I know to do what I'm after here cleanly is to generate post_display_list programmatically within a view and then pass it in as context, but this is not at all ideal. Because we're dealing with presentation here, it would be much better to have the HTML viewable inline with the surrounding template, so that for more complex cell layouts especially, it's easier to understand and update. By capturing the output as a list of values instead of one big string, it can be passed into another template for further manipulation.

I'm considering building this myself one way or another, but this seems an intuitive enough addition to the for tag that I would think it would be useful for everyone, so thought I'd mention it here as a feature request.

Request or proposal

proposal

Additional Details

No response

Implementation Suggestions

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status
    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions