用于自定义链接的 Liquid 标签
Liquid tag for custom linking
我开始使用 Jekyll 和 Liquid,但遇到了一些麻烦。
假设我想在 Jekyll 中做一些 Liquid 标签,使得 links: when write
...and according to {% cite my_link %} we have that...
然后 Jekyll 搜索 post 其 caption
(一些 YAML 预定义信息)是 my_link
并根据某种样式为其创建一个 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 %}
的第二部分。一旦我们得到它,这段代码是否有效?
非常感谢您的帮助
第一个问题“如何分配标题=my_link”?
您可以阅读Jekyll Tags plugins documentation, and more important, Liquid Tag code。
在最后一个参考中,您将了解到 initialize
方法默认情况下使 @markup
变量可用于其他方法。此变量包含传递给您的代码的参数。
因此,在 render
方法中,您已经拥有一个包含所需值的 @markup
变量。然后你可以做caption = @markup
。请注意,这仅适用于一个参数标签。对于多个参数,您将不得不在 @markup
上使用正则表达式来对片段进行排序。
第二个问题:你的代码能用吗?
答案是否定的。插件是 ruby 代码而不是 Liquid。
这是一个可行的示例:
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)
注意:您必须在 _config.yml
中设置一个 lang
变量。例如:lang: 'en'
.
我开始使用 Jekyll 和 Liquid,但遇到了一些麻烦。
假设我想在 Jekyll 中做一些 Liquid 标签,使得 links: when write
...and according to {% cite my_link %} we have that...
然后 Jekyll 搜索 post 其 caption
(一些 YAML 预定义信息)是 my_link
并根据某种样式为其创建一个 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 %}
的第二部分。一旦我们得到它,这段代码是否有效?
非常感谢您的帮助
第一个问题“如何分配标题=my_link”?
您可以阅读Jekyll Tags plugins documentation, and more important, Liquid Tag code。
在最后一个参考中,您将了解到 initialize
方法默认情况下使 @markup
变量可用于其他方法。此变量包含传递给您的代码的参数。
因此,在 render
方法中,您已经拥有一个包含所需值的 @markup
变量。然后你可以做caption = @markup
。请注意,这仅适用于一个参数标签。对于多个参数,您将不得不在 @markup
上使用正则表达式来对片段进行排序。
第二个问题:你的代码能用吗?
答案是否定的。插件是 ruby 代码而不是 Liquid。
这是一个可行的示例:
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)
注意:您必须在 _config.yml
中设置一个 lang
变量。例如:lang: 'en'
.