2

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.

Brad Bell
  • 67,440
  • 6
  • 73
  • 143
paveell
  • 31
  • 1
  • {{ 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? – Robin Schambach Apr 19 '18 at 09:18
  • Can there be a more correct solution to the problem? – paveell Apr 19 '18 at 09:23
  • Described the problem here: https://craftcms.stackexchange.com/questions/25754/how-can-i-add-and-replace-classes-in-the-markdown-field – paveell Apr 19 '18 at 11:39

2 Answers2

2

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.

Andris Sevcenko
  • 2,997
  • 11
  • 27
2

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

'<span class="remark__label">' ~ loop.index ~ '</span>'
carlcs
  • 36,220
  • 5
  • 62
  • 139
  • Yes, this is how the numbering works, but it does not work, it gives out 1 1 1 1 – paveell Apr 19 '18 at 09: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 09:43
  • 1
    Described the problem here: https://craftcms.stackexchange.com/questions/25754/how-can-i-add-and-replace-classes-in-the-markdown-field – paveell Apr 19 '18 at 10:22