Replace values in Liquid from posted values
When you have a form that has certain fields to be submitted, these are present in your post request you can use these values to construct a personelized message. I made this include that replaces specific named tokens with their value.
{%- include "includes/tokenized_message", text_to_be_tokenized: object.text -%}
The text_to_be_tokenized
is the input for this include. It expects a string and then creates an array by splitting the message on [
first.
With this you get an array of strings that are split on [
. Then you loop over this array and split on ]
. If the split is NAME
you replace it with the value of name
.
Awesome right?
{%- capture message -%}
{%- assign messageToBeTokenized = text_to_be_tokenized | split: '[' -%}
{%- for messagePart in messageToBeTokenized-%}
{%- assign mesSplit = messagePart | split: ']' -%}
{%- for split in mesSplit-%}
{%- if split == "NAME" -%}
{{ name }}
{%- else -%}
{{split}}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- endcapture -%}
{{ message }}
In this example you have split == "NAME"
which now responds with tokens in text inputs like so:
Dear email person with name: [NAME],
Thanks you fur spamming me!
Kind regards, Wonky.
This include then do some splitting magic and the result is tokenized.
Dear email person with name: replaced value with name.
This can be used for using form input and then give user a confirmation email with their name in it. Or any other value you want to replace in a message.