<p>If you need to do something like the image below. The best option is to set the content as a variable and use the <code>replace()</code> filter to replace the tokens tags. Finally, use <code>autoescape</code> pair tag to output the content.</p>

<p>Not an elegant solution, but it works for me ;)</p>

<pre><code>{# #############
        Field names
        -----
        Body: f_body
        Assets:
            f_media
                position
                media (assets)
############# #}


{% block content %}

    {% set body = entry.f_body|raw %}

    {% for item in entry.f_media %}
        {% if item.type == 'assets' %}
            {% set name = '{image_' ~ loop.index ~ '}' %} # name of token to replace
            {% set image = item.media[0].getUrl() %} # image path
            {% set imageElement = '&lt;img src="' ~ image ~ '" class="' ~ item.position ~ '" /&gt;'  %} # image element

            {% set body = body|replace(name, imageElement) %}
        {% endif %}
    {% endfor %}


    {% autoescape false %}
        {{ body }}
    {% endautoescape %}

{% endblock %}
</code></pre>

<p>ps: This is something I came up quickly, needs bit of refactoring obvs.</p>

<p><a href="https://i.sstatic.net/EwbMt.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/EwbMt.png" alt="Image replacement technique"></a></p>

CC BY-SA 3.0
2017-11-07T21:50:49.513Z
Late to the party boss ;)
Dimitri Kouvdis
CC BY-SA 3.0
2017-11-09T15:28:58.730Z
what a legend! boom
Richard Frank
CC BY-SA 3.0
2017-11-07T21:44:36.167Z
2017-11-10T16:57:12.977Z
2017-11-10T16:57:12.977Z
Dimitri Kouvdis
<p>This generally isn't necessary in Craft. You can use a Matrix Field and combination of Matrix Blocks to build advanced content with flexible images. This is really the best way to go if it's an option.</p>

CC BY-SA 3.0
2014-07-26T16:10:00.307Z
2014-07-26T16:10:00.307Z
Ben Parizek
<p>I'd like to use a tokenised image replacement technique which is commonly adopted in ExpressionEngine. This is where the author can add image tokens such as {image_1}, {image_2} etc into their content, and these get replaced by images defined in a matrix in the same publish form (with additional information such as image size etc). What technique can I use to achieve this?</p>

CC BY-SA 3.0
2014-07-26T15:02:55.030Z
This generally isn't necessary in Craft. You can use a Matrix Field and combination of Matrix Blocks to build advanced content with flexible images.  This is really the best way to go if it's an option.  That said, if you really needed what you've described, you could possibly do so with outputting your Rich Text field with Twig's template_from_string function and looping through your tokens field with the replace filter or something like that. I'm not posting this as an answer as I haven't tested this.
Ben Parizek
CC BY-SA 3.0
2014-07-26T15:29:32.297Z
@Ben the Matrix part of your comment alone is worth an actual answer, that I'd tag as accepted if this was my question.
carlcs
CC BY-SA 3.0
2014-07-26T16:10:16.470Z
Ok @ChristianSeelbach, you talked me into it =)
Ben Parizek
<assets><matrix>
How to integrate an image haystack replacement technique
# How to integrate an image haystack replacement technique

(Asked by: username_0 on 2014-07-26)

I'd like to use a tokenised image replacement technique which is commonly adopted in ExpressionEngine. This is where the author can add image tokens such as {image\_1}, {image\_2} etc into their content, and these get replaced by images defined in a matrix in the same publish form (with additional information such as image size etc). What technique can I use to achieve this?

**Tags:** assets, matrix
> **Comments:**
> **username_1**: This generally isn't necessary in Craft. You can use a Matrix Field and combination of Matrix Blocks to build advanced content with flexible images.  This is really the best way to go if it's an option.  That said, if you really needed what you've described, you could possibly do so with outputting your Rich Text field with Twig's template_from_string function and looping through your tokens field with the replace filter or something like that. I'm not posting this as an answer as I haven't tested this.  
> **username_2**: @Ben the Matrix part of your comment alone is worth an actual answer, that I'd tag as accepted if this was my question.  
> **username_1**: Ok @ChristianSeelbach, you talked me into it =)  

---

## 2 answers

### Answer by username_3 on 2017-11-07. Score: 3 | Accepted answer

If you need to do something like the image below. The best option is to set the content as a variable and use the replace() filter to replace the tokens tags. Finally, use autoescape pair tag to output the content.
Not an elegant solution, but it works for me ;)
{# #############
        Field names
        -----
        Body: f_body
        Assets:
            f_media
                position
                media (assets)
############# #}


{% block content %}

    {% set body = entry.f_body|raw %}

    {% for item in entry.f_media %}
        {% if item.type == 'assets' %}
            {% set name = '{image_' ~ loop.index ~ '}' %} # name of token to replace
            {% set image = item.media[0].getUrl() %} # image path
            {% set imageElement = ''  %} # image element

            {% set body = body|replace(name, imageElement) %}
        {% endif %}
    {% endfor %}


    {% autoescape false %}
        {{ body }}
    {% endautoescape %}

{% endblock %}

ps: This is something I came up quickly, needs bit of refactoring obvs.

> **Comments:**
> **username_3**: Late to the party boss ;)  
> **username_0**: what a legend! boom  

---

### Answer by username_1 on 2014-07-26. Score: 6

This generally isn't necessary in Craft. You can use a Matrix Field and combination of Matrix Blocks to build advanced content with flexible images. This is really the best way to go if it's an option.


---

