I'm using Timber for a Wordpress site and I'm trying to start and build Gutenberg blocks. The blocks work in the backend editor but I cant get them to work in the template. In fact nothing from the content box prints to the screen when I try use {{post.content}} as it just returns null.
How to get post.content to return the content?
WP - 5.8 Timber 1.18.0
template-block-test.php
<?php
/* Template Name: block test */
$context = Timber::get_context();
Timber::render( 'template-block-test.twig', $context );
template-block-test.twig
{% extends 'base.twig' %}
{% block content %}
<h1>Header to test file</h1>
{{post.content}} //returns null
<pre>
{{dump()}}
</pre>
{% endblock %}
functions.php
add_action( 'acf/init', 'acf_forty_it_block', 9 );
add_action( 'acf/init', 'acf_forty_it_block_settings', 10 );
function acf_forty_it_block() {
// Check function exists.
if( function_exists( 'acf_register_block_type' ) ) {
// register a testimonial block.
acf_register_block_type( array(
'name' => 'fortyblock',
'title' => __('Forty'),
'description' => __('Forty does a great job at been 60/40'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'design',
'icon' => 'format-gallery',
'keywords' => array( 'test', 'forty', ),
) );
}
}
function my_acf_block_render_callback( $block, $content = '', $is_preview = false ) {
$context = Timber::context();
$context['block'] = $block;
$forty = array(
'top_header' => get_field('forty_top_header'),
'text' => get_field('forty_text'),
'list_content' => get_field('forty_list_content'),
'image' => get_field('forty_image'),
'color' => get_field('forty_color'),
);
$context['forty'] = $sixtyForty;
$context['is_preview'] = $is_preview;
// Render the block.
Timber::render( 'blocks/forty.twig', $context );
}
I'm using Timber for a Wordpress site and I'm trying to start and build Gutenberg blocks. The blocks work in the backend editor but I cant get them to work in the template. In fact nothing from the content box prints to the screen when I try use {{post.content}} as it just returns null.
How to get post.content to return the content?
WP - 5.8 Timber 1.18.0
template-block-test.php
<?php
/* Template Name: block test */
$context = Timber::get_context();
Timber::render( 'template-block-test.twig', $context );
template-block-test.twig
{% extends 'base.twig' %}
{% block content %}
<h1>Header to test file</h1>
{{post.content}} //returns null
<pre>
{{dump()}}
</pre>
{% endblock %}
functions.php
add_action( 'acf/init', 'acf_forty_it_block', 9 );
add_action( 'acf/init', 'acf_forty_it_block_settings', 10 );
function acf_forty_it_block() {
// Check function exists.
if( function_exists( 'acf_register_block_type' ) ) {
// register a testimonial block.
acf_register_block_type( array(
'name' => 'fortyblock',
'title' => __('Forty'),
'description' => __('Forty does a great job at been 60/40'),
'render_callback' => 'my_acf_block_render_callback',
'category' => 'design',
'icon' => 'format-gallery',
'keywords' => array( 'test', 'forty', ),
) );
}
}
function my_acf_block_render_callback( $block, $content = '', $is_preview = false ) {
$context = Timber::context();
$context['block'] = $block;
$forty = array(
'top_header' => get_field('forty_top_header'),
'text' => get_field('forty_text'),
'list_content' => get_field('forty_list_content'),
'image' => get_field('forty_image'),
'color' => get_field('forty_color'),
);
$context['forty'] = $sixtyForty;
$context['is_preview'] = $is_preview;
// Render the block.
Timber::render( 'blocks/forty.twig', $context );
}
Share
Improve this question
asked Jan 9, 2022 at 12:56
monkeyman905monkeyman905
398 bronze badges
1 Answer
Reset to default 2You will need to add the current page object to the timber context using timber_context
filter.
Something like this
add_filter('timber_context', 'bt_timber_add_to_context');
function bt_timber_add_to_context ($context) {
// add current page content
$context['page'] = Timber::get_post();
return $context;
}
That's how I added it in my custom timber theme.
Now when you want to get the page content you can do this
{{ page.content }}
This is availalbe only if you use Timber::context()
in your render
/compile
methods.
Basically if you use Timber::context()
, one of the object properties will be page