首页 > 解决方案 > 在 Liquid 标签调用中使用变量而不是字符串

问题描述

我已经检查过这个解决方案,它似乎对我的问题不起作用。我在将post.image变量名称传递给 tag 时遇到问题responsive_image。如果我像这样传递字符串,{% responsive_image path: assets/img/ar-7.jpg %}它可以正常工作,但我没有找到如何将变量传递给它的方法。有任何想法吗?

1)我认为这会起作用,不幸post.image的是传递的是字符串而不是变量。注释代码是我需要更改为响应式图像的工作示例。

 {% if post.image %}
  {% responsive_image path: post.image %}
  <!-- <img class="has-ratio" src="{{post.image}}" /> -->
 {% endif %}
Invalid image path specified: "post.image" 
  Liquid Exception: unable to open image `/Users/.../Documents/Apps/Jekyll/wtc-mbp/post.image': No such file or directory @ error/blob.c/OpenBlob/2881 in .html

2)这个答案的解决方案,不起作用

 {% if post.image %}
  {% assign path = post.image %}
  {% responsive_image path %}
 {% endif %}
Invalid image path specified: nil 
  Liquid Exception: no decode delegate for this image format `' @ error/constitute.c/ReadImage/566 in .html

3)另一个想法也行不通

 {% if post.image %}
  {% assign path = post.image %}
  {% responsive_image path: path %}
 {% endif %}
Invalid image path specified: "path" 
Liquid Exception: unable to open image `/Users/.../Documents/Apps/Jekyll/wtc-mbp/path': No such file or directory @ error/blob.c/OpenBlob/2881 in .html

标签: jekyllgithub-pagesliquid

解决方案


要使用 Liquid 变量,您需要选择responsive_image_block标签:

{% responsive_image_block %}
  path: {{ post.image }}
{% endresponsive_image_block %}

推荐阅读