我做了一个简单的Grav插件来添加一些用户信息! 我想在模板.html.twig上制作新的蓝图
这是一个插件配置 yaml 文件:
enabled: false
authors:
-
name: Author 1
desc: Description 1
-
name: Author 2
desc: Description 2
custom_file:
这是一个蓝图:
header.author:
type: select
label: Author
classes: fancy
data-options@: 'GravPluginAuthorsPlugin::getAuthors'
我在插件 php 文件中有这个:
public static function getAuthors() {
$author_name = $this->grav['config']->get('plugins.authors.name');
}
我收到错误: 不在对象上下文中使用$this
对此有什么解决方案吗?谢谢!
问题是因为你的函数是static
的,你的类还没有被初始化(没有$this
或$this->grav
在创建类的实例时在构造函数中设置)。
在没有看到整个班级的情况下,希望这足以引导您朝着正确的方向前进......
导入 php 文件顶部的 Grav 类(如果还没有),
use GravCommonGrav;
然后修改函数以调用Grav::instance()
而不是$this->grav
:
$author_name = Grav::instance()['config']->get('plugins.authors.name');
此instance()
函数创建获取配置所需的 Grav 实例。