javascript Tag
The javascript tag wraps inline JavaScript code in <script> tags. It’s useful for embedding small scripts directly in your templates.
Syntax
Section titled “Syntax”{% javascript %} // Your JavaScript code here{% endjavascript %}Output
Section titled “Output”The tag wraps your code in a <script> element:
{% javascript %} console.log('Hello from Liquid!');{% endjavascript %}<script> console.log('Hello from Liquid!');</script>Example Usage
Section titled “Example Usage”Block-specific initialization:
{# In a carousel block #}<div id="carousel-{{ block.id }}" class="carousel"> {% for slide in block.slides %} <div class="carousel__slide">{{ slide.content_html }}</div> {% endfor %}</div>
{% javascript %} document.addEventListener('DOMContentLoaded', function() { new Carousel('#carousel-{{ block.id }}', { autoplay: {{ block.autoplay | default: false }}, interval: {{ block.interval | default: 5000 }} }); });{% endjavascript %}Conditional scripts:
{% if page.has_map %} {% javascript %} function initMap() { const map = new google.maps.Map(document.getElementById('map'), { center: { lat: {{ venue.latitude }}, lng: {{ venue.longitude }} }, zoom: 15 }); } {% endjavascript %}{% endif %}Passing data to JavaScript:
{% javascript %} window.eventData = { id: '{{ event.id }}', title: '{{ event.title | escape }}', startDate: '{{ event.startDate }}' };{% endjavascript %}