2

I'm working on a JS mod for Shopify. I have been told that Liquid renders first, then JS.

With that in mind, I'm a bit confused as to how to implement a piece of logic.

I want to check if the customer object is present (logged in) and if so I want to set a JS variable customerID to the value of the customer's ID attribute.

Currently, I'm working with

{% if customer %}

    <script>
    var sasCustomerId = {{ customer.id }};
    </script>

{% endif %}

What I don't understand is how this can render properly. If the Liquid goes first, that would leave

{% if customer %}

    {{ customer.id }}

{% endif %}

Which seems strange. Is this an instantaneous thing where it happens "first" but is instantaneous from the user's point of view?

Sorry if this is a dumb question. Not a Compsci guy myself though I'd love to learn more about how all these scripts I write actually work if anyone would care to educate me.

Thanks!

1 Answer 1

1

It works like PHP, the liquid condition is going to be evaluated, if it is true, then your JavaScript is rendered, e.g.:

<script>
var sasCustomerId = 123;
</script>

If not, then nothing is going to be rendered.

Sign up to request clarification or add additional context in comments.

2 Comments

Makes sense. I guess it's more like order of operations?
Yes, your whole code is only executed after the liquid rendered completly.

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.