Good day!

In Craft CMS 3, I'm tring to configure the replacement of tags for markdown via replace, everything works except for the tag {{ loop.index }}

Please tell me how to properly configure to work correctly {{ loop.index }}?

{{ block.markdownContent|markdown

 |replace('<p>', '<p class="section__paragraph text">')
 |replace('<h2>', '<h2 class="section__title title">')
 |replace('<a ', '<a class="link link-color"')
 |replace('<ul>', '<ul class="attention">')
 |replace('<li>', '<li class="attention__item"><p class="attention__text">')
 |replace('</li> ', '</p></li>')
 |replace('<ol>', '<ol class="remark list">')
 |replace('<li>', '<li class="remark__item item"><p class="remark__text"><span class="remark__label">{{ loop.index }}</span>')
 |replace('</li> ', '</p></li>')|raw }}

Can you advise other ways of realizing this task.

  • {{ loop.index }} is usually part of Twig language. I'm not sure why/how you are going to change the syntax of the language, More important: why does your field contains Twig Syntax in the first place? Apr 19 '18 at 9:18
  • Can there be a more correct solution to the problem?
    – paveell
    Apr 19 '18 at 9:23
  • Described the problem here: craftcms.stackexchange.com/questions/25754/…
    – paveell
    Apr 19 '18 at 11:39

Twig output tags won't work inside of Twig output tags.

You should be using '<li class="remark__item item"><p class="remark__text"><span class="remark__label">'~loop.index~'</span>'

The overall approach seems a bit heavy-handed though.

  • You beat me to it by 7 secs :(
    – carlcs
    Apr 19 '18 at 9:22
  • I'll remember this next time you beat me :) Apr 19 '18 at 9:25

You are using {{ ... }} echo syntax within {{ ... }} to return a variable. Use string concatenation instead:

'<span class="remark__label">' ~ loop.index ~ '</span>'
  • Yes, this is how the numbering works, but it does not work, it gives out 1 1 1 1
    – paveell
    Apr 19 '18 at 9:29
  • loop.index not returning what you want is a different issue. Can you open a new question where you share the code including your for-loops please (and leave out all irrelevant things like replace filters)?
    – carlcs
    Apr 19 '18 at 9:43
  • 1
    Described the problem here: craftcms.stackexchange.com/questions/25754/…
    – paveell
    Apr 19 '18 at 10:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.