Liquid - platformOS Liquid Tags

background

Invokes code within the tag asynchronously, in the background. You will only have access to variables you explicitly pass to the background tag. The context object is available by default, but with limitations - you need to explicitly pass page/layout metadata if you want to use it, as well as device and constants.

Params

  • options - Hash - Any variable provided to the background tag will become accessible in the code. The following keys are special: delay [Integer] which defines the number of minutes to delay running code (defaults to 0, which means run code as soon as possible) priority [String] low, default or high - see AsyncCallbackPriorityEnum GraphQL Object max_attempts [Integer] the number of time to re-try job upon failing. Default is 1, maximum is 5 source_name [String] your label of the job, which would help you identify it

Examples


        The following code will run all the log queries in the background, not earlier than 5 minutes since the execution. You will only have access to variables
explicitly provided to the background tag: "data" and "hey". Note that you will not have access to the "my_data" variable. Also note
that the result of this background tag will not be rendered, and you will not have access to any variable inside the background tag.
{% parse_json my_data %}{ "hello": "world" }{% endparse_json %}
{% assign not_available_in_background = "You will not see me" %}
{% background priority: 'low', delay: 5, max_attempts: 3, source_name: "my custom job", data: my_data, hey: 'hello' %}
  {% log not_available_in_background %}{% comment %}Will be null, as this variable was not passed to the background tag {% endcomment %}
  {% log data %}{% comment %}You will see { "hello": "world" }, as it was provided to the background tag{% endcomment %}
  {% log hey %}{% comment %}You will see "hello" as it was provided to the background tag{% endcomment %}
  {% log context %}{% comment %}You will copy the current request context and make it available in the background{% endcomment %}
  {% assign not_available_outside = "You will not see me" %}
  {{ not_available_outside }} {% comment %}This variable will be rendered in the background, meaning you won't see anything on the page{% endcomment %}
{% endbackground %}
{{ not_available_outside }}{% comment %}This will be blank, because the assign will happen in the background - you won't have access to it here{% endcomment %}
      

cache

Checks if there's string cached for the given key. If yes, it just returns the value without processing anything inside the cache tag, otherwise it executes code, stores the result in the cache. When you hit the page with such code for the first time, the code will be executed and hence it will iterate through the loop and generate random strings. However, then the result of the block will be cached and all the following requests within 20 seconds would not invoke the code - instead, it would just take the value from the cache. The output will not change. When the cache expires, the code will be evaluated again, producing another set of random string. Cache is automatically invalidated if any changes are applied to any view/translation.

Params

  • key - String - the key which should uniquely identify the cached fragment
  • expire - Integer - optional. number of seconds since populating the cache to expiration

Examples


        {% cache 'this is my key', expire: 20 %}
  
    {% for i in (1..100) %}
  • {{ 18 | random_string }}
  • {% endfor %}
{% endcache %}

        {% parse_json pagesUpdatedAt %}
{%- cache pagesUpdatedAtCache -%}
{%- graphql g = 'pages_updated_at' | dig: 'admin_pages', 'results' -%}
{{- g -}}
{%- endcache -%}
{% endparse_json %}
{%- export pagesUpdatedAt, namespace: 'nav' -%}
      

content_for

Stores a block of markup in an identifier for later use. Render it later with `yield` tag.

Params

  • name - String - the markups will be stored under this name
  • flush - Boolean - optional. If the flush parameter is true content_for replaces the blocks it is given instead of appending content to the existing one

Examples


        {% content_for 'header' %}
  Hello world
{% endcontent_for %}
      

export

Makes variables defined inside partial accessible anywhere via context.exports.`namespace`

Params

  • variable - Any - variable to be stored inside the namespace
  • namespace - String - namespace under which variable will be in context.exports

Examples


        {% assign a = 'value for a' %}
{% assign b = 'value for b' %}
{% export a, b, namespace: 'my_hash' %}

{{ context.exports.my_hash }} => { "a": "value for a", "b": "value for b" }
{% parse_json company %}
{
  "name": "platformOS",
  "technologies": ["liquid", "graphql"],
  "countries": { "USA": 5, "Poland": 5, "Romania": 2 }
}
{% endparse_json %}

{% export company, namespace: 'data' %}

{{ context.exports.data }} =>
  {"company"=>{"name"=>"platformOS", "technologies"=>["liquid", "graphql"], "countries"=>{"USA"=>5, "Poland"=>5, "Romania"=>2}}}
{{ context.exports.data.company.technologies }} => liquidgraphql
{{ context.exports.data.company.name }} => platformOS
      

form

Used to generate a html form element for a resource. Use within `form configuration`. Inside the tag you can use `form_builder` variable.

Params

  • html-id - String - set id attribute in `<form id="">` element
  • html-novalidate - String - add `novalidate` attribute to `<form novalidate>` element
  • html-class - String - add css class `<form class="">` element
  • html-multipart - Boolean - add `enctype="multipart/form-data"` attribute to `<form enctype="multipart/form-data">` element. Multipart is enabled by default
  • html-data- - attr - [String] add data attribute to `<form data-[attr]="">` element

Examples


        {% form %}
  {{ form_builder }}
{% endform %}
      

        {% form html-id: 'someid', html-novalidate: true, html-class: 'big-form green-form', html-data-user-id: '12345', html-multipart: false %}
{% endform %}
      

graphql

Used to execute GraphQL query stored in a file or invoke GraphQL query inline. All arguments provided to the tag will be passed to GraphQL. It returns a hash with the data or the errors that occurred (for example, variable type mismatch, required variable not provided, syntax error) etc.

Params

  • query_name - String - name of the GraphQL query. For get_users.graphql file name of the query is get_users
  • arguments - Object - optional. key:value pair(s) which will be passed to GraphQL query
  • args - Hash - optional. hash of the arguments which will be passed to GraphQL query

Examples


        Invoke query "get_models" and store the result in variable "g":
{% graphql g = "get_models", model_id: context.params.slug2 %}
      

        Chain filters to make it easier to access the data:
{% graphql g = "get_models", model_id: context.params.slug2 | dig: 'get_models'%}
      

        Pass all arguments at once as a hash:
{% parse_json arguments %}
  { "model_id": {{ context.params.slug2 | json }} }
{% endparse_json %}
{% graphql g = "get_models", args: arguments %}
      

        Pass all arguments at once as a JSON string:
{% graphql g = "get_models", args: '{ "per_page": "20" }' %}
      

        Invoke inline query and store the result in variable "g". Please note - this is NOT recommended for the queries.
{% graphql g, id: context.params.slug2, title: "new title" %}
  mutation { customization_update(id: $id, customization: { title: $title }){ id } }
{% endgraphql %}
      

include_form

This tag should be used to render forms defined in forms directory

Params

  • form_name - String - name of the form configuration from forms directory
  • id - ID - id of the resource you want to edit
  • parent_resource_id - String - name of the resource type, f.e. name of custom_model_type, transactable_type, user_profile_type

Examples


        {% include_form 'signup_form' %}
      

        {% include_form 'edit_user', id: user.id %}
      

        {% include_form 'manager_invite_to_interview', parent_resource_id: 'invitation', user: g.user %}
      

log

Print any information to marketplace logs

Params

  • message - Any - Any object that can be printed
  • type - String - type of log entry. Use it to tag your logs to make it easier to differentiate logs. If you set it to 'error' pos-cli will also notify your OS about it.

Examples


        {% log 'hello world' %}
      

        {% log params, type: 'request-params' %}
      

        {% log user.id, type: 'debug' %}
      

        {% log context.current_user, type: 'error' %}
      

print

Print variable, skipping any additional sanitization. It provides a way to display a variable which consists of both safe and unsafe html.

Params

  • variable - String - Variable containing safe and potentially unsafe HTML

Examples


        {% assign invokable_script = "<script>alert('I will be executed')</script>" %}
{% assign malicious_script = "<script>alert('I should be escaped')</script>" %}
{% capture result %}
  {{ malicious_script }}{{ invokable_script | html_safe }}
{% endcapture %}
{% print result %}
      

response_status

Allows you to set HTTP status for the response. Default 200.

Params

  • status - Int - HTTP status, default 200

Examples


        {% response_status 204 %}
      

        {% response_status 404 %}
      

session

Stores data during user's session Sets field of given name to given value in context.session

Examples


        {% session foo = 'bar' %}
{{ context.session | json }} => { "foo": "bar" }
      

        {% assign bar = "42" }
{% session foo = bar %}
{{ context.session | json }} => { "foo": "42" }
      

        {% session foo = null %}
{{ context.session | json }} => {}
      

        {% session foo = blank %}
{{ context.session | json }} => {}
      

spam_protection

Generates html for spam protection. Supports Google reCAPTCHA strategies: recaptcha_v2, recaptcha_v3

Params

  • strategy - String - name of protection strategy. Default is recaptcha_v2
  • action - String - action name for reCAPTCHA V3, action may only contain alphanumeric characters and slashes

Examples


        {% form %}
  {% spam_protection "recaptcha_v2" %}
{% endform %}
      

        {% form %}
  {% spam_protection "recaptcha_v3", action: "signup" %}
{% endform %}
      

yield

Execute code wrapped inside content_for

Params

  • name - String - name of content_for block

Examples


        Use this in a liquid view first. Then you can use yield inside the layout (for example) or another subsequently rendered view, like below:
{% content_for 'greeting' %}Hello world{% endcontent_for %}

# another_file.liquid
{% yield 'greeting' %}
      

Questions?

We are always happy to help with any questions you may have.