Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what's the difference between .js and .js.liquid in shopify?

Tags:

shopify

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);
like image 722
guest Avatar asked Sep 22 '14 21:09

guest


People also ask

What is js liquid?

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.

Is .liquid the same as javascript?

You can use liquid in . js. liquid files, but . js files are javascript only.

How do you put liquid in a javascript file?

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.


1 Answers

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.

like image 145
Steph Sharp Avatar answered Sep 24 '22 03:09

Steph Sharp