Skip to content

setContextValue(string key, string|int|float|bool value)

The setContextValue method is used to store a scalar value under a given key, so it can later be read with getContextValue. Stored values are shared across every module rendered within the current page render, so a value set by one module instance is visible to any other module instance rendered on the same page — this makes it possible, for example, to avoid rendering the same markup twice for the same product across multiple module instances.

Warning

In the Shoper Visual Editor (SVE), when a module using this method is dragged onto the canvas, its initial render only has context of that module in isolation, not the full page — so setContextValue/getContextValue may see incomplete or incorrect data at that point. Save the layout and refresh the page to see these methods behave correctly with the full page context.

Input parameters

key

string The key to store the value under. If a non-string value is passed, the method does nothing and returns false.

value

string|int|float|bool The value to store. Only scalar values are accepted; passing an array, object or null makes the method do nothing and return false.

Returned value

bool - true if the value was stored, false if key or value failed validation.

Example

source
{% set jsonLdKey = 'product_rating_json_ld_' ~ product.id %}
{% if ObjectApi.getContextValue(jsonLdKey) is null %}
    {% set _ = ObjectApi.setContextValue(jsonLdKey, true) %}
{% endif %}
output