0

I am migrating and troubleshooting a wordpress theme. I have a gutenberg block and an alert module set up to use the Timber composer package that allows the use of the twig templating engine.

I have it configured in a class

ProcessorTable.php

<?php
namespace CRG\Blocks;

class ProcessorTable
{
    public function __construct()
    {
        $this->createProcessorTable();
    }

    public function createProcessorTable()
    {
        if (function_exists('acf_register_block')) {
          // register a custom vue gravity forms block
                acf_register_block(array(
                    'name'            => 'processor-table-block',
                    'title'           => __('Processor Table Block'),
                    'description'     => __('A Block for displaying a contracted database processors table'),
                    'category'        => 'crg-custom-blocks',
                    'icon'            => 'welcome-write-blog',
                    'render_callback' => array($this, 'render_processor_table'),
                    'keywords'        => array( 'table' ),
          ));
        }
      }
    public function render_processor_table($block, $content = '', $is_preview = false)
    {
        $context = \Timber\Timber::context();
        // Store block values.
        $context['block'] = $block
        // Store field values.
        $context['fields'] = get_fields();
        // Store $is_preview value.
        $context['is_preview'] = $is_preview;
        $twigPath = TEMPLATEPATH . "/src/views/blocks/block-processor-table.html.twig";
        \Timber\Timber::render($twigPath, $context, 600);

    }
}

AlertNotification.php

<?php

namespace CRG\Controllers\SiteWide;

class AlertNotification {

    public function AlertModal(){
        $context = \Timber\Timber::context();
        $context['alert_header_text'] = get_field('alert_header_text', 'options');
        $context['alert_text'] = get_field('alert_text', 'options');
        $context['alert_icon'] = get_field('alert_icon', 'options');
        $context['alert_color'] = get_field('alert_color', 'options');
        $context['alert_toggle'] = get_field('alert_toggle', 'options');




        \Timber\Timber::render( TEMPLATEPATH . "/src/views/sitewide/alert-modal.html.twig", $context );
    }
}

this code worked on a cpanel server and on nexcess managed wordpress hosting, but when I migrated it to kinsta the code stopped rendering. It looks like the Timber::context() works, and the method can find the html.twig files, but it can't render the twig template, and produces no errors

I have tried troubleshooting this by checking the composer package versions, reinstalling the timber composer package, testing the code outside of the class directly into the functions.php file, and verifying the code can reach and output the file contents as a string. I checked the error log files and was unable to find a solution, or a cause of the error.

Hosting configuration: Kinsta Caching: disabled Wordpress debugging is enabled Running on PHP 7.4 Using MySQL

Virtua Creative
  • 2,025
  • 1
  • 13
  • 18
  • What do you mean it "stopped rendering"? Are you getting a white screen of death on the frontend, or a partial render? You may be getting errors that Kinsta is suppressing by default. Recommend you remove the `600` param from your `Timber::render` call, as caching can obfuscate errors if you manage to cache a bad render. – acobster Apr 23 '20 at 05:43
  • So I think I figured this out. when I moved to kinsta, the render function was looking in the wrong directory, since I needed to use \Timber\Timber::$locations to set to correct twig template directory. – Chris Gaffney Apr 24 '20 at 16:55

1 Answers1

0

I added this to my functions.php file


\Timber\Timber::$locations = TEMPLATEPATH . "/src/views";

so everything in the functions.php file for timber to work would look like this


require_once(__DIR__ . '/vendor/autoload.php');
$timber = new Timber\Timber();
\Timber\Timber::$locations = TEMPLATEPATH . "/src/views";

then in my ProcessorTable.php file I can call the Twig file path like this

$twigPath = "/blocks/block-processor-table.html.twig";

return \Timber\Timber::render( $twigPath, $context);

My guess about what went wrong is that the timber package was looking in the wrong director when rendering, based on incorrect locations information the Timber:locations method allows you to set up a custom location to store the templates: https://timber.github.io/docs/guides/template-locations/

Not sure why this would have worked on other systems, but not Kinsta. However, this appears to be a good practice going forward by explicitly setting the views.