How to pass a variable into a custom tag in Liquid?

后端 未结 3 1619
独厮守ぢ
独厮守ぢ 2021-02-19 11:25

I have written a custom tag in liquid, and I\'d like to pass a variable to it. Liquid tags will turn any parameter into a string.

For example:

{% nav pag         


        
3条回答
  •  太阳男子
    2021-02-19 11:50

    I had a similar problem. I solved it by creating a custom lookup method:

    def look_up(context, name)
      lookup = context
    
      name.split(".").each do |value|
        lookup = lookup[value]
      end
    
      lookup
    end
    

    To use it, create something like this:

    def initialize(tag_name, markup, tokens)
      @markup = markup
      super
    end
    
    def render(context)
      output = super
      if @markup =~ /([\w]+(\.[\w]+)*)/i
        @myvalue = look_up(context, $1)
      end
    
      do_something_with(@myvalue)
    end 
    

提交回复
热议问题