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

Add a filter that returns an array element by index #38

Closed
hatifnatt opened this issue Feb 28, 2020 · 1 comment
Closed

Add a filter that returns an array element by index #38

hatifnatt opened this issue Feb 28, 2020 · 1 comment

Comments

@hatifnatt
Copy link

In Django template It's tricky to access specific array element by index if array is obtained as the result of applying a filter, because it's impossible to directly use index after filter.
I.e. I want only third part of the path

data = /first/second/third/fourth
data|split:'/'|slice:'2:3'|join:''

not very clear and intuitive, slice with single element still returns an array.

Better with custom JS filter, but not very portable, you need to supply your custom filter with your template:

In[Param];
data|split:'/'|index:'2'

In Jinja2 element can be easily accessed by index or slice can be taken without additional filters:

data|split('/')[2]

Go template has an index function.

@HeavyHorst
Copy link
Owner

I added an index filter.

{{ "/home/user/test" | split:"/" | index:"1" }}

You can also use negative indexes like in python for example.

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

2 participants