【问题标题】:Jekyll custom plugin ignored in post.html page在 post.html 页面中忽略 Jekyll 自定义插件
【发布时间】:2020-07-03 10:00:24
【问题描述】:

我创建了一个 Jekyll 标签插件,它可以在除 _layout/post.html 之外的所有模板(index/about/contact/default/page)上正常工作。我尝试在 Stackoverflow 和 Google 上寻找解决方案,但还没有找到。

我创建了一个新的“产品”集合,其中调用了我的自定义 Liquid-tag 插件以在 post.html 上呈现 HTML 代码块。这是插件,

module Jekyll
    class AmazonAffiateHelper < Liquid::Tag

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

        def render(context)
            input_split = split_params(@input)

            output = ""
            if (input_split.length > 0)
                href = input_split[0].strip
                src = input_split[1].strip
                src2 = input_split[2].strip
                href2 = input_split[3].strip

                output = "<a href=\"#{href}\" target=\"_blank\"><img border=\"0\" src=\"#{src}\""
                output += " width=\"150\" class=\"center-block amazon-fix \"></a>"
                output += " <img src=\"#{src2}\" width=\"1\" height=\"1\" border=\"0\""
                output += " alt=\"\" style=\"border:none !important; margin:0px !important;\"/>"
                output += " <br><br><a href=\"#{href2}\" class=\"btn btn-amazon amazon-fix\">"
                output += "BUY ON AMAZON</a>"
            end

            return output
        end

        def split_params(params)
            return params.split("|")
        end
    end
end

Liquid::Template.register_tag('amazon', Jekyll::AmazonAffiateHelper)

例如,结果输出是这样的,

Here's is a book you might like, {% amazon variable|variable2|variable3|variable4 %}

我没有从bundler exec jekyll server --trace得到任何有用的东西

我错过了什么?提前致谢

PS:我不是程序员,但我正在学习。

【问题讨论】:

    标签: ruby jekyll


    【解决方案1】:

    我已经设法通过创建一个新的 jekyll-block 插件来解决它。不知道为什么我首先需要这样做,但它有效,解决方案如下 - 我相信一定有更好的方法。

    module Jekyll 
        class RecommendationBlock < Liquid::Block 
            def initialize(tag_name, markup, tokens) 
                super 
                @input = markup 
            end 
            
            def render(context) 
                contents = super 
                # very important for this solution
                content = Liquid::Template.parse(contents).render context 
                output = content 
                return output 
            end 
        end 
    end 
    
    Liquid::Template.register_tag("recommendation", Jekyll::RecommendationBlock)
    

    然后,在 post.html 上,我循环浏览产品集合

    {% for product in site.products limit 5}
        # product.content has the custom amazon tag {% amazon variable1|variable2...%}, which is cause the problem
        {% recommendation recom }{{ product }}{% endrecommendation %}
    {% endfor}
    

    如果遇到同样的问题,我希望它对某人有所帮助。

    谢谢大家。

    【讨论】:

      猜你喜欢
      • 2015-06-09
      • 2016-07-31
      • 2011-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-07
      • 2011-04-17
      • 2012-07-22
      相关资源
      最近更新 更多