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

Appending to a list #240

Closed
jbergstroem opened this issue Jun 4, 2014 · 12 comments
Closed

Appending to a list #240

jbergstroem opened this issue Jun 4, 2014 · 12 comments

Comments

@jbergstroem
Copy link
Contributor

I'm trying to append to a list. Jinja2 docs suggest a similar pattern:

{% do navigation.append('a string') %}

Neither do nor list.append() seems to exist in nunjucks.

@sbruchmann
Copy link

You might want to take a look at nunjucks-append to see how this could be implemented with nunjucks.

@jbergstroem
Copy link
Contributor Author

@sbruchmann Thanks for the pointer; looks a bit out of scope -- but I'll have a closer look.

@mattbasta
Copy link
Contributor

Did you try push()? They're just JavaScript arrays
On Jun 3, 2014 6:28 PM, "Johan Bergström" notifications@github.com wrote:

I'm trying to append to a list. Jinja2 docs suggest a similar pattern:

{% do navigation.append('a string') %}

Neither do nor list.append() seems to exist in nunjucks.


Reply to this email directly or view it on GitHub
#240.

@jbergstroem
Copy link
Contributor Author

@mattbasta Excuse my inexperience, but how would I call push() from a block within a template? Are you perhaps suggesting that I should declare the variable and pass it to the template while rendering?

@mattbasta
Copy link
Contributor

No. There is no list.append() in Nunjucks because append is a method that comes from Python. Use list.push() instead.

@jbergstroem
Copy link
Contributor Author

Sorry, should've been closed a while ago.

@paulmsmith
Copy link

In what context should I use push? I want to do the same thing. However when I do:

{{ myarray.push(myobj) }}

I'm seeing what looks like an index outputted?

@jlongster
Copy link
Contributor

I'm seeing what looks like an index outputted?

That's because you're calling the native array push method which mutates the array and doesn't return the array (unfortunately). If you want the array returned you should write a custom filter. We haven't built this into core because we want to discourage too much of this in templates, as you really should avoid doing this in the template.

@paulmsmith
Copy link

Thanks for the quick reply @jlongster. That makes sense, I didn't want to do it in the templates either, I was doing it as a quick fix because I'm waiting for a fellow developer to write the filter.

@jlongster
Copy link
Contributor

Ok. For now just put that expression at the top-level of your template and use myarray later, it will be mutated.

@rambo-panda
Copy link

<<set a = [1,2]>>
<<set a = (a.push(3),a) >>

@tk-codes
Copy link

@rambo-panda
Your solution works. but how efficient is that?

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

No branches or pull requests

7 participants