如何在Jekyll标签插件中检测当前页面?

7
我有一个Jekyll(Liquid)块插件,我想检测当前页面是什么。我发现在render中传递了一个context,并且我可以将当前站点对象作为context.registers[:site]检索出来。 但是,尝试将当前页面作为context.registers[:page]获取失败。
我正在尝试解决的问题是创建一个简单的块插件,以检测当前页面是否是标记中提到的页面,并对其进行突出显示。
如果您有任何提示,将不胜感激。
谢谢!
4个回答

17

原来我们还可以这样做:

  def render(context)
    page_url = context.environments.first["page"]["url"]

这不是显而易见的,但它并不需要修改代码。


太棒了。而且完全不直观。感谢你找出来。 - cboettig
2
但是如果您需要在“initialize”时获取“context”对象呢? - jackyalcine
context.environments.first["page"]["path"] can be expressed more succinctly as context['page']['path'] - Mike Slinn

3

context['page'] 似乎返回一个哈希值,其中包括当前页面的大部分属性,包括 urlpath

因此,可以使用以下代码获取实际的页面对象:

context.registers[:site].pages.detect { |p| p.path==context['page']['path'] }

1

我认为使用Jekyll本身没有好的方法。convertible.rb只将site对象传递给Liquid,其中不包含任何特定于页面的数据。

我建议编辑convertible.rb以传递所需的数据,向主项目提交拉取请求以拉取您的更改,并在本地使用您的分支生成站点。开源万岁!

以下微不足道的补丁对我来说在Jekyll 0.11.0中本地有效,使页面哈希在Liquid中可用为context.registers[:page](注意:此时它是预转换的哈希,因此您将访问context.registers[:page]['url']而不是context.registers[:page].url):

diff --git a/lib/jekyll/convertible.rb b/lib/jekyll/convertible.rb
index d33abc5..a674ef5 100644
--- a/lib/jekyll/convertible.rb
+++ b/lib/jekyll/convertible.rb
@@ -69,7 +69,7 @@ module Jekyll
     #
     # Returns nothing.
     def do_layout(payload, layouts)
-      info = { :filters => [Jekyll::Filters], :registers => { :site => self.site } }
+      info = { :filters => [Jekyll::Filters], :registers => { :site => self.site, :page => payload['page'] } }

       # render and transform content (this becomes the final content of the object)
       payload["pygments_prefix"] = converter.pygments_prefix

希望这能有所帮助!

1

当制作菜单需要稍微不同地显示当前页面时,这个问题总是会出现。我编写了一个Jekyll插件,用于为Bootstrap 5执行此操作:

# Copyright 2020 Michael Slinn
#
# Apache 2 License
# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.

module Jekyll
  # Generates a Bootstrap 5 nav item.
  # 
  # Usage: {% nav_item icon link text %}
  #   Quotes are not used
  #   icon is assumed to be within the /assets/icons directory
  #   link must reference a local web page, do not preface with http: or https:
  #   text can be one or more words
  # 
  # Example:           
  # {% nav_item house-door.svg /index.html Welcome! %}

  class Bootstrap5NavItem < Liquid::Tag

    def initialize(href, command_line, tokens)
      super

      @active = '"'

      tokens = command_line.strip.split(" ")

      @icon = tokens.shift
      @link = tokens.shift
      @text = tokens.join(" ").strip
    end

    def render(context)
      relative_link = @link.delete_prefix('/')
      page = context['page']['path']  # relative to site root
      #puts "******* page=#{page}; @link=#{@link}"

      if page == relative_link then
        %Q(<li>
          <a class="nav-link active" aria-current="page" href="#">
            <img class="feather" src="/assets/icons/#{@icon}">
            #{@text}
          </a>
        </li>)
      else
        %Q(<li>
          <a class="nav-link" href="#{@link}">
            <img class="feather" src="/assets/icons/#{@icon}">
            #{@text}
          </a>
        </li>)
      end
    end
  end
end

Liquid::Template.register_tag('nav_item', Jekyll::Bootstrap5NavItem)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接