Use parameters within helpers

It's possible now to add parameters to a helper shortcut. It will be possible then to reuse some code with specific options.

Here my random partial. This partial will loop on a dataset coming from arangodb and will use our partial _product for the loop. The do_not_eval attribute indicate that we don't want to parse & compile etlua template yet. It will be compiled at the end of the process so partials can access parents' variables

<% for k, item in pairs(dataset.results) do %>
  {{ partial | _product | do_not_eval }}
<% end %>

Now here the request we want to run :

FOR product IN products
  LET page = @page
  LET count = LENGTH(
    FOR p in products FILTER p.online == true RETURN 1
  )
  FILTER product.online == true
  LET assets = (
    FOR asset IN assets 
      FILTER asset.assetable_id == product.id
      FILTER asset.assetable_type == 'Product' 
      RETURN asset
  )
  __IF limit__ LIMIT @limit __END limit__
  __IF_NOT limit__ LIMIT 4 __END_NOT limit__
  SORT RAND()
  RETURN { product, count, page, assets }

Notice that __IF limit__ ... __END limit__ and __IF_NOT limit__ ... __END_NOT limit__ are not AQL syntax but kinda filters we use to make dynamic requests based on parameters.

And finaly here our _product partial :

<% price = item.product.price_cents/100 %>
<div class="col-lg-3 col-md-3 col-sm-4 col-6 product-item">
  <div class="product-card">
    
    <div class="content">
      <img src="<%= item.image_url%>" />
      <div class="title">
        <h3>
          <a href="/<%= lang %>/our/product/slug/<%= item.product.slug %>">
            <%= item.product.titles['title_' .. lang] %>
          </a>
        </h3>
      </div>
      <p class="description">
        <span class="price-tag">
          <%= tostring(currency[item.product.price_currency]) .. price %>
        </span>
      </p>
    </div>
  </div>
</div>

Now all we have to do is to create a helper

And use this helper were we need it. Let say below the product page we want to display another products : 

<h4>Other products</h4>
<div class="products">
  <div class="row">
    {{ helper | ProductRandom | limit#4 }}
  </div>
</div>

<h4>Similar products</h4>
<div class="products">
  <div class="row">
    {{ helper | ProductRandom | limit#8 }}
  </div>
</div>
Fasty is a very fast CMS built on top of Lapis / openresty / lua and arangoDB

Leave a comment