Talk Nerdy To Me
Talk Nerdy To Me

Reputation: 672

check existence of twig template in Timber

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.

Upvotes: 2

Views: 1091

Answers (1)

Talk Nerdy To Me
Talk Nerdy To Me

Reputation: 672

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

Thank you @Jeto for the trail.

Upvotes: 2

Related Questions