Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to apply a slice to a back-end generated data?

Here are my 2 datas in a <div>:

<div class="NP" id="sliced">{{app.user.Firstname}}{{app.user.Lastname}}</div>

I would like to slice these 2 datas so it displays the first letters of each string, in the div.

Roughly, I would like to make some like:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 {{app.user.Firstname.slice(0,2)}}{{app.user.Lastname.slice(0,2)}}

but I don’t know how to implement JS or JQUERY into these things.
I tried to attribute a script to the data, in vain

 {{ app.user.firstName, {'label_attr' : {'script' : `$("#sliced").slice(0,2);`} }}

It returns me a punctuation error even if I only try to attr a simple class.

>Solution :

No need to use JS. You just need to use the correct filter. You don’t use . to access filters, you use the pipe (|).

<div>{{ app.user.Firstname|slice(0,2) }}{{ app.user.Lastname|slice(0,2) }}</div>

Which can also be expressed like this.

<div>{{ app.user.Firstname[:2] }}{{ app.user.Lastname[:2] }}</div>

Docs.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading