在 Timber 中使用自定义函数 [英] Using custom functions with Timber

查看:35
本文介绍了在 Timber 中使用自定义函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试通过使用 Timber 入门主题中的示例并按照 木材文档,但我这辈子都做不到.

I've been trying to get a custom function working by using the example in the Timber starter theme, and following the instructions in the Timber docs, but I can't for the life of me get this working.

我的functions.php是这样的:

My functions.php is like so:

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;
    }
}

然后是我的 Twig 文件:

Then my Twig file:

{{ my_function }}

返回

Twig_Error_Syntax: Unknown "my_function" function

所以我试着让我的 Twig 像

So I've tried with my Twig being like

{{ function (my_function)  }}

然后返回

Warning: call_user_func_array() expects parameter 1 to be a valid callback, no array or string given

我也尝试过像这样使用我的functions.php代码:

I've also tried with my functions.php code like so:

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;
}

那个人回来了

Error: Call to a member function addFunction() on null

显然我在某处遗漏了一个核心概念,但我不知道从哪里开始.我围绕此进行的搜索似乎都不适用于我的情况.

Clearly I'm missing a core concept somewhere, but I don't know where to start. None of the searches I've made around this seem to apply to my situation.

谁能指出我正确的方向?

Can anyone point me in the right direction?

推荐答案

向 Twig 添加功能时,您必须使用 timber/twig 过滤器.如果你只在你的类中定义了一个 add_to_twig 方法,什么都不会发生.

When adding functionality to Twig, you’ll have to use the timber/twig filter. If you only define an add_to_twig method in your class, nothing will happen.

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

So you’d need something like the following

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

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

    …
}

在 Twig 中提供一个函数

现在让我们看看您的 add_to_twig 方法.当您想添加功能时,您需要使用 addFunction 而不是 addFilter.所以在你的情况下,它可能应该是

Make a function available in Twig

Now let’s look at your add_to_twig method. When you want to add a function, you need to use addFunction instead of addFilter. So in your case, it should probably be

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

当您使用 {{ my_function }} 时,Twig 可能会在上下文中查找值 my_function.我会像函数一样明确地调用它:{{ my_function() }}.

When you use {{ my_function }}, Twig probably looks for a value my_function in the context. I’d explicitly call it like a function: {{ my_function() }}.

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

When you want to call the function directly through {{ function(my_function) }}, then you need to pass the function name as a string:

{{ function('my_function') }}

但是,因为您将 my_function 定义为 StarterSite 类的方法,您需要告诉 Twig 在哪里可以找到该函数:

However, because you defined my_function as a method of your StarterSite class, you need to tell Twig where it can find that function:

{{ function(['StarterSite', 'my_function']) }}

但是!当您像这样从 Twig 调用类方法时,该方法需要是静态的.所以你必须在你的类中像这样定义 my_function:

But! When you call a class method from Twig like that, then the method needs to be static. So you’d have to define my_function like this in your class:

class StarterSite extends Timber\Site {
    …

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

    …
}

timber/twig 全局上下文中的过滤器

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

timber/twig filter in the global context

If you add add_to_twig (together with the timber/twig filter) to your functions.php, it could work as well, but then you’d also need to call my_function as a method of your StarterSite class. Again, you can do that using the array notation:

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

    return $twig;
}

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

I hope this clears things up. There are a lot possibilities to call functions in Twig, the easiest is always to define the function you want to call in the global context (e.g. directly in functions.php) and then call it through function('my_function').

这篇关于在 Timber 中使用自定义函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆