// Load Twig environment and compile template $loader = new Twig_Loader_Array(array( 'hello' => 'Hello {{ name }}!' )); $twig = new Twig_Environment($loader); $template = $twig->loadTemplate('hello'); // Render template with variables echo $template->render(array('name' => 'World'));
// Load Twig environment and compile template $loader = new Twig_Loader_Array(array( 'if' => '{% if condition %}True{% else %}False{% endif %}' )); $twig = new Twig_Environment($loader); $template = $twig->loadTemplate('if'); // Render template with variables echo $template->render(array('condition' => true));This example loads a template string with a control structure that outputs "True" or "False" depending on the value of "condition". The rendered output would be "True" since we set "condition" to true. Package library: This code also uses the Twig library for PHP.