2

First of all I don't have the abillity to create custom functions of somekind. My question need to be done with the frontend options of Twig. And yes I'm pretty new to Twig :)

I'm trying to add some Twig variables to a javascript variable. That javascript variable is used to translate text to different languages.

The end result need to be:

var ajaxTranslations = {{ 'Free shipping; In stock; Natural; Juice;' | t_json | raw }}; 
// so Natural and Juice need to added in the exact same way as above

So I have a Twig variable that have some text in it like so:

{{ product_usp }} // results in -> Natural, Juice

So first I want to split that string (after each comma) into two seperate values. Next I want to create an array with those values and add them to that javascript variable. That's where my problems start.

How do you add tusp to that javascript variable?

What I did is this:

 {% set tusp = [] %}
  {% set usps = theme.product_usp | split(',') %}

  {% for usp in usps %}
   {% set tusp = usp ~ ';' %}
  {% endfor %}

<script>
    var ajaxTranslations = {{ 'Free shipping; In stock; ~ tusp' | t_json | raw }};
<script>

This results in empty value or ~ tusp is seen as text instead of a value.

Any help greatly appreciated.

3
  • What is t_json? is that a custom filter? Commented Jun 29, 2017 at 16:44
  • @AlvinBunk Could be but the tusp variable is not concatenated or interpolated correctly. Commented Jun 29, 2017 at 16:49
  • If you found this answer useful please vote for the answer below, thanks! Commented Jun 29, 2017 at 17:09

1 Answer 1

2

When you use the tilde operator it must be located in between a string(s) and/or a variable(s):

<script>
    var ajaxTranslations = "{{ 'Free shipping; In stock; ' ~ tusp | raw }}";
<script>

Or you can use "string interpolation":

<script>
    var ajaxTranslations = "{{ "Free shipping; In stock; #{tusp}" | raw }}";
<script>
Sign up to request clarification or add additional context in comments.

4 Comments

Do note you will need to wrap the statement in parantheses or the filter t_json will only be applied to the variable tusp
@Michael Nino; Those both options is something I've tried. Both give a result like this: {&quot;&quot or #(tusp}. Do you have an idea why that is?
If you would like to assign a string to a JavaScript variable you might do the following: var ajaxTranslations = "{{ 'Free shipping; In stock; ' ~ tusp | raw }}"; instead. I don't believe you need to use a filter e.g., t_json. I am assuming this filter is a custom filter you are someone has defined for you.
Here is the Twig Documentation for encoding to JSON: See json_encode. You don't need to json_encode since you are assigning a string value to a JavaScript variable. However, you should read the documentation if you are interested in assigning arrays to a JavaScript variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.