In the assets section, there are files with .js
and .js.liquid
names.
What is the difference between the 2, which one should I use if I want to include a js file into a theme?
This is inside a .js.liquid file right now. I would like to construct a string made up of liquid variable. when I append this string inside a DOM element, it doesn't seem to show up.
var someString = '';
{%for line in order.line_items%}
someString += 'q='+{{line.quantity}}+'&p='+{{line.product.id}}+'&pp='+{{line.price}}+'&w='+{{line.grams}}+'&i='+{{line.product.featured_image|product_img_url|url_param_escape}}+'&n='+{{line.product.title|url_param_escape}}+'&';
{%endfor%}
$('body').append(someString);
Liquid is a JavaScript template engine. It was created by Shopify. Liquid files have the extension of . liquid ; they are a mix of static data such as HTML and Liquid constructs.
You can use liquid in . js. liquid files, but . js files are javascript only.
liquid file will only accept translation string as var in Liquid (example: {{ "my_text" | t }}. If you want to use other kind of Liquid strings in Javascript code, you may use a section and include your javascript inside {% javascript %} tags. This will also work in a snippet or a template.
You can use liquid in .js.liquid
files, but .js
files are javascript only.
If you're including a regular js file, use the .js
extension. If you've got liquid in the file as well, you'll need to use .js.liquid
.
EDIT:
I don't think order
can be accessed in a .js.liquid
file. See here:
The order object can be accessed in order email templates, the Thank You page of the checkout, as well as in apps such as Order Printer.
Also see this discussion on the Shopify forums:
You can only use Liquid filters and the global settings object in liquidified assets.
It works like this: whenever you edit a .css.liquid of a .js.liquid file, or whenever you edit your theme settings, a new .css or .js file is created and then stored on our assets servers. Updating your shop in any other way will not update those files...
Given the above, Shopify cannot expose store information (that is likely to change) in those files.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With