在液体标签调用中使用液体变量 [英] using Liquid variables inside of a liquid tag call

查看:83
本文介绍了在液体标签调用中使用液体变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Liquid中创建了一个自定义链接标签,我试图像这样将液体变量传递给该标签的调用中

I made a custom link tag in Liquid and I am trying to be able to pass liquid variables into the call for that tag like so

{{ assign id = 'something' }} // this value is actual dynamic while looping through data 
{% link_to article: id, text: 'Click Me!' %} // my custom tag

但是,这导致article参数作为"id"而不是根据其上方的assign语句的"something"传递.

However this results in the article parameter being passed in as 'id' instead of 'something' as per the assign statement above it.

有人知道如何将变量传递给标记调用吗?

Does anyone know how to pass variables into tag calls?

推荐答案

看起来不太可能,我的解决方案是将变量名传递给标签,并从标签所处的上下文中获取变量名.呈现.像这样:

Doesn't look like this is possible, my solution was to just pass the variable name in to the tag and grab it out of the context the tag is being rendered in. Like so:

{% for article in category.articles %}
  {% link_to variable: article, text: title %}
{% endfor %}

在我的代码中(压缩):

in my tag code (condensed):

def render(context)
  uri = "article/#{context[@options[:variable]]['id']}"
  "<a href='#{uri}'>#{build_link_text context}</a>"
end

这篇关于在液体标签调用中使用液体变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆