Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1064387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T18:55:59+00:00 2026-05-16T18:55:59+00:00

I don’t know whether it’s possible, but I’d like to be able to write

  • 0

I don’t know whether it’s possible, but I’d like to be able to write something like the following:

{% with var1 var2 var3 as some_list %}
    {{ some_list|maximum }}
{% endwith %}

Creating a list on the fly from an arbitrary number of template variables and/or literals seems useful, so I’m hopeful that I’ve overlooked something simple.

Failing that, though, I’d like to know how to create a template tag which accepts an arbitrary number of arguments. (I’ve played around with simple_tag, which works well for tags which accept a fixed number of arguments.)

I don’t want to go to the trouble of creating a parser and subclassing django.template.Node until I’ve ascertained that there’s no simpler solution.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T18:56:00+00:00Added an answer on May 16, 2026 at 6:56 pm

    If you want to add a new variable (ie some_list), you’ll need access to the template’s context, so simple_tag won’t be enough.

    For me, the first approach is to try to do this sort of work in the view, in order to keep the templates as simple as possible.

    If that’s not appropriate, you’ll have to write the tag manually, like this:

    @register.tag
    def make_list(parser, token):
      bits = list(token.split_contents())
      if len(bits) >= 4 and bits[-2] == "as":
        varname = bits[-1]
        items = bits[1:-2]
        return MakeListNode(items, varname)
      else:
        raise template.TemplateSyntaxError("%r expected format is 'item [item ...] as varname'" % bits[0])
    
    class MakeListNode(template.Node):
      def __init__(self, items, varname):
        self.items = map(template.Variable, items)
        self.varname = varname
    
      def render(self, context):
        context[self.varname] = [ i.resolve(context) for i in self.items ]
        return ""
    

    And use it like this to create a new variable some_list:

    {% make_list var1 var2 var3 as some_list %}
    

    Feel free to give it a better name!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I don't remember whether I was dreaming or not but I seem to recall
I don't know if anyone has seen this issue before but I'm just stumped.
Don't know why, but sometimes LocationManager is still working also after closing application. I
I don't know when to add to a dataset a tableadapter or a query
I don't want PHP errors to display /html, but I want them to display
I don't expect a straightforward silver bullet answer to this, but what are the
Don't know if there is a better way to do this, so that is
I don't see this in the documentation anywhere, so it probably doesn't exist, but
I don't really have a real world problem, yet, but I'm trying to learn
I don't know the slightest bit of Perl and have to fix a bug

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.