自定义链接的液体标签

时间:2016-05-03 06:50:30

标签: html module tags jekyll liquid

我开始使用Jekyll和Liquid,我遇到了一些麻烦。

假设我想在Jekyll里面做一些制作链接的Liquid标签:写作时

...and according to {% cite my_link %} we have that...

然后Jekyll搜索caption(某些YAML预定义信息)为my_link的帖子,并根据某种样式为其创建链接。要创建这个Liquid标签,我想必须从像

这样的东西开始
module Jekyll
   class Cite < Liquid::Tag
     def render(context)
        caption = ????????
        aux = site.posts | where: "lang", page.lang | where: "caption", "#{@caption}" | first
        return <a href="{{aux.cleanurl}}" class="{{aux.kind}}" title="{{aux.title}}"></a>
    end
  end
end

Liquid::Template.register_tag('cite', Jekyll::Cite)

首先,我不知道如何分配caption=my_link,即提取{% cite my_link %}的第二部分。一旦我们得到它,这段代码会起作用还是错了?

非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

第一个问题“如何分配caption = my_link ”?

您可以阅读Jekyll Tags plugins documentation,更重要的是,Liquid Tag code

在最后一篇参考文献中,您将了解到$(document).on('change','#chkisJointApplication',function(){ if(this.checked) { //loading partial view $('#element').load('/Controller/GetPartialView',function(){ alert("Partial View Loaded"); }); } else $("#element").html(''); //clearing the innerHTML if checkbox is unchecked }) 方法默认情况下为其他方法提供initialize变量。此变量包含传递给标记的参数。

因此,在@markup方法中,您已经有一个包含所需值的render变量。然后,您可以@markup。请注意,这仅适用于一个参数标记。对于多个参数,您必须使用caption = @markup上的正则表达式对各个部分进行排序。

第二个问题:你的代码会起作用吗?

答案是否定的。插件是ruby代码而不是Liquid。

以下是一个可行的示例:

@markup

注意:您必须在module Jekyll class CiteTag < Liquid::Tag def render(context) # @markup is a Liquid token so, we convert it to string # then we remove spaces before and after caption = @markup.to_s.strip site = context.registers[:site] posts = site.collections['posts'].docs page = context.registers[:page] if defined? page['lang'] and !page['lang'].nil? then # if current page has a lang front matter varaible set (or a default) currentLang = page['lang'] elsif defined? site.config['lang'] # if no lang is defined in current page, fallback to site.lang currentLang = site.config['lang'] else # if no site.lang is available we raise an error raise "No default 'lang' option defined in _config.yml" end # create a new array with posts selected on currentLang and caption variable selectedPosts = posts.select do |post| postLang = post.data['lang'] postCaption = post.data['caption'] sameLang = postLang == currentLang sameCaption = postCaption == caption sameLang and sameCaption end # select first post post = selectedPosts.first # print the link link = "<a href=\"#{site.baseurl}#{post.url}\" class=\"#{post.data['kind']}\" title=\"#{post.data['title']}\">#{post.data['title']}</a>" end end end Liquid::Template.register_tag('cite', Jekyll::CiteTag) 中设置lang变量。例如:_config.yml

相关问题