2
votes

Is there a way in Timber to check to see if a Twig template exists before attempting to render one?

I have seen SO answers for how to accomplish this in Symfony ($this->get('twig')->getLoader()->exists('AcmeDemoBundle:Foo:bar.html.twig')), but I need a Timber-specific answer.

If not, I can always use PHP-specific

if ( get_stylesheet_directory() . '/templates/template-name.twig' ) { ... }

but I thought I'd see if anyone knew of a Timber method to accomplish this.

1
I don't know much about WP/Timber, but looks like Timber\Loader has a get_loader method that should return the Twig loader instance (which you can then call exists on).Jeto

1 Answers

2
votes
$loader = new Timber\Loader;
if ( $loader->get_loader()->exists( 'template-name.twig' ) ) {
    // file exists
}

Thank you @Jeto for the trail.