using Liquid variables inside of a liquid tag call

后端 未结 4 987
鱼传尺愫
鱼传尺愫 2020-12-01 21:08

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\' }} //          


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-01 21:43

    This solved the case for me context[@markup.strip].

    My problem was that i wanted to be able to pass a variable to my custom Liquid tag like this: {% get_menu main_menu navigation.html settings.theme.id %}

    In order to do this i first split the variable string into different varaibles on every space character.

    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)
    

    *This is just a more rich example that the answer above by Jonathan Julian

提交回复
热议问题