Ejemplo n.º 1
0
/**
 * Renders a template.
 *
 * @param Apishka_Templater_Environment $env
 * @param array                         $context
 * @param string|array                  $template      The template to render or an array of templates to try consecutively
 * @param array                         $variables     The variables to pass to the template
 * @param bool                          $withContext
 * @param bool                          $ignoreMissing Whether to ignore missing templates or not
 * @param bool                          $sandboxed     Whether to sandbox the template or not
 *
 * @return string The rendered template
 */
function twig_include(Apishka_Templater_Environment $env, $context, $template, $variables = array(), $withContext = true, $ignoreMissing = false, $sandboxed = false)
{
    $alreadySandboxed = false;
    $sandbox = null;
    if ($withContext) {
        $variables = array_merge($context, $variables);
    }
    if ($isSandboxed = $sandboxed && $env->hasExtension('sandbox')) {
        $sandbox = $env->getExtension('sandbox');
        if (!($alreadySandboxed = $sandbox->isSandboxed())) {
            $sandbox->enableSandbox();
        }
    }
    $result = null;
    try {
        $result = $env->resolveTemplate($template)->render($variables);
    } catch (Apishka_Templater_Error_Loader $e) {
        if (!$ignoreMissing) {
            if ($isSandboxed && !$alreadySandboxed) {
                $sandbox->disableSandbox();
            }
            throw $e;
        }
    }
    if ($isSandboxed && !$alreadySandboxed) {
        $sandbox->disableSandbox();
    }
    return $result;
}