Ad

Check Existence Of Twig Template In Timber

- 1 answer

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.

Ad

Answer

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

Thank you @Jeto for the trail.

Ad
source: stackoverflow.com
Ad