自定义链接的液体标签 [英] Liquid tag for custom linking

查看:131
本文介绍了自定义链接的液体标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始使用Jekyll和Liquid,并且我有一些麻烦。



假设我想在Jekyll里面做一些Liquid标签,它可以创建链接:当写作时

...并根据{%cite my_link%}我们有...



然后Jekyll搜索其< caption (一些YAML预定义的信息)是 my_link 并根据某种风格创建一个链接。为了创建这个Liquid标签,我想你必须从类似于

 模块Jekyll 
class引用< / p> Liquid :: Tag
def render(context)
caption = ????????
aux = site.posts |其中:lang,page.lang |其中:标题,#{@ caption}|首先
返回< 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%} 的第二部分。

非常感谢您的帮助

解决方案

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



您可以阅读 Jekyll标签插件文档,更重要的是,液体标签代码



在这最后一个参考文献中,您将了解到 initialize 方法默认情况下会制作一个 @markup 其他方法可用的变量。这个变量包含传递给你的标签的参数。

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



第二个问题:你的代码能工作吗?



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



下面是一个可以工作的例子:

 模块Jekyll 
class CiteTag< Liquid :: Tag
def render(context)

#@markup是一个Liquid标记,所以我们将它转​​换为字符串
#然后我们删除$ b $之前和之后的空格b caption = @ markup.to_s.strip
site = context.registers [:site]
posts = site.collections ['posts']。docs
page = context.registers [:page ]

如果已定义? page ['lang']和!page ['lang']。nil?那么
#如果当前页面有一个lang前置变量集合(或默认值)
currentLang = page ['lang']
elsif定义了吗? site.config ['lang']
#如果在当前页面中没有定义lang,则回退到site.lang
currentLang = site.config ['lang']
else
#如果没有site.lang可用,我们提出一个错误
raise在_config.yml中定义的没有默认的'lang'选项
end

#创建一个包含文章的新数组选中currentLang和标题变量
selectedPosts = posts.select do | post |
postLang = post.data ['lang']
postCaption = post.data ['caption']
sameLang = postLang == currentLang
sameCaption = postCaption == caption
sameLang和sameCaption
end

#select first post
post = selectedPosts.first

#打印链接
link = < a href = \#{site.baseurl}#{post.url} \class = \#{post.data ['kind']} \title = \#{post 。数据[ '标题']} \ >#{post.data [ '标题']}< / A> 中

end
end
end

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

注意:您必须在 lang 变量> _config.yml 。例如: lang:'en'


I am starting to use Jekyll and Liquid and I have some trouble about it.

Suppose I want to do some Liquid tag inside Jekyll that makes links: when writing

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

then Jekyll searches the post whose caption (some YAML predefined information) is my_link and creates a link for it according to some style. To create this Liquid tag I suppose one has to start with something like

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)

First of all I do not know how to assign caption=my_link, that is, to extract the second part of {% cite my_link %}. Once we get it, will this code work or is it wrong?

Thank you very much for your help

解决方案

First question "how to assign caption=my_link" ?

You can read Jekyll Tags plugins documentation, and more important, Liquid Tag code.

In this last reference, you will learn that the initialize method is, by default, making a @markup variable available for other methods. This variable contains parameters passed to your tag.

So, once in render method you already have a @markup variable containing the desired value. You can then do caption = @markup. Note that this is only true for one parameter tags. For multiple parameters, you will have to use regular expressions on @markup to sort the pieces.

Second question : Will your code work ?

The answer is NO. Plugins are ruby code not Liquid.

Here's an example that can work :

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)

Note : you have to set a lang variable in _config.yml. eg : lang: 'en'.

这篇关于自定义链接的液体标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆