Using multiple templates in a single snippet

When you have multiple message templates, it's usually easiest to have one snippet for each template.

However, there are many other ways to do this. In this post, I'll be sharing with you how to handle multiple templates in one snippet, by using keyed lists.

The first step is to create a keyed list with a title and content for each template. A keyed list is a list where each item as a "key" and a corresponding "value".

So in our case, the "key" will be the title, and the "value" will be the content.

{myList=[
"Thank you"="Thank you very much for your order",
"Help?"="Do you need any help with this?",
"Dispatched"="Your order has been dispatched",
"Refund"="Your order has been refunded"]}

I've put each item on a separate line for the sake of clarity, however this isn't necessary.

Looking at the snippet above, you can see how each item in the list has a key and a value.

So:
Key: "Thank you"
Value: "Thank you very much for your order"

...and so on.

In the next step, I'm going to add a {formmenu} command that will use the keys in the myList list as its options.

{myList=[
"Thank you"="Thank you very much for your order",
"Help?"="Do you need any help with this?",
"Dispatched"="Your order has been dispatched",
"Refund"="Your order has been refunded"]}

Title: {formmenu: name=title; values={=keys(myList)}}

Notice how the {formmenu} command contains the keys of myList. This is done with this portion of the command:
values={=keys(myList)}

Next, I want the snippet to give me the value corresponding to the key I choose in my {formmenu}.

Here's how it's done:

{myList=[
"Thank you"="Thank you very much for your order",
"Help?"="Do you need any help with this?",
"Dispatched"="Your order has been dispatched",
"Refund"="Your order has been refunded"]}

Title: {formmenu: name=title; values={=keys(myList)}}
Content: {=myList[title]}

The final line tells Text Blaze to give the value corresponding to the item chosen in the {formmenu}, which is referenced by the name title.

This is a pretty advanced way of doing things, but I'm hoping it will help jog your creativity and give you a few ideas of what's possible with list functions.

Got any ideas you want to implement? Reply to the thread and we can work on them together :slight_smile:

2 Likes

On an additional note, the keys and values in a list can also contain variables.

{formtext: name=firstname; default=Puss in Boots}
{myList=[
"Thank you"="Thank you very much for your order, "&firstname&".",
"Help?"="Do you need any help with this, "&firstname&"?",
"Dispatched"="Your order has been dispatched, "&firstname&".",
"Refund"="Your order has been refunded, "&firstname&"."]}

Title: {formmenu: name=title; values={=keys(myList)}}
Content: {=myList[title]}

1 Like

Nicely illustrated Cedric

This will help simplify some of my more convoluted snippets and increase speed of execution.

On the question of improving speed of large complex snippets that import many other snippets, I have utilized the utilized value fields such as:
{options=["option1", "option2", "option3"]}
{formmenu: name=menu; values={=options}}
{=menu}
where I can use {=menu} multiple time in the same snippet effectively (as long as the snippet is imported into the larger snippet).

The difficulty arises when using lists with multiple options and worse still using an Other... option
eg
{multipleoptions=["multipleoption1", "multipleoption2", "multipleoption3", "Other..."]}
{formmenu: name=menu2; values={=multipleoptions}; multiple=yes}
{if: count(menu2) > 0; trim=left}
Menu2: {repeat: for i in seq(1, count(menu2))}
{if: menu2[i] == "Other..."}{formtext: name=Please specify...}{else}{=menu2[i]}
{endif}
{endrepeat}
{endif: trim=left}

{=menu2}
In the second example {=menu2} produces a bizarre output (which I believe you actually have a solution for, but not when the "Other..." is chosen.
More troubling is the elegant snippet you recently developed for diagnosis - how can I place the results/output in 3 different areas of a large snippet - more bizarre is that I imported that snippet 3 times (Diagnosis) into a large report and I thought the output when I entered data into the first import would automatically come across to the other 2 - but it doesn't!!

Can you help - once again.

Gratefully

George

@George_Marinos, can you insert the actual snippet in the forum post please?

I will need to use the contents of =menu2 several times in the same form.

For those who are interested in the solution to what @George_Marinos was asking, please see this snippet: