Using asset_url within a .js.liquid file

蹲街弑〆低调 提交于 2019-12-25 11:34:38

问题


I'm coding a Shopify theme and I need to use Google Maps with custom pointers in one of my templates.

I am having trouble with the asset_url working within a Javascript file so I can link correctly to my new pointer/marker graphics. Any pointers on what I'm doing wrong?

I tried both of the following and neither worked:

var image = {
  url: "url({{ 'gr_gm-pointer.png' | asset_url }})"
  ...etc
};

and

var image = {
  url: url({{ 'gr_gm-pointer.png' | asset_url }})
  ...etc
};

I'm a novice at both javascript and liquid, so any help would be much appreciated. Thank you for your help in advance.


回答1:


Are you sure that asset exists in your theme? All my tests using js.liquid files work fine when referencing an asset like that.




回答2:


Its working only when you write your script in your .liquid file;

<script>
....
....append('<img src=\"{{ 'ajax-loader.gif' | asset_url }}" />');
....
</script>

or there is another solution here;

https://ecommerce.shopify.com/c/ecommerce-design/t/hardcoding-asset-url-s-is-it-safe-31768




回答3:


This is old, but I was struggling with this and found a solution, so I figured I'd post an answer. The google maps marker object has an icon property, you can call it in liquid like this:

var marker = new google.maps.Marker({
      position: latLng,
      map: map,
      icon: '{{ 'your-icon.png' | asset_img_url: '60x' }}'


来源:https://stackoverflow.com/questions/17258334/using-asset-url-within-a-js-liquid-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!