如何检测 Jekyll 标签插件中的当前页面?
我有一个 Jekyll(液体)块插件,我想检测当前页面是什么。我看到上下文被传递到渲染中,并且我可以将当前站点对象检索为 context.registers[:site]。但是,尝试以 context.registers[:page] 形式获取当前页面会失败。
我试图解决的问题是创建一个简单的块插件来检测当前页面是否是标记中提到的页面,并突出显示它。
任何提示将不胜感激。
谢谢!
I have a Jekyll (Liquid) block plugin and I'd like to detect what the current page is. I see that a context is passed into render, and that I can retrieve the current site object as context.registers[:site]. However, attempts to get the current page as context.registers[:page] fail.
The problem I'm trying to solve is to create a simple block plugin to detect if the current page is the page mentioned in the tag, and highlight it.
Any tips would be greatly appreciated.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实证明,我们也可以这样做:
这并不明显,但不需要修补代码。
Turns out we can also do this like:
Which is not obvious but it doesn't require patching the code.
context['page']
似乎返回包含当前页面大部分属性的哈希值,包括url
和path
因此 实际页面对象可以通过以下方式检索
context['page']
seems to return a hash with most of the properties of the current page, includingurl
andpath
so the actual page object can be retrieved with
我认为按原样使用 Jekyll 没有什么好方法。
convertible.rb
仅将site
对象传递给 Liquid,其中不包含任何特定于页面的数据。我建议只编辑
convertible.rb
来传递您需要的数据,向主项目提交拉取请求以拉取您的更改,并在本地使用您的 fork 来生成您的网站。开源万岁!以下简单的补丁适用于我在本地针对 Jekyll 0.11.0 的情况,使页面哈希在 Liquid 中可用
context.registers[:page]
(注意:此时它是一个预先转换的哈希,因此您将访问context.registers[:page]['url']
,而不是context.registers[:page].url
):希望有帮助!
I don't think there's a good way of doing it with Jekyll as-is.
convertible.rb
only passes thesite
object to Liquid, which doesn't contain any page-specific data.I'd suggest just editing
convertible.rb
to pass in the data you need, submitting a pull request to the main project to pull in your changes, and using your fork locally to generate your site. Hooray for open source!The following trivial patch works for me locally against Jekyll 0.11.0, making a page hash available in Liquid as
context.registers[:page]
(note: it's a pre-converted hash at this point, so you'd accesscontext.registers[:page]['url']
, notcontext.registers[:page].url
):Hope that helps!
当制作需要以稍微不同的方式显示当前页面的菜单时,总是会出现此问题。这是我为 Bootstrap 5 编写的 Jekyll 插件:
This issue always arises when making menu that needs to display the current page a little differently. Here is a Jekyll plugin I wrote that does that for Bootstrap 5: