Include a script in the layout only if certain conditions are met

In my default layout, I need to include a javascript file (carousel.js) only if the page will contain a carousel slider. If the page does not contain a carousel slider then do nothing. I have created an include for the carousel.js and named it carousel-script.html. There are several pages in my site that will contain a carousel slider. What is the best way to do this? I have tried creating a variable in the front matter and calling it ‘carousel’ but it doesnt work. Here is what I have tried so far:

{% if carousel == ‘true’ %}
{% include carousel-script.html %}
{% endif %}

1 Like

you don’t need the == true part - though you should be able to do it with it, but it might be true not 'true' ?

try it like:

{% if carousel %}
{% include carousel-script.html %}
{% endif %}

and then in the front matter have :
carousel: true

Thank you @rdyar, that worked.