【问题标题】:Render custom twig file for generic page template using Timber使用 Timber 为通用页面模板渲染自定义树枝文件
【发布时间】:2021-05-31 10:34:56
【问题描述】:

我正在尝试使用 Timber/Twig 在 WordPress 中创建自定义页面模板。

我使用了以下代码,但不明白为什么它不起作用。 木材文档没有提供具体示例。

我有一个名为 template-form-template.php 的文件,用于多个表单页面。 我更喜欢在 admin 中选择模板,而不是为每个页面 slug 创建一个 page-...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 );

?>

模板名称按预期显示在 WordPress 管理员中。 但是,没有调用 page-form.twig。 我正在测试的页面中有一个简码,它显示为原始简码,这意味着页面内容正在显示,但没有树枝格式。

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

【问题讨论】:

    标签: wordpress templates twig timber


    【解决方案1】:

    你是否在你的functions.php中初始化了Timber?

    $timber = new Timber\Timber();
    

    或者设置视图目录来查找 .twig 文件?

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

    【讨论】:

    • 是的,该网站已经建成并运行。我只想添加一个通用模板选项
    • page-form.twig 是否与您的其他视图位于同一文件夹中?我建议重新检查您的路径,以确保某处没有错字。
    • 确实如此,我检查了拼写。我还尝试使用相应的 page-form.twig 命名 php 文件 page-form-template.php 而不是 template-form-template.php,但它不起作用。要么我在这里遗漏了一些非常明显的东西,要么文档可以使用一个应该如何完成的示例!
    • 您可以尝试改名为 template-custom-form.php 吗? page- 前缀可能会影响模板层次结构
    • 你有没有参考这里的文档:timber.github.io/docs/guides/custom-page-templates
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多