我有一个 Jekyll (Liquid) 块插件,我想检测当前页面是什么。我看到一个上下文被传递到渲染中,并且我可以将当前站点对象检索为 context.registers[:site]。但是,尝试将当前页面作为 context.registers[:page] 获取失败。
我要解决的问题是创建一个简单的块插件来检测当前页面是否是标签中提到的页面,并突出显示它。
任何提示将非常感谢。
谢谢!
原来我们也可以这样做:
def render(context)
page_url = context.environments.first["page"]["url"]
这并不明显,但不需要修补代码。
context['page']
似乎返回具有当前页面大部分属性的哈希,包括url
和path
因此可以使用检索实际的页面对象
context.registers[:site].pages.detect { |p| p.path==context['page']['path'] }
我认为 Jekyll 没有一个好的方法。convertible.rb
只将site
对象传递给 Liquid,它不包含任何特定于页面的数据。
我建议只进行编辑convertible.rb
以传递您需要的数据,向主项目提交拉取请求以提取您的更改,并在本地使用您的 fork 来生成您的站点。开源万岁!
以下琐碎的补丁适用于我在本地针对 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
希望有帮助!
当制作需要以不同方式显示当前页面的菜单时,总是会出现这个问题。这是我为 Bootstrap 5 编写的 Jekyll 插件:
# 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)