助手可以轻松快速地将代码片段添加到模板中。当你处理更复杂的代码时,我们建议使用助手而不是模板。
¥A helper makes it easy to quickly add snippets to your templates. We recommend using helpers instead of templates when you’re dealing with more complicated code.
无法从 source
文件访问助手。
¥Helpers can not be accessed from source
files.
概要
¥Synopsis
hexo.extend.helper.register(name, function () { |
示例
¥Example
hexo.extend.helper.register("js", function (path) { |
<%- js('script.js') %> |
常见问题
¥FAQ
自定义助手放在哪里?
¥Where to place custom helper?
将其放在 scripts/
或 themes/<yourtheme>/scripts/
文件夹下。
¥Place it under scripts/
or themes/<yourtheme>/scripts/
folder.
如何在自定义助手中使用另一个已注册的助手?
¥How do I use another registered helper in my custom helper?
所有辅助程序都在相同的上下文中执行。例如,在自定义助手中使用 url_for()
:
¥All helpers are executed in the same context. For example, to use url_for()
inside a custom helper:
hexo.extend.helper.register("lorem", function (path) { |
如何在另一个扩展(例如过滤器、注入器)中使用已注册的助手?
¥How do I use a registered helper in another extension (e.g. Filter, Injector)?
hexo.extend.helper.get
将返回辅助函数,但它需要以 hexo 作为其上下文,因此:
¥hexo.extend.helper.get
will return the helper function, but it needs to have hexo as its context, so:
const url_for = hexo.extend.helper.get("url_for").bind(hexo); |