Answers.Body: <p>This is partially side stepping your question but, it could be a solid option just the same: </p>
  
  <p>You might consider checking out Selvin's <a href="https://github.com/selvinortiz/craft.doxter" rel="nofollow noreferrer">Doxter Markdown plugin</a>. It has support for Entries, Assets, Users, Tags, Globals and Categories.</p>
  
  <p><img src="https://monosnap.com/file/FJBdyRxEgtep0L8hGkSI2ZczVfKnCh.png" alt="Craft Doxter markdown icons"></p>
  
  <p>This all via reference codes which can be copied and pasted into the Markdown as needed.</p>
  
  <p><img src="https://monosnap.com/file/WQK6Rv4pwHZKJCSKa21ugH0Af0hpae.png" alt="reference code"></p>
  
  <p><a href="https://i.sstatic.net/DHBOp.gif" rel="nofollow noreferrer"><img src="https://i.sstatic.net/DHBOp.gif" alt="Doxter walkthrough"></a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-13T21:17:53.353Z
Answers.Text: Cheers Natetronn. I'll take a look.
Answers.UserDisplayName: Jonathan Schofield
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T09:10:34.933Z
Answers.Text: Thanks for the suggestion. The documentation for [reference tags](https://craftcms.com/docs/reference-tags) is devoid of any examples, but if I read between the lines correctly one has to know the ID of an element _in advance_ in order to be able to refer to it in a reference tag -- so I don’t think that helps what I’m hoping to do.
Answers.UserDisplayName: Jonathan Schofield
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T19:03:17.413Z
Answers.Text: Did you try the plugin? It allows you to pick an Asset and then presents you the proper reference tag so you can copy and paste it where you want in the Markdown. In other words, there is nothing to know in advance. It literally pops up the Assets window with all the images so the client can select the one they want to use and then, once selected, it offers the proper reference tag, ID included, all set and ready to go.
Answers.UserDisplayName: Natetronn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T19:11:33.317Z
Answers.Text: I added a image to the answer so you can see this it visually in action.
Answers.UserDisplayName: Natetronn
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-16T20:59:16.247Z
Answers.Text: Thanks for the GIF, Natetronn, that helps. I'll definitely try the plugin and have a proper look. Didn't have time last Sunday for what is a side project. Thanks so much for the suggestion.
Answers.UserDisplayName: Jonathan Schofield
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-13T20:26:54.940Z
Answers.LastActivityDate: 2015-12-14T19:24:04.750Z
Answers.LastEditDate: 2015-12-14T19:24:04.750Z
Answers.UserDisplayName: Natetronn
Answers.Body: <p>Well this feels like an awful kludge but at least I have it working with a finite number of image variables in the code below. </p>
  
  <p>There has to be a more elegant and flexible way to do this. And Natetronn, if your proposal does just that, perhaps you could help me see how. Otherwise, other cleverer folks, do let me know.</p>
  
  <pre><code>{% for subsection in entry.subsections %}
  
      {# Set variables for up to 4 images per subsection #}
      {% set n  = 0 %}
      {% set imageN = '' %}
      {% set image1 = '' %}
      {% set image2 = '' %}
      {% set image3 = '' %}
      {% set image4 = '' %}
  
      {% set sectionImgs = subsection.sectionImages %}
  
      {% for img in sectionImgs %}
          {% set n = loop.index %}
  
          {% set imageN %}
          &lt;figure&gt;
              &lt;img src="{{ img.url }}" alt="{{ img }} {{ n }}"&gt;
              {% if img.caption|length %}
              &lt;figcaption&gt;
                  &lt;span{{ img.caption }}&lt;/span&gt;
              &lt;/figcaption&gt;
              {% endif %}
          &lt;/figure&gt;
          {% endset %}
  
          {# assign imageN to a specific variable #}
          {% switch n %}
              {% case 1 %} {% set image1 = imageN %}
              {% case 2 %} {% set image2 = imageN %}
              {% case 3 %} {% set image3 = imageN %}
              {% case 4 %} {% set image4 = imageN %}
          {% endswitch %}
      {% endfor %}
  
      {{ subsection.sectionText|markdown|replace({
          '%%image1%%': image1, 
          '%%image2%%': image2, 
          '%%image3%%': image3, 
          '%%image4%%': image4
      })|raw }}
  
  {% endfor %}{# /subsection #}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T09:53:00.180Z
Answers.LastActivityDate: 2015-12-14T09:53:00.180Z
Answers.UserDisplayName: Jonathan Schofield
Answers.Body: <p>I'd suggest a better approach as detailed here: <a href="https://experiencehq.net/blog/craft-with-markdown-and-matrix" rel="nofollow">https://experiencehq.net/blog/craft-with-markdown-and-matrix</a>
  Once you go down the "structured content" route you'll never look back, I promise you :)</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-16T21:04:56.350Z
Answers.Text: Awesome, erwinheiser. Thanks, that is just what I wish I'd thought of :)
Answers.UserDisplayName: Jonathan Schofield
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2015-12-14T15:40:29.650Z
Answers.LastActivityDate: 2015-12-14T15:40:29.650Z
Answers.UserDisplayName: erwinheiser
Body: <p>I have a Matrix block with a text field and an Assets field. The text field has a Markdown filter applied to it.</p>
  
  <p>I'd like the content owner to be able to enter a placeholder reference to one of the images uploaded to the sibling Assets field.</p>
  
  <p>For example, when the template processes…</p>
  
  <pre><code>This is a **paragraph**.
  
  %%img2%%
  
  This is some more text.
  </code></pre>
  
  <p>…then <code>%%img2%%</code> (or some such syntax that won't conflict with Markdown) gets replaced with the HTML generated for the second image by the relevant Assets query running in the template.</p>
  
  <p>I've done things like this before with MXjumper and ExpressionEngine but am pretty new to Craft and not sure how to go about it.</p>
  
  <p>I figure I need to: </p>
  
  <ol>
  <li>Set a <em>dynamically named</em> variable (because I don't know in advance how many images are in the image field)</li>
  <li>Use <em>replace</em> on the text field, but the replace statement should incorporate dynamically named search strings and the replacements would be the variables named at step 1 not strings.</li>
  </ol>
  
  <p>Really not sure how to execute either of these 2 steps in Twig.</p>
  
  <p>Would something like the following work for step1?</p>
  
  <pre><code>{% for img in sectionImages %}
      {% set n %}{{ loop.index }}{% endset %}
      {% set img ~ n %}
      &lt;figure&gt;
          &lt;img src="{{ img.url }}" alt="{{ img }}"&gt;
          {% if img.caption|length %}
          &lt;figcaption&gt;
              &lt;span{{ img.caption }}&lt;/span&gt;
          &lt;/figcaption&gt;
          {% endif %}
      &lt;/figure&gt;
      {% endset %}
  {% endfor %}
  </code></pre>
  
  <p>And what then for step 2…</p>
  
  <pre><code>{{ block.sectionText|markdown|replace('%%img' ~ n ~ '%%': img ~ n) }}
  </code></pre>
  
  <p>My step 1 code feels like it's close but I suspect it's not right. And in any case I feel sure my step 2 code is further off from what's needed.</p>
  
  <p>I think maybe I need to be storing <code>img</code> as key|value pairs in an array and working with that somehow.</p>
  
  <p>Any pointers much appreciated.</p>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-12-13T19:52:08.543Z
Comments.Text: I sense I need to be using [attribute](http://twig.sensiolabs.org/doc/functions/attribute.html) for dynamically named variables, but the lack of examples in Twig documentation has me stumped.
Comments.UserDisplayName: Jonathan Schofield
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-12-16T07:57:46.087Z
Comments.Text: Is this tiny images you need to insert inline? Otherwise, why not do like everyone else and make the author end the block, add image block and then continue with another text block? You could even make this work for inline images, just remove the surrounding </p> and <p> with Twig.
Comments.UserDisplayName: carlcs
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2015-12-16T20:57:48.213Z
Comments.Text: Thanks carlcs. Yes, that's an option I've considered and done elsewhere. Pros and cons to do with content coherence in the CMS I'm weighing up.
Comments.UserDisplayName: Jonathan Schofield
Tags: <assets><variable><replace><set>
Title: Replace placeholder string in text field with HTML generated by Asset query
ThreadText: # Replace placeholder string in text field with HTML generated by Asset query
  
  (Asked by: username_0 on 2015-12-13)
  
  I have a Matrix block with a text field and an Assets field. The text field has a Markdown filter applied to it.
  
  I'd like the content owner to be able to enter a placeholder reference to one of the images uploaded to the sibling Assets field.
  
  For example, when the template processes…
  
  ```
  This is a **paragraph**.
  
  %%img2%%
  
  This is some more text.
  
  ```
  
  …then `%%img2%%` (or some such syntax that won't conflict with Markdown) gets replaced with the HTML generated for the second image by the relevant Assets query running in the template.
  
  I've done things like this before with MXjumper and ExpressionEngine but am pretty new to Craft and not sure how to go about it.
  
  I figure I need to:
  
  1. Set a *dynamically named* variable (because I don't know in advance how many images are in the image field)
  2. Use *replace* on the text field, but the replace statement should incorporate dynamically named search strings and the replacements would be the variables named at step 1 not strings.
  
  Really not sure how to execute either of these 2 steps in Twig.
  
  Would something like the following work for step1?
  
  ```
  {% for img in sectionImages %}
      {% set n %}{{ loop.index }}{% endset %}
      {% set img ~ n %}
      <figure>
          <img src="{{ img.url }}" alt="{{ img }}">
          {% if img.caption|length %}
          <figcaption>
              <span{{ img.caption }}</span>
          </figcaption>
          {% endif %}
      </figure>
      {% endset %}
  {% endfor %}
  
  ```
  
  And what then for step 2…
  
  ```
  {{ block.sectionText|markdown|replace('%%img' ~ n ~ '%%': img ~ n) }}
  
  ```
  
  My step 1 code feels like it's close but I suspect it's not right. And in any case I feel sure my step 2 code is further off from what's needed.
  
  I think maybe I need to be storing `img` as key|value pairs in an array and working with that somehow.
  
  Any pointers much appreciated.
  
  **Tags:** assets, variable, replace, set
  
  > **Comments:**
  > **username_0**: I sense I need to be using [attribute](http://twig.sensiolabs.org/doc/functions/attribute.html) for dynamically named variables, but the lack of examples in Twig documentation has me stumped.  
  > **username_1**: Is this tiny images you need to insert inline? Otherwise, why not do like everyone else and make the author end the block, add image block and then continue with another text block? You could even make this work for inline images, just remove the surrounding </p> and <p> with Twig.  
  > **username_0**: Thanks username_1. Yes, that's an option I've considered and done elsewhere. Pros and cons to do with content coherence in the CMS I'm weighing up.  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_2 on 2015-12-13. Score: 2
  
  This is partially side stepping your question but, it could be a solid option just the same:
  
  You might consider checking out Selvin's [Doxter Markdown plugin](https://github.com/selvinortiz/craft.doxter). It has support for Entries, Assets, Users, Tags, Globals and Categories.
  
  ![Craft Doxter markdown icons](https://monosnap.com/file/FJBdyRxEgtep0L8hGkSI2ZczVfKnCh.png)
  
  This all via reference codes which can be copied and pasted into the Markdown as needed.
  
  ![reference code](https://monosnap.com/file/WQK6Rv4pwHZKJCSKa21ugH0Af0hpae.png)
  
  [![Doxter walkthrough](https://i.sstatic.net/DHBOp.gif)](https://i.sstatic.net/DHBOp.gif)
  
  > **Comments:**
  > **username_0**: Cheers username_2. I'll take a look.  
  > **username_0**: Thanks for the suggestion. The documentation for [reference tags](https://craftcms.com/docs/reference-tags) is devoid of any examples, but if I read between the lines correctly one has to know the ID of an element _in advance_ in order to be able to refer to it in a reference tag -- so I don’t think that helps what I’m hoping to do.  
  > **username_2**: Did you try the plugin? It allows you to pick an Asset and then presents you the proper reference tag so you can copy and paste it where you want in the Markdown. In other words, there is nothing to know in advance. It literally pops up the Assets window with all the images so the client can select the one they want to use and then, once selected, it offers the proper reference tag, ID included, all set and ready to go.  
  > **username_2**: I added a image to the answer so you can see this it visually in action.  
  > **username_0**: Thanks for the GIF, username_2, that helps. I'll definitely try the plugin and have a proper look. Didn't have time last Sunday for what is a side project. Thanks so much for the suggestion.  
  
  ---
  
  ### Answer by username_0 on 2015-12-14. Score: 2
  
  Well this feels like an awful kludge but at least I have it working with a finite number of image variables in the code below.
  
  There has to be a more elegant and flexible way to do this. And username_2, if your proposal does just that, perhaps you could help me see how. Otherwise, other cleverer folks, do let me know.
  
  ```
  {% for subsection in entry.subsections %}
  
      {# Set variables for up to 4 images per subsection #}
      {% set n  = 0 %}
      {% set imageN = '' %}
      {% set image1 = '' %}
      {% set image2 = '' %}
      {% set image3 = '' %}
      {% set image4 = '' %}
  
      {% set sectionImgs = subsection.sectionImages %}
  
      {% for img in sectionImgs %}
          {% set n = loop.index %}
  
          {% set imageN %}
          <figure>
              <img src="{{ img.url }}" alt="{{ img }} {{ n }}">
              {% if img.caption|length %}
              <figcaption>
                  <span{{ img.caption }}</span>
              </figcaption>
              {% endif %}
          </figure>
          {% endset %}
  
          {# assign imageN to a specific variable #}
          {% switch n %}
              {% case 1 %} {% set image1 = imageN %}
              {% case 2 %} {% set image2 = imageN %}
              {% case 3 %} {% set image3 = imageN %}
              {% case 4 %} {% set image4 = imageN %}
          {% endswitch %}
      {% endfor %}
  
      {{ subsection.sectionText|markdown|replace({
          '%%image1%%': image1, 
          '%%image2%%': image2, 
          '%%image3%%': image3, 
          '%%image4%%': image4
      })|raw }}
  
  {% endfor %}{# /subsection #}
  
  ```
  
  
  ---
  
  ### Answer by username_3 on 2015-12-14. Score: 2
  
  I'd suggest a better approach as detailed here: <https://experiencehq.net/blog/craft-with-markdown-and-matrix>
  Once you go down the "structured content" route you'll never look back, I promise you :)
  
  > **Comments:**
  > **username_0**: Awesome, username_3. Thanks, that is just what I wish I'd thought of :)  
  
  ---
  
  