Liquid in section

Interpolation

Interpolation is a way to print out the dynamic data inside the HTML structure.

liquid.json
{% assign shop_name = shop.name %}
<h1 class="@attr(section.settings.class)">
    Hello @print(section.settings.name)! Welcome to the {{ shop.shop_name }}
</h1>

Evaluate

The engine is built in Javascript, In some situations, it's useful to embed logic code into your views. You can use the blade @js directive to execute your logic code. It works similarly to <?php and ?> in PHP programming language.

liquid.json
<ul>
@js
    let items = section.settings.items_group;
    items.map((item) => {
        print('<li>'+item.name+'</li>'); 
        // the print function works same with @print()
    })
@endjs
</ul>

Conditionals

You may construct if statements using the @if, @elseif, @else, and @endif directives. These directives function identically to their Javascript counterparts:

liquid.json
@if (section.settings.limit_items < 5)
   Product items limit less than 5
@elseif ( section.settings.limit_items < 10)
    Product items limit less than 10
@else
    Product items limit greater than 10
@endif

For convenience, Blade also provides an @unless directive:

liquid.json
@unless (section.settings.show_logo == 'yes')
    Customer don't want to show logo
@endunless 

Switch case

Switch statements can be constructed using the @switch, @case, @break, @default and @endswitch directives:

liquid.json
@switch (section.settings.head)
    @case 'one'
        <h1>The case one of variable section.settings.head<1>
    @break
    @case 'two'
        <h2>The case two</h2>
    @break
    @default
        <h3>The default</h3>
    @break
@endswitch

For Loop

In addition to conditional statements, Engine provides simple directives for working with Javascript's loop structures. Again, each of these directives functions identically to their Javascript counterparts:

liquid.json
// Loop JSON object
@for (object as key => item)
	<p>@print(key): <strong>@print(item)</strong></p>
@elsefor
	<p>No items</p>
@endelse

// Loop an array
@for (array as item)
	<p>@print(item)</p>
	// The current index is: _i
@endfor

// Loop sequentially 
@for (var i=0; i<10; i++)
	<p>@print(i)</p>
@endfor

While loop

liquid.json
@var i=10
@while (i > 0)
<p>@print(i--)</p>
@endwhile

If you love LayoutHub, could you consider posting an review? That would be awesome and really help us to grow our business, here is the link.

Last updated