getherbert / herbert

The WordPress Plugin Framework:
http://getherbert.com/
634 stars 95 forks source link

How use wordpress and php function in twig? #176

Open hamedmoody opened 7 years ago

hamedmoody commented 7 years ago

Hi. How use wordpress and php functions in twig template. Thanks for your best framework.

ken-bem commented 7 years ago

You should google the Timber Framework

hamedmoody commented 7 years ago

but i need herbert framework, is not any way?

ken-bem commented 7 years ago

You use both, Timber is an addon for twig specifically for Wordpress

hamedmoody commented 7 years ago

id use "composer require timber/timber". but what i do next?

ken-bem commented 7 years ago

After that create a 'views' folder inside your app folder.

then you render using this code:


use Timber\Timber;

...
$context = Timber::get_context();

$context['VARIABLE_TO_ASSIGN_TO_TWIG'] = "foo";

Timber::render("path/to/template.twig", $context);
webstractions commented 7 years ago

You can also use Twig's registerUndefinedFunctionCallback() without having to include Timber. There are some caveats though. Check out the documentation for defining undefined functions on the fly.