567 votes
1 answers
How to use javascript variables in shopify's liquid? like vue.js
some.liquid
<div>this is {{year}}</div>
<script>
var year = ''
if (location.hostname == 'stackoverflow.com') {
year = 'good'
} else {
year = 'nood'
}
</script>
Obviously, the idea above cannot be implemented, so what should I do?
Undefined asked
739
votes
Answer
Solution:
The reason why it can't work that way is that Shopify doesn't execute JavaScript on their server when parsing liquid file. So you want to manually modify that element using JavaScript
<div id="my-element"></div>
<script>
const myElement = document.getElementById('my-element');
var year = ''
if (location.hostname == 'stackoverflow.com') {
year = 'good'
} else {
year = 'nood'
}
myElement.innerText = `This is ${year}`
</script>
Undefined answered
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.