Snippets
Snippets are smaller custom pieces of Jinja, code, text, or HTML that can be saved and reused across campaigns. Their use can range from saving frequently used Jinja macros, to saving snippets of text, such as the unsubscription sentence that you use in all your SMS campaigns. Similar to HTML blocks or Custom rows, all Snippets are saved within the Asset Manager.
Snippet support
Snippets are supported within:
- Email campaigns
- Scenarios (SMS content, webhooks, browser and mobile push content, wait node)
- Blocks
- Custom rows
- Weblayers
Snippets are not supported in:
- In-app messages
- Experiments
- Tags
- Consent page
We will be adding support for all Campaign types in the future.
Create a snippet
You can create a new snippet in 3 ways:
- From the Asset Manager > Snippets by clicking New snippet at the top of the screen.
- From the your campaign or email node by clicking the plus button on the bottom of the editor or node modal, selecting snippet in the asset selector and choosing the New snippet option.
- From the predefined templates by using the Save as copy option.
All cases open a separate snippet builder window where you can create and preview the snippet.
It is possible to create constant or non-constant parameters of various types to be used within the snippets, and to define the parameter values upon using the snippet in a campaign. This is covered in more detail in the 'Using parameters' section.
Upon saving, all snippets are stored within the project templates, and can be accessed from the Asset Manager, section Snippets.
There are 2 types of Snippet templates:
- Predefined templates are created by Bloomreach and cover many different use cases. These are not stored in your project but loaded in real time from a central repository. This means they cannot be edited directly but can be saved as copy into the project templates and then further edited using the “Save as copy” option.
- Project templates are any custom snippets you created or copied and changed from predefined templates.
Predefined templates
Users can also use a predefined template from the campaign asset picker.
In case the template contains parameters, the user must first save the snippet into the project templates via Save as copy and then select the project copy for use in the campaign. To reference a snippet in a campaign it must have a project copy.
In case the template does not contain parameters, the user will have 2 options:
- Save as copy and then use the project template copy
- Copy as code which will copy the code of the predefined template into the clipboard.
Snippet editor
Regardless of the way you go about creating a new snippet, clicking on the New Snippet button will open up a code editor with a Parameters tab on the right. The editor is where you will write the content of the snippet. That can be anything, from plain text to Jinja macros or HTML code.
At any point, you can preview the snippet by clicking on the Test tab. This will allow you to select a specific customer to preview the snippet on, and specify the available parameters.
Use parameters
It is possible to create parameters to be used in the snippet. Parameters are like variable values that make it possible to insert a custom value within, for example, each email separately. This will prompt the option to choose the value of the parameter when the snippet is selected within a campaign.
Parameters can be created using the parameters editor by clicking Add parameter or can be defined directly in the code and created using the button Load parameters from the code editor.
Each parameter can be also assigned to the category. Categories should help to better organize the parameters within the snippet. Category can be created using the parameters editor by clicking Add category. Both parameters and categories also support drag and drop functionality.
You can define following attributes of a parameter:
Parameter reference-> name of the reference of the parameter to be used in the code, The parameters display name is derived from it (similar to template parameters)
- The derived parameter name will automatically split words if camel-back notation is used or word connected with underscore
reference paramName > Param Name
reference param_name > Param Name
Parameter type > data type of the parameter - Available data types:
- string (default) – a simple one-line text, type returned is string
- text – multi-line text, type returned is string
- number – type returned is integer or float
- boolean – type returned is True/False
- list – type accepted/returned is Jinja list type. For example: [1,2,3]
- object – type accepted/returned is Jinja dict type. For example: {"key": value}
- datetime – datetime picker, value returned is timestamp as number
- date – date picker, value returned is timestamp as number with default hours set to 23:59:59 currently FE seems to create timestamp at 23:59:59 (CET)
- time – time picker, value returned is string with the time “12:00”
- color – color picker, accepts color values in HEX, rgb or rgba format, type returned is string
- enum – selector for defined string values
- image – image picker from the Bloomreach file management, value returned is a string that represents the URL of the image
- recommendation – recommendation picker, value returned is an ID of the selected recommendation model
- metric – metric picker, value returned is an ID of the selected metric
- For most of the data types except (datetime, date, time, and boolean) it is possible to use either constant values or Jinja variables such as {{ customer.first_name }}
- Parameters types are validated for constant values, in case a Jinja variable is used as parameter the type of the parameter is not validated
Default value is the default parameter value. Default value will be used in case no specific parameter value is provided upon snippet usage in campaign (calling the snippet) and to prepopulate the parameters input.
- For most of the data types except (datetime, date, time, and boolean) it is possible to use either constant default values or Jinja variables such as {{ customer.first_name }}.
- If no default value is provided and the parameter is not filled, the Jinja undefined value will be used.
Required field: Define whether a parameter must be filled in when using the snippet. Parameters marked as required must be filled in unless the default value is also used.
Tooltip: Tooltip will be displayed by the parameter name when inputting the parameter values in campaign
Parameters can be used within the code as standard Jinja variables using the keyword params and the defined reference name. For example:
{{ params.parameterName }} or {{ params["parameter name"] }}
{% set otherVariable = params.parameterName %}
{% if param.paramName = "something" %}
…
{% endif %}
Multi words param references can only be used with the params [""] notation.
Calling snippets with parameters:
{{ snippet('<snippet id>', {'parameterName': 'value'}) }}
Use Jinja as a parameter
It is allowed to use simple jinja variables/expressions as parameters of snippets. Parameters can’t contain complicated Jinja expressions within {%... %} or strings containing Jinja within {%... %} _such as _set, if, for, loop.
Contextual variables such as 'event', 'webhook', 'email.view' are not available directly in the snippets but can be used as parameters in campaigns where those variables are available.
Notation
There are 2 types of notation of Jinja variables as parameters depending on their usage.
- When using snippets as the Jinja reference, the variables must be used directly (without {{ }}).
For example:
{{ snippet('<snippet id>', {'purchasePrice': event.total_price }) }}
{{ snippet('<snippet id>', {'finishDate': ((time + 604800) | from_timestamp('%d-%m-%y')) }) }}
{{ snippet('<snippet id>', {'products': [1,2,3] }) }}
Strings containing Jinja personalization must be created using ~, otherwise they would not be rendered:
{{ snippet('<snippet id>', {'mainText': 'Hello ' ~ customer.first_name ~ ', welcome!' }) }}
- When filling in the parameters via the inputs in the asset picker, here you need to use the {{}} notation so we can recognize its a variable and generate the reference correctly, for example, without “”. In the parameters inputs, it's also possible to input a string containing Jinja and it will be automatically translated into the ~ notation. For example, the following parameters inputs:
will be translate into this reference:
{{ snippet('606321bfe04f9d2be61e0812', {'purchasePrice': event.total_price, 'mainText': 'Hello '~ (customer.first_name) ~', welcome!', 'fallbackValue': customer.last_name}) }}
Use snippets in a campaign
Snippets can be accessed from within supported campaigns via the plus button at the bottom right of the screen. This button opens a modal with the asset picker for snippets and blocks.
Clicking on Snippets will load available snippets from the Asset Manager. If the campaign is a part of an initiative, it will only show assets from the same initiative or assets marked as Global objects.
Selecting a snippet will open the snippet preview with a customer selector, snippet description, and the parameters inputs. When entering the parameters, the preview will be regenerated once you stop inputting values (click outside).
Language variants behavior
The language variant of the snippet will be selected based on the selected main campaign template language version. To illustrate here is how following situations would be solved:
Template with language variant
We have 3 assets: campaign template with sk variant, block with no language variant, and snippet with sk variant and a customer with sk language:
- template (sk) > using block (default) > using snippet (sk)
- template (sk) > using snippet(sk)
Template with no language variant
We have 3 assets: campaign template with no language variant, block with no language variant, and snippet with sk variant and a customer with sk language:
- template (default) > block (default) > snippet (default)
- template (default) > snippet(default)
The language variant of the snippet will be selected based on the selected main campaign template language version.
Insert options
A snippet can be inserted into a campaign by clicking the plus button on the bottom of the editor or action node modal and selecting a snippet in the asset selector. Chosen snippets will be displayed in preview with the option to specify the parameters.
After selecting and configuring the snippet, there are 2 ways of inserting the snippet into your campaign:
- Copy as a reference: Creates a Jinja reference with the inserted parameters and copy it onto the clipboard.
- Copy as code: Copies the full content of the snippet into clipboard.
Copy as a reference
This will copy the reference to the snippet as a Jinja tag that can be pasted into code. This will not allow changes to the snippet directly in the campaign and will be updated when the snippet is changed.
Referencing a snippet using Jinja:
- Without parameters:
`{{ snippet("<snippet id>") }}`
- With parameters:
`{{ snippet("<snippet id>", {"<param name>": <param value>, "<param name>": <param value> }) }}`
Copy as code
This will take the full snippet content from the default language template and copy it to the clipboard. Paste the code into the desired campaign part. This code can be changed as desired, but will not be updated when the snippet is changed, neither will it consider language variants.
This option is only available for snippets without parameters.
Limitations
- Hard limits:
- Max size 10kB of the template of a snippet per translation.
- Max 30 distinct translations per snippet.
- Max 20 of distinct snippets being used in a single campaign template or block.
- Max 20 distinct parameters can be created per snippet.
- Max length 100 characters of a parameter name or category.
- Max length 1,000 characters for parameter tool-tip.
- Parameters can’t contain complicated Jinja expressions within {%... %} or strings containing Jinja within {%... %} such as set, if, for loop.
- It is not possible to use a snippet inside a snippet.
Updated about 1 month ago