This feature allows OTRS administrators and OTRS developers with experience using Template Toolkit to write Template Toolkit code snippets in OTRS templates. This can facilitate, for example, the customization of response templates with multiple languages because you can customize the same template for many languages by using conditions without adding completely new response templates. This saves resources within OTRS and gives you a cleaner overview of your response templates.
This feature extends the standard editor by enabling logical if-else conditions as well as loops and much more.
To insert code snippets:
-
Go to the System Configuration screen.
-
Search for the setting
AdvancedEditor
and enable it. -
Open Templates, Signatures or Salutations modules in the administrator interface.
-
Add or modify a resource in these screens.
-
Find the new icon in the Rich Text editor toolbar (the last one).
-
Click on the new icon to open a new window.
-
Select TemplateToolkit from the Language drop-down.
-
Write the following lines into the text box.
[% IF ( matches = Data.CustomerUser.UserLanguage.match('^de') ) %] Sehr geehrte(r) [% Data.CustomerUser.UserFirstname %] [% Data.CustomerUser.UserLastname %],<br /> [% ELSIF ( matches = Data.CustomerUser.UserLanguage.match('^hu') ) %] Tisztelt [% Data.CustomerUser.UserLastname %] [% Data.CustomerUser.UserFirstname %]!<br /> [% ELSE %] Dear [% Data.CustomerUser.UserFirstname %] [% Data.CustomerUser.UserLastname %],<br /> [% END %]
-
Click on the OK button to save the code snippet.
-
Click on the Save or Save and finish button.
-
Add the template, signature or salutation to the queue you want to use it.
-
Create a new ticket, choose a customer user (for this example we will call him John Doe), then choose the queue you have configured your template for.
If the customer user language is German, it should print Sehr geehrte(r) John Doe,. If it is set to Hungarian, you should see Tisztelt Doe John!. If your customer user has no language set or it is set to English, you will see Dear John Doe,.
As you can see, conditions in templates can be very useful, but be careful to write correct Template Toolkit syntax. If you make mistakes there, you will get an error during choosing a template.
See also
For detailed instructions on how to write Template Toolkit code, refer to the Template Toolkit User Manual.
Debugging Templates
Writing Template Toolkit code is not too difficult compared to other languages. Nevertheless errors can occur as we are all just humans.
If you have mistakes in your Template Toolkit syntax, saving a template will bring up an error screen that tell you about what went wrong and where to find the error.
In this example the OTRS system informed us, that there was an unexpected token [
in the line [% IF Data.OTRSTagData.TypeID == 1 [% %]
. A second opening bracket was entered inside the same Template Toolkit tag, which for sure is forbidden.
To fix the error:
-
Go back to the previous page.
-
Edit the template and save it.
-
Repeat these steps until no more errors are found.