1

I have a rich text field, e.g. for defining an image caption. Now in my template I would like to prepend some text to the content of the rich text field, e.g. Figure 1: [rich text]. Is this possible? The content of the paragraph-tag of the rich text field should include the prepended text.

3

If you want to prepend another paragraph you could to this:

{% set paragraphs = entry.body|split('</p>') %}
{% for paragraph in paragraphs %}
  {% if loop.index == 1%}
    <p>Prepended Custom Text</p>
  {% endif %}
    {{paragraph|raw}}
{% endfor %}

If you want to display a custom string in the first paragraph itself you could do this:

{% set paragraphs = entry.body|split('</p>') %}
{% for paragraph in paragraphs %}
  {% if loop.index == 1%}
    {% set paragraph = paragraph|replace({'<p>': '<p>My custom prepended Text '}) %}
  {% endif %}
    {{paragraph|raw}}
{% endfor %}

This looks a little bit hacky tough there are probably prettier ways to do so.

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.