<p>You can use the <code>in</code> operator to check if the HTML from your Redactor field contains a particular tag:</p>
<pre class="lang-twig prettyprint-override"><code>{% set hasImgTag = '&lt;img' in i.media %}
</code></pre>
<p>However, this will break if your redactor input contains anything unexpected. You're having trouble here because you're trying to process <em>unstructured</em> content in a <em>structured</em> way. Redactor is terrible for structured content, optimized images, semantic HTML etc.</p>
<p>Instead, I would use different fields to allow editors to add images or videos to a block. I assume <code>questionBlock</code> is a matrix field? In this case, you can use two different block types for images and videos with an upload field. Then you can switch in your template based on the block type and generate the correct markup for those assets respectively.</p>
<p>Another option would be a single upload field that allows both video and image files. Then you can switch based on the file type / extension and display the according markup. Both options are much better for usability (for your editors) and more future-proof.</p>

CC BY-SA 4.0
2022-01-06T09:25:33.380Z
Thanks for the amazing answer! I just wanted to ask as well - is there a way to get the data from the i.meida i.e. lets say it is a IMG - is there a way to grab the i.media.url ?
Wally
CC BY-SA 4.0
2022-01-06T09:31:49.630Z
@Wally I don't think so – the Redactor output will be HTML, so you can't get the original asset easily. You could probably get the `src` URL using RegEx, but that wouldn't accomplish much, and you wouldn't be able to output responsive images this way. As I mentioned, use an asset field instead if you want to process the image/video files further or customize the output.
MoritzLost
CC BY-SA 4.0
2022-01-06T09:34:07.080Z
Awesome & thanks again for the helpful knowledge  - I had a feeling that would be the case but always good to check
Wally
CC BY-SA 4.0
2022-01-05T15:32:27.737Z
2022-01-05T15:32:27.737Z
MoritzLost
<p>Is there a way to use a switch statement on redactors output i.e. to say -</p>
<p><strong>If this output is a video &gt; use this code</strong></p>
<p><strong>if this output is a Image &gt; use this code</strong></p>
<p>Why? Well i'm trying to apply imager (to reduce image sizes) to the images that are being added into redactor.</p>
<p>Little more detail:</p>
<p>So currently I run a loop of all the redactor inputs</p>
<pre><code>{% for i in block.questionBlock.all() %}
                    &lt;article&gt;
                      &lt;div class=&quot;text&quot;&gt;
                        {{ i.answer }}
                      &lt;/div&gt;

                      &lt;div class=&quot;media&quot;&gt;
                        {{ i.media }}
                      &lt;/div&gt;
                    &lt;/article&gt;
                    {% endfor %}
</code></pre>
<p>In the block <code>{{ i.media }}</code> I need to get into this block to check its contents and see if it is an image and if it is to apply imager to it. I figured that a switch statement would work best, but I am unsure how to check what the content of <code>{{ i.media }}</code>.</p>
<p>I know that the picture will have a <code>img</code> html tag and the video with have a <code>iframe</code> tag - so is there a way to say.</p>
<pre><code>if i.media contains img
</code></pre>
<p>Any ideas?</p>
<p>Thanks - W</p>

<redactor>
Switch statement for redactor components
# Switch statement for redactor components

(Asked by: username_0 on 2022-01-05)

Is there a way to use a switch statement on redactors output i.e. to say -
If this output is a video > use this code
if this output is a Image > use this code
Why? Well i'm trying to apply imager (to reduce image sizes) to the images that are being added into redactor.
Little more detail:
So currently I run a loop of all the redactor inputs
{% for i in block.questionBlock.all() %}
                    
                      
                        {{ i.answer }}
                      

                      
                        {{ i.media }}
                      
                    
                    {% endfor %}

In the block {{ i.media }} I need to get into this block to check its contents and see if it is an image and if it is to apply imager to it. I figured that a switch statement would work best, but I am unsure how to check what the content of {{ i.media }}.
I know that the picture will have a img html tag and the video with have a iframe tag - so is there a way to say.
if i.media contains img

Any ideas?
Thanks - W

**Tags:** redactor

---

## 1 answers

### Answer by username_1 on 2022-01-05. Score: 2 | Accepted answer

You can use the in operator to check if the HTML from your Redactor field contains a particular tag:
{% set hasImgTag = '
However, this will break if your redactor input contains anything unexpected. You're having trouble here because you're trying to process unstructured content in a structured way. Redactor is terrible for structured content, optimized images, semantic HTML etc.
Instead, I would use different fields to allow editors to add images or videos to a block. I assume questionBlock is a matrix field? In this case, you can use two different block types for images and videos with an upload field. Then you can switch in your template based on the block type and generate the correct markup for those assets respectively.
Another option would be a single upload field that allows both video and image files. Then you can switch based on the file type / extension and display the according markup. Both options are much better for usability (for your editors) and more future-proof.

> **Comments:**
> **username_0**: Thanks for the amazing answer! I just wanted to ask as well - is there a way to get the data from the i.meida i.e. lets say it is a IMG - is there a way to grab the i.media.url ?  
> **username_1**: @Wally I don't think so – the Redactor output will be HTML, so you can't get the original asset easily. You could probably get the `src` URL using RegEx, but that wouldn't accomplish much, and you wouldn't be able to output responsive images this way. As I mentioned, use an asset field instead if you want to process the image/video files further or customize the output.  
> **username_0**: Awesome & thanks again for the helpful knowledge  - I had a feeling that would be the case but always good to check  

---

