简体   繁体   English

如何在javascript中包含液体?

[英]How to include liquid inside javascript?

I've been trying all day long to include this liquid code inside javascript with no luck so far.. 我一直在努力将这个液体代码包含在javascript中,但到目前为止还没有运气。

I'm simply trying to update a div with the cart data to show the image and name, this is what I've got. 我只是想用购物车数据来更新div以显示图像和名称,这就是我所拥有的。

$('.openCart').click(function(e)
{          
    $.ajax({
        type: 'POST',
        url: '/cart/add.js',
        data: data,
        dataType: 'json',
        success: function()
        {
          {% capture content %}{% include 'addItemToCartDetails' %}{% endcapture %}
          var content = {{ content | json }};
          $("._second").html(content);
        }
    });
 });

Overall the following code doesn't work(simply because of the for loop, and I have no clue how to get around this..): If I remove the for loop then the code retrieves the divs and everything, besides the item data since the loop isn't working. 总的来说下面的代码不起作用(仅仅因为for循环,我不知道如何绕过这个...):如果我删除for循环然后代码检索div和所有内容,除了项目数据以后循环不起作用。

This is the addItemToCartDetails.liquid , 这是addItemToCartDetails.liquid

{% for item in cart.items %}

    <div class="_second_1">
      <a href="{{ item.url | widivin: collections.all }}" class="cart-image">
        <img width="320" src="{{ item | img_url: '700x700' }}" alt="{{ item.title | escape }}">
      </a>
    </div>

    <div class="_second_2"><h3>Product Name</h3>{{ item.product.title }}</div>

    <div class="_second_3"><h3>Remove Product</h3><span class="clearCart">Remove</span></div>

    <div class="_second_4"><h3>Quantity</h3><input class="quantity" type="input" name="updates[]" id="updates_{{ item.id }}" value="{{ item.quantity }}" min="0" data-id="{{ item.id }}" title="If you'd like another subscription please checkout separately" alt="If you'd like another subscription please checkout separately" disabled></div>

    <div class="_second_5">
      <h3>Total Price</h3>

      <div class="totalDetails">Total: {{ item.line_price | plus: 0 | money }} / month</div>

    </div>

    <div class="_third">
      <input type="submit" name="checkout" value="PROCEED TO CHECKOUT">
    </div>

{% endfor %}

You are trying to use Liquid when you should be using Javascript 当你应该使用Javascript时,你正试图使用​​Liquid

All of the Liquid processing happens on the backend to construct an HTML file that gets passed to the browser. 所有Liquid处理都在后端进行,以构造一个传递给浏览器的HTML文件。 Once the HTML page has been passed to the user's browser, Javascript can be used to manipulate the document and make this appear/disappear/change. 将HTML页面传递到用户的浏览器后,可以使用Javascript来操作文档并使其显示/消失/更改。

Practically, this means that your {% for item in cart.items %} in addItemToCartDetails.liquid will be rendered once, before page load, and never again afterwards. 实际上,这意味着addItemToCartDetails.liquid中的{% for item in cart.items %}将在页面加载之前呈现一次,之后再也不会呈现。 No matter how many items are added to the cart, the results of that snippet will only ever be current as of page load. 无论向购物车添加了多少商品,该代码段的结果在页面加载时都只会是最新的。

You should be using the Javascript variables instead 您应该使用Javascript变量

Shopify passes the line item object representing the most recently-added product to your success callback function. Shopify将表示最近添加的产品的订单项对象传递给您的成功回调函数。 Your code should look something like this: 您的代码应如下所示:

$('.openCart').click(function(e)
{          
    $.ajax({
        type: 'POST',
        url: '/cart/add.js',
        data: data,
        dataType: 'json',
        success: function(line_item)
        {
          var content = '<h3>You added a ' + line_item.title + '!</h3>' +
                        '<p> We appreciate that you want to give us ' + Shopify.formatMoney(line_item.line_price, {{ shop.money_format }}) + '!</p>' + 
                        '<p>That is very nice of you!  Cheers!</p>';
          // Note: Not every theme has a Shopify.formatMoney function.  If you are inside an asset file, you won't have access to {{ shop.money_format }} - you'll have to save that to a global javascript variable in your theme.liquid and then reference that javascript variable in its place instead.
          $("._second").html(content);
        }
    });
 });

If you're curious about what all you can access in the response object, add a console.log(line_item) to the beginning of your success function to print the object to your browser's console. 如果您对响应对象中可以访问的内容感到好奇,请将console.log(line_item)添加到成功函数的开头,以将对象打印到浏览器的控制台。 (In most browsers you can right-click any element on the page and select 'Inspect Element' to bring up your developer tools. There should be a tab called 'Console' where the logs get printed to, and once your information is there you should be able to click on the object to expand its contents.) (在大多数浏览器中,您可以右键单击页面上的任何元素,然后选择“检查元素”以显示开发人员工具。应该有一个名为“控制台”的选项卡,其中会打印日志,一旦您的信息在那里,应该能够点击对象来扩展其内容。)

In your first snippet, pass cart.items as the variable items to the template: 在第一个代码段中,将cart.items作为变量items传递给模板:

{% include 'addItemToCartDetails', items: cart.items %}

In the file addItemToCartDetails.liquid template, modify the for loop statement accordingly: addItemToCartDetails.liquid文件模板中,相应地修改for循环语句:

{% for item in items %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM