0

使用 Timber 库时,是否有向所有实例或渲染页面提供数据的选项或方法?

我想在核心functions.php文件中设置一些站点范围的数据,并使其可用于所有模板,而无需在每次之前手动添加它Timber::render()

4

1 回答 1

1

timber_contexttimber\context您使用get_context.

这是一个如何添加菜单/导航的示例(来自TimberMenu 上的 Wiki 页面):

add_filter( 'timber_context', function( $context ) {
    /* So here you are adding data to Timber's context object, i.e... */
    $context['foo'] = 'I am some other typical value set in your functions.php file, unrelated to the menu';

    /* Now, in similar fashion, you add a Timber menu and send it along to the context. */
    $context['menu'] = new Timber\Menu(); // This is where you can also send a WordPress menu slug or ID

    return $context;
} );

将数据放入模板所需的最少操作将是:

$context = Timber::get_context();

Timber::render( 'template.twig', $context );
于 2016-07-17T16:29:25.987 回答