Ruby on rails 3 在液体标记调用中使用液体变量

Ruby on rails 3 在液体标记调用中使用液体变量,ruby-on-rails-3,template-engine,liquid,Ruby On Rails 3,Template Engine,Liquid,我在Liquid中创建了一个定制的link标记,我正在尝试将Liquid变量传递到该标记的调用中,如下所示 {{ assign id = 'something' }} // this value is actual dynamic while looping through data {% link_to article: id, text: 'Click Me!' %} // my custom tag 但是,根据上面的assign语句,这会导致项目参数作为“id”而不是“somethin

我在Liquid中创建了一个定制的link标记,我正在尝试将Liquid变量传递到该标记的调用中,如下所示

{{ assign id = 'something' }} // this value is actual dynamic while looping through data 
{% link_to article: id, text: 'Click Me!' %} // my custom tag
但是,根据上面的assign语句,这会导致项目参数作为“id”而不是“something”传入


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

看起来这是不可能的,我的解决方案是将变量名传递到标记中,并从标记呈现的上下文中获取它。像这样:

{% for article in category.articles %}
  {% link_to variable: article, text: title %}
{% endfor %}
在我的标记代码中(压缩):

def呈现(上下文)
uri=“article/#{context[@options[:variable]['id']}”
""
结束

我最近用Jekyll 0.11.2和Liquid 2.3.0非常简单地解决了这个问题,将变量名作为标记参数传递

{% assign v = 'art' %}
{% link_to_article v %}
您还可以在循环中传递控制变量的名称,如上面的
article

Liquid::Tag.initialize
中,
@markup
是第二个参数,即标记名后面的字符串。分配的变量可在
上下文的顶层中使用

def render(context)
  "/#{context[@markup.strip]}/"
end

这显然只允许传递一个参数。更复杂的解决方案是解析参数,如
x:2,y:3

这解决了我
上下文[@markup.strip]
的问题

我的问题是,我希望能够像这样将一个变量传递给我的自定义液体标记:
{%get\u menu main\u menu navigation.html settings.theme.id%}

为此,我首先将变量字符串拆分为每个空格字符上的不同变量

class GetMenu < Liquid::Tag
    include ApplicationHelper
    def initialize(tag_name, variables, tokens)

        @variables = variables.split(" ")

        @menu_object = @variables[0]
        @file_name = @variables[1]
        @theme_id = @variables[2]

        super
    end

    def render(context)

        # This is where i use context[@theme_id.strip] to get the variable of "settings.theme.id"
        content = CodeFile.find_by(hierarchy: 'snippet', name: @file_name.to_s, theme_id: context[@theme_id.strip])

        @menu ||= Menu.find_by_slug(@menu_object)

        context.merge('menu' => @menu)

        Liquid::Template.parse(content.code).render(context)

    end

end

Liquid::Template.register_tag('get_menu', GetMenu)
class-GetMenu@menu)
Liquid::Template.parse(content.code).render(context)
结束
结束
Liquid::Template.register_标签('get_menu',GetMenu)

*这只是一个更丰富的例子,上面乔纳森·朱利安(Jonathan Julian)的回答是这样的:最好有一个可以用文字和变量调用的标记,比如

{% assign v = 'art' %}
{% link_to_article v %}

当然还有

{% link_to_article include.article %}
为了做到这一点,我提出了一个助手函数

def get_value(context, expression)
  if (expression[0]=='"' and expression[-1]=='"') or (expression[0]=="'" and expression[-1]=="'")
    # it is a literal
    return expression[1..-2]
  else
    # it is a variable
    lookup_path = expression.split('.')
    result = context
    puts lookup_path
    lookup_path.each do |variable|
      result = result[variable] if result
    end
    return result
  end
end
在渲染中,只需调用helper函数来获取文本或变量的值

def render(context)
  v = get_value(context, @markup.strip)
end
仅供参考,初始化器如下所示:

def initialize(tag_name, markup, tokens)
  @markup = markup
  super
end

如果到目前为止我只在Shopify平台上编辑了liquid,你能告诉我实现你的助手功能的快捷方式吗?
def get_value(context, expression)
  if (expression[0]=='"' and expression[-1]=='"') or (expression[0]=="'" and expression[-1]=="'")
    # it is a literal
    return expression[1..-2]
  else
    # it is a variable
    lookup_path = expression.split('.')
    result = context
    puts lookup_path
    lookup_path.each do |variable|
      result = result[variable] if result
    end
    return result
  end
end
def render(context)
  v = get_value(context, @markup.strip)
end
def initialize(tag_name, markup, tokens)
  @markup = markup
  super
end