<p>I would use a loop to set a conditional, and then check it afterwards. You need to initialize the conditional outside of the loop, otherwise it won't be set outside the loop later.</p>

<pre><code>{% set haveAVideo = false %}
{% for block in matrixBlocks %}
  {% if block.type == 'video' %}
    {% set haveAVideo = true %}
  {% endif %}
{% endfor %}

{% if haveAVideo %}
  {# video block js #}
  {% includeJsFile ... %}
{% endif %}
</code></pre>

CC BY-SA 3.0
2014-08-19T16:17:30.600Z
Thanks Marion, this was exactly what I was looking for. Since I already had a switch case for my blocks I just added the set = true to the top of each block case and placed the conditional tests way at the bottom of my template before the closing endblock tag, though I could have placed this anywhere within my block tag.
Stuart McCoy
CC BY-SA 3.0
2014-08-19T16:24:29.353Z
I would use the [includeJsFile](http://buildwithcraft.com/docs/templating/includejsfile) tag to include the javascript, and keep all the setting and testing code together for clarity. Glad it's working for you!
Marion Newlevant
CC BY-SA 3.0
2014-08-27T16:46:13.250Z
I don't know if this is better but I wound up changing all my switch case logic to include files and placed the variables and js at the top of each respective include file (slideshow js with slideshow include, video js...). This removes the need for the haveVideo variable because the js is only written to the page if the case is true.
Stuart McCoy
CC BY-SA 3.0
2014-08-19T15:49:44.220Z
2014-08-19T15:49:44.220Z
Marion Newlevant
<p>When you're looping through your matrix blocks, check against the <a href="http://buildwithcraft.com/docs/templating/matrixblockmodel#type" rel="nofollow"><code>block.type</code></a> to see what kind of blocks you're dealing with. Assuming you find the block type that you want (at least once), try referencing the JS file with an <a href="http://buildwithcraft.com/docs/templating/includejsfile" rel="nofollow"><code>includeJsFile</code></a> tag...</p>

<pre><code>{% includeJsFile "/your/site/script.js" %}
</code></pre>

<p>When you use that tag, your JS file will be queued up... If a future loop triggers the same tag, but the file is already queued up, it won't be added again.</p>

<p>Another benefit is that this will automatically render the script tag at the bottom of your page, just before the body tag closes.</p>

CC BY-SA 3.0
2014-08-19T15:55:16.147Z
You could additionally wrap the `includeJsFile` tag in a conditional checking for the loop index [`loop.first`](http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable). There's also the [`getFootHtml()`](http://buildwithcraft.com/docs/templating/includejsfile) tag to exactly define the position of the scripts.
carlcs
CC BY-SA 3.0
2014-08-19T15:58:40.927Z
@carlcs I'm not sure `loop.first` plays in here... He only cares about "whether or not" the block is used, not anything to do with the position of that block.
Lindsey D
CC BY-SA 3.0
2014-08-19T16:07:00.427Z
Lindsey yep right. It doesn't work if you loop through all block types at once, and that's what one probably does. Sorry!
carlcs
CC BY-SA 3.0
2014-08-19T16:15:56.293Z
Just adding the IncludeJs and IncludeJsFile to my switch cases repeated the script for each slideshow on the page. Marion's answer below worked and was the direction I was thinking initially but hadn't figured out the best way to test for the block itself with a variable.
Stuart McCoy
CC BY-SA 3.0
2014-08-19T15:46:33.860Z
2014-08-19T15:46:33.860Z
Lindsey D
<p>I need to add some javascript to a page depending upon whether or not a matrix block is used, one is for a slideshow block to load responsiveslides.js, another is for a video block to load fitvid.js. If I place it in the loop then I will get the javascript repeated for each time an entry uses that matrix block. I'm looking to add this towards the bottom of the template and load the .js files once each if the block is used in a loop above. What's the best way to do this?</p>

<matrix><conditional>
What's the best way to setup a conditional to see if a matrix block is used?
# What's the best way to setup a conditional to see if a matrix block is used?

(Asked by: username_0 on 2014-08-19)

I need to add some javascript to a page depending upon whether or not a matrix block is used, one is for a slideshow block to load responsiveslides.js, another is for a video block to load fitvid.js. If I place it in the loop then I will get the javascript repeated for each time an entry uses that matrix block. I'm looking to add this towards the bottom of the template and load the .js files once each if the block is used in a loop above. What's the best way to do this?

**Tags:** matrix, conditional


---

## 2 answers

### Answer by username_1 on 2014-08-19. Score: 5 | Accepted answer

I would use a loop to set a conditional, and then check it afterwards. You need to initialize the conditional outside of the loop, otherwise it won't be set outside the loop later.
{% set haveAVideo = false %}
{% for block in matrixBlocks %}
  {% if block.type == 'video' %}
    {% set haveAVideo = true %}
  {% endif %}
{% endfor %}

{% if haveAVideo %}
  {# video block js #}
  {% includeJsFile ... %}
{% endif %}

> **Comments:**
> **username_0**: Thanks Marion, this was exactly what I was looking for. Since I already had a switch case for my blocks I just added the set = true to the top of each block case and placed the conditional tests way at the bottom of my template before the closing endblock tag, though I could have placed this anywhere within my block tag.  
> **username_1**: I would use the [includeJsFile](http://buildwithcraft.com/docs/templating/includejsfile) tag to include the javascript, and keep all the setting and testing code together for clarity. Glad it's working for you!  
> **username_0**: I don't know if this is better but I wound up changing all my switch case logic to include files and placed the variables and js at the top of each respective include file (slideshow js with slideshow include, video js...). This removes the need for the haveVideo variable because the js is only written to the page if the case is true.  

---

### Answer by username_2 on 2014-08-19. Score: 2

When you're looping through your matrix blocks, check against the block.type to see what kind of blocks you're dealing with. Assuming you find the block type that you want (at least once), try referencing the JS file with an includeJsFile tag...
{% includeJsFile "/your/site/script.js" %}

When you use that tag, your JS file will be queued up... If a future loop triggers the same tag, but the file is already queued up, it won't be added again.
Another benefit is that this will automatically render the script tag at the bottom of your page, just before the body tag closes.

> **Comments:**
> **username_3**: You could additionally wrap the `includeJsFile` tag in a conditional checking for the loop index [`loop.first`](http://twig.sensiolabs.org/doc/tags/for.html#the-loop-variable). There's also the [`getFootHtml()`](http://buildwithcraft.com/docs/templating/includejsfile) tag to exactly define the position of the scripts.  
> **username_2**: @username_3 I'm not sure `loop.first` plays in here... He only cares about "whether or not" the block is used, not anything to do with the position of that block.  
> **username_3**: Lindsey yep right. It doesn't work if you loop through all block types at once, and that's what one probably does. Sorry!  
> **username_0**: Just adding the IncludeJs and IncludeJsFile to my switch cases repeated the script for each slideshow on the page. Marion's answer below worked and was the direction I was thinking initially but hadn't figured out the best way to test for the block itself with a variable.  

---

