Timber: How do I render a twig file into a variable?

Created on 21 Dec 2017  路  3Comments  路  Source: timber/timber

Something that's similar to render string.
$twigTemplate = Timber::render('myTwig', ['hi', 'hi']);

Most helpful comment

Hey @sgu1

You鈥檙e probably looking for Timber::compile(). Instead of using ob_start() and ob_get_contents(), you could use:

$compiled = Timber::compile( 'my_twig_file.twig', [ 'hi' => 'hello' ] );

All 3 comments

Never mind I figured it out. You can simply use output buffering.
Like this:
ob_start();
Timber::render('myTwig', ['hi', 'hi']);
$template = ob_get_contents();

Hey @sgu1

You鈥檙e probably looking for Timber::compile(). Instead of using ob_start() and ob_get_contents(), you could use:

$compiled = Timber::compile( 'my_twig_file.twig', [ 'hi' => 'hello' ] );

Thanks, Timber::compile() was exactly what I was looking for.

Was this page helpful?
0 / 5 - 0 ratings