简体   繁体   English

使用 Timber 为通用页面模板渲染自定义 twig 文件

[英]Render custom twig file for generic page template using Timber

I am trying to create a custom page template in WordPress, using Timber/Twig.我正在尝试使用 Timber/Twig 在 WordPress 中创建自定义页面模板。

I have used the following code and do not understand why it's not working.我使用了以下代码,但不明白为什么它不起作用。 Timber docs don't provide specific examples.木材文档没有提供具体示例。

I have a file named template-form-template.php, to be used for several form pages.我有一个名为 template-form-template.php 的文件,用于多个表单页面。 I prefer to select the template in admin rather than creating a page-...twig for each page slug.我更喜欢 select 管理员中的模板,而不是为每个页面 slug 创建一个页面-...twig。

<?php
/**
 * Template Name: Form Template
 * Description: Use this template for custom forms.
 */

$context = Timber::context();
$context['post'] = new Timber\Post();
Timber::render( 'page-form.twig', $context );

?>

The template name appears as expected in WordPress admin.模板名称按预期显示在 WordPress 管理员中。 However, the page-form.twig is not being called.但是,没有调用 page-form.twig。 I have a shortcode in the page I'm testing, which appears as a raw shortcode, meaning the page content is being shown but with no twig formatting.我正在测试的页面中有一个简码,它显示为原始简码,这意味着页面内容正在显示,但没有 twig 格式。

page-form.twig content is: page-form.twig 内容为:

{% extends "base.twig" %}

{% block content %}
    <div class="content-wrapper">
        <article class="post-type-{{post.post_type}}" id="post-{{post.ID}}">
            <section class="article-content">
                <h1 class="article-h1">page.twig{{post.title}}Foooo</h1>
                <div class="article-body">
                    {{post.content}}
                </div>
            </section>
        </article>
    </div>
    <!-- /content-wrapper -->
{% endblock %}

Have you initialized Timber in your functions.php?您是否在您的函数中初始化了 Timber.php?

$timber = new Timber\Timber();

Or sets the view directory to find.twig files?还是设置查看目录查找.twig文件?

Timber::$dirname = array( 'templates', 'views' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM