2011-10-27 22 views
15

の内部の液体の変数を使用して、私は液体でカスタムリンクタグを作り、私はそう液体タグコール

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

のようにそのタグの呼び出しに液体変数を渡すことができるようにしようとしていますが、これは、その結果アーティクルパラメータは、その上にあるassignステートメントに従って 'something'ではなく 'id'として渡されます。

誰かがタグ呼び出しに変数を渡す方法を知っていますか?このようには見えません

答えて

2

は、私の解決策だけでタグの中に変数名を渡すと、タグがでレンダリングされている文脈からそれをつかむことだった、可能であるようなので:。

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

(凝縮)自分のタグコードで:

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

はどのようにこれをテストしますか? – mhenrixon

5

私は最近、tagパラメータとして変数のを渡すことで、非常に簡単ジキル0.11.2および液体2.3.0でこれを解決してきました。

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

上記のarticleのようにループ内にあるコントロールvarの名前を渡すこともできます。

Liquid::Tag.initializeでは、@markupが2番目のパラメータであり、タグ名の後の文字列です。割り当てられた変数は、contextの最上位レベルで使用できます。

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

これは明らかに1つのパラメータのみを渡すことができます。より複雑なソリューションは、x: 2, y: 3のようなパラメータを解析します。

3

このケースを解決しましたcontext[@markup.strip]この私が最初にすべてのスペース文字に異なるvaraiblesに変数文字列を分割を行うために{% get_menu main_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) 

*これはジョナサンジュリアン

0

ことによって、上記の答えは

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

のようなリテラルと変数を使用して呼び出すことができ、タグを持っているのは素晴らしいことだろうとちょうどより多くの豊富な例でありますまたは

{% link_to_article 'art' %} 

または

私は

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 

そして、ちょうどリテラルまたは変数の値を取得するためにヘルパー関数を呼び出して、レンダリングにヘルパー関数を提案するために

{% link_to_article "art" %} 

ももちろん

{% link_to_article include.article %} 

。 FYI

def render(context) 
    v = get_value(context, @markup.strip) 
end 

、初期化子は、次のようになります。

def initialize(tag_name, markup, tokens) 
    @markup = markup 
    super 
end 
関連する問題