在Timber中使用自定义功能

ric*_*nes 1 wordpress timber

我一直在尝试通过使用Timber starter主题中的示例并遵循Timber docs中的说明来使自定义函数正常工作,但是我一生都无法正常工作。

我的functions.php就像这样:

class StarterSite extends TimberSite {

    ...

    function my_function() {
        return "Foo";
    }
    function add_to_twig( $twig ) {
        /* this is where you can add your own functions to twig */
        $twig->addExtension( new Twig_Extension_StringLoader() );
        $twig->addFilter('my_function', new Twig_SimpleFilter('my_function', array($this, 'my_function')));
        return $twig;
    }
}
Run Code Online (Sandbox Code Playgroud)

然后是我的Twig文件:

{{ my_function }}
Run Code Online (Sandbox Code Playgroud)

这返回

Twig_Error_Syntax: Unknown "my_function" function
Run Code Online (Sandbox Code Playgroud)

所以我试着让我的树枝像

{{ function (my_function)  }}
Run Code Online (Sandbox Code Playgroud)

那又回来了

Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given
Run Code Online (Sandbox Code Playgroud)

我也尝试过使用functions.php代码,如下所示:

function add_to_twig( $twig ) {
    /* this is where you can add your own functions to twig */
    $twig->addExtension( new Twig_Extension_StringLoader() );
    $twig->addFunction( new Timber\Twig_Function( 'my_function', 'my_function' ) );
    return $twig;
}
Run Code Online (Sandbox Code Playgroud)

那一回

Error: Call to a member function addFunction() on null
Run Code Online (Sandbox Code Playgroud)

显然,我在某个地方缺少核心概念,但是我不知道从哪里开始。我对此所做的任何搜索似乎都不适用于我的情况。

谁能指出我正确的方向?

Gch*_*htr 5

向Twig添加功能时,必须使用timber/twig过滤器。如果仅add_to_twig在类中定义方法,则不会发生任何事情。

所以你需要像下面这样的东西

class StarterSite extends Timber\Site {
    public function __construct() {
        parent::__construct();

        add_filter( 'timber/twig', array( $this, 'add_to_twig' ) );
    }

    …
}
Run Code Online (Sandbox Code Playgroud)

使功能在Twig中可用

现在,让我们看看您的add_to_twig方法。要添加功能时,需要使用addFunction代替addFilter。所以在您的情况下,可能应该是

$twig->addFunction( new Timber\Twig_Function(
    'my_function',
    array( $this, 'my_function' )
) );
Run Code Online (Sandbox Code Playgroud)

使用时{{ my_function }},Twig可能会my_function在上下文中寻找值。我会明确地将其称为函数:{{ my_function() }}

通过调用函数 function()

当您想直接通过调用函数时{{ function(my_function) }},则需要将函数名称作为字符串传递:

{{ function('my_function') }}
Run Code Online (Sandbox Code Playgroud)

但是,由于您已定义my_functionStarterSite类的方法,因此需要告诉Twig在哪里可以找到该函数:

{{ function(['StarterSite', 'my_function']) }}
Run Code Online (Sandbox Code Playgroud)

但!当您从Twig这样调用类方法时,该方法必须是静态的。因此,您必须my_function在课堂上定义如下:

class StarterSite extends Timber\Site {
    …

    public static function my_function() {
        return "Foo";
    }

    …
}
Run Code Online (Sandbox Code Playgroud)

timber/twig 在全局范围内过滤

如果将add_to_twig(与timber/twig过滤器一起)添加到functions.php中,它也可以正常工作,但是您还需要my_function作为StarterSite类的方法进行调用。同样,您可以使用数组符号来做到这一点:

function add_to_twig( $twig ) {
    $twig->addFunction( new Timber\Twig_Function(
        array( 'StarterSite', 'my_function' ),
        array( $this, 'my_function' )
    ) );

    return $twig;
}
Run Code Online (Sandbox Code Playgroud)

我希望这可以解决问题。在Twig中有很多调用函数的方法,最简单的方法是始终在全局上下文中定义要调用的函数(例如,直接在functions.php中),然后通过调用它function('my_function')