1
votes

I am trying to create a custom page template in WordPress, using Timber/Twig.

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. I prefer to select the template in admin rather than creating a page-...twig for each page slug.

<?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. However, the page-form.twig is not being called. 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.

page-form.twig content is:

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

1 Answers

0
votes

Have you initialized Timber in your functions.php?

$timber = new Timber\Timber();

Or sets the view directory to find .twig files?

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