js.liquid file imported as asset in theme.liquid not finding liquid global variables

js.liquid file imported as asset in theme.liquid not finding liquid global variables

gwof
Tourist
9 0 3

Hi, 

I have the following file assets/ac.js.liquid with the following code: 

 

{% if customer %}
    vgo('setAccount', '123456789');
   vgo('setTrackByDefault', true);
    vgo('setEmail', '{{ customer.email }}');
    vgo('process');
{% else %}
    vgo('setAccount', '123456789');
    vgo('setTrackByDefault', true); 

    vgo('process');
{% endif %}

 

which I then import either in the head or the bottom of the theme.liquid file:

 

{{ 'ac.js' | asset_url | script_tag }}

 

or

 

<script src="{{ 'ac.js' | asset_url }}" defer="defer"></script>

 

 

The problem is that the `customer` liquid variable is never found (with of course the user being logged in)! If I do the same thing with simply a .liquid (e.g. sections/ac.liquid) file

 

<script>
{% if customer %}
    vgo('setAccount', '123456789');
   vgo('setTrackByDefault', true);
    vgo('setEmail', '{{ customer.email }}');
    vgo('process');
{% else %}
    vgo('setAccount', '123456789');
    vgo('setTrackByDefault', true); 

    vgo('process');
{% endif %}
</script>

 

and import it as a section inside the theme with

 

{% section 'ac' %}

 

this works with no problem. But clearly, it's very hacky to use a liquid file for pure js assets where the whole body is wrapped into a script tag.

 

Why my .js.liquid asset file does not have access to the liquid variables correctly?!

Replies 0 (0)