Render a Twig template.

@param string|array $file The template file.
@param array        $vars To pass to the Twig template.

@return string

@throws LogicException If the **$file** is not in the ``$page->dir()``, or if it doesn't exist.
763917
public function renderTwig($file, array $vars = array())
    {
        $page = Page::html();
        if (is_array($file)) {
            $vars = (isset($file['vars']) && is_array($file['vars'])) ? $file['vars'] : array();
            $default = (isset($file['default']) && is_dir($file['default'])) ? rtrim($file['default'], '/').'/' : null;
            $file = (isset($file['file']) && is_string($file['file'])) ? $file['file'] : '';
            if ($default && $template = $this->getFiles($file, $default)) {
                $file = array_pop($template);
            }
        }
        $base = $page->dir['page'];
        $cut = strlen($base);
        if (strpos($file, $this->blog->folder.'themes/') === 0) {
            $dir = $this->blog->folder.'themes/';
            $file = substr($file, strlen($dir));
            $theme = strstr($file, '/', true).'/';
            $dir .= $theme;
            $file = substr($file, strlen($theme));
            $loader = $this->getTwig()->getLoader();
            $loader->addPath($dir, 'theme');
            $dir = substr($dir, $cut);
        } elseif (strpos($file, $base) === 0) {
            $dir = substr(dirname($file).'/', $cut);
            $file = basename($file);
        } else {
            throw new \LogicException("The '{$file}' is not in your website's Page::dir folder.");
        }
        if (!is_file($base.$dir.$file)) {
            throw new \LogicException("The '{$dir}{$file}' file does not exist.");
        }
        if (is_null($this->asset)) { // our first time here
            $this->getTwig()->setParser(new Parser($this->getTwig()));
        }
        $this->asset = array(
            'dir' => $dir,
            'chars' => $page->url['chars'],
        );
        $save = array(
            'template' => $dir.$file,
            'vars' => $vars,
            'start' => microtime(true),
        );
        try {
            $html = $this->getTwig()->render($dir.$file, array_merge($vars, $this->vars));
            $save['output'] = $html;
        } catch (\Exception $e) {
            $html = '<p>'.$e->getMessage().'</p>';
            $save['error'] = $e->getMessage();
        }
        $save['end'] = microtime(true);
        $save['time'] = $save['end'] - $save['start'];
        self::$templates[] = $save;

        return $html;
    }
1254762
public function render($templateFile, array $vars = [])
    {
        $this->getService('retriever')->setLocalVars($vars);
        return $this->getService('template')->render($templateFile, $vars);
    }
348712
public function renderTemplate($template, $vars)
    {
        $twig = $this->getContainer()->get('twig');

        $template = $twig->createTemplate($template);

        return $template->render($vars);
    }
400712
public function fakeTemplate($file)
    {
        $this->template->loadTemplate($file);
        $varsName = $this->template->getVariables();
        $out = '';
        $num = $this->num;
        for ($i = 0; $i < $num; $i++) {
            $fakeData = $this->num(1)->fake($varsName)->toArray();
            $data = array_combine(array_values($varsName), $fakeData);
            $out .= $this->template->render($data);
        }

        return $out;
    }
1055547
public function render($template = null, $vars = null)
    {
        $context = $this->_context;

        if ($vars !== null) {
            $context->vars = $vars;
        }

        if (!$template) {
            $area = $this->dispatcher->getArea();
            $controller = $this->dispatcher->getController();

            if ($area) {
                $dir = "@app/Areas/$area/Views/$controller";
            } else {
                $dir = "@views/$controller";
            }

            if (!isset($this->_dirs[$dir])) {
                $this->_dirs[$dir] = $this->filesystem->dirExists($dir);
            }

            if ($this->_dirs[$dir]) {
                $template = $dir . '/' . ucfirst($this->dispatcher->getAction());
            } else {
                $template = $dir;
            }
        }

        $this->eventsManager->fireEvent('view:beforeRender', $this);

        $context->content = $this->_render($template, $context->vars, false);

        if ($context->layout !== false) {
            $layout = $this->_findLayout();
            $context->content = $this->_render($layout, $context->vars, false);
        }

        $this->eventsManager->fireEvent('view:afterRender', $this);

        return $context->content;
    }
1031979
private function renderContent(array $fileInfo, array $globalTemplateVars): string
    {
        if (empty($fileInfo['source'])) {
            return '';
        }

        $filename = $this->applicationConfig->getSourceFile($fileInfo['source']);
        $content = file_get_contents($filename);

        if (!empty($fileInfo['template_vars'])) {
            if ($globalTemplateVars) {
                $templateVars = array_replace($globalTemplateVars, $fileInfo['template_vars']);
            } else {
                $templateVars = $fileInfo['template_vars'];
            }
        } else {
            $templateVars = $globalTemplateVars;
        }

        if ($templateVars && '.twig' == substr($fileInfo['source'], -5)) {
            $twig = new TwigEnvironment(new TwigArrayLoader([]));
            $template = $twig->createTemplate($content);
            $content = $template->render($templateVars);
        }

        return $content;
    }
229697
public function processString($string, array $vars = [])
    {
        // override the twig header vars for local resolution
        $this->grav->fireEvent('onTwigStringVariables');
        $vars += $this->twig_vars;

        $name = '@Var:' . $string;
        $this->setTemplate($name, $string);

        try {
            $output = $this->twig->render($name, $vars);
        } catch (\Twig_Error_Loader $e) {
            throw new \RuntimeException($e->getRawMessage(), 404, $e);
        }

        return $output;
    }
320341
protected function generateOutputFile($template, $file, array $parameters)
    {
        $template = sprintf('%s/%s', static::LANGUAGE, $template);
        $content = $this->render($template, $parameters);
        $ext = static::EXTENSION;
        $addNewLine = static::LANGUAGE !== 'json-schema';
        return new OutputFile(
            "{$this->compileOptions->getOutput()}/{$file}$ext",
            trim($content) . ($addNewLine ? PHP_EOL : '')
        );
    }
903911
public function template($filename, array $vars = [])
    {
        if ($view = view()->load($filename, $vars, true)) {
            $this->body($view);
        }

        return $this;
    }
1230379
public function render($vars = [], $asc = 0)
	{
		$vars = $this->vars + $vars;
		$className = get_called_class();

		while($asc > 0)
		{
			$parentClass = get_parent_class($className);

			if(!$parentClass)
			{
				break;
			}

			$className = $parentClass;

			$asc--;
		}

		do {
			$reflection = new \ReflectionClass($className);
			$classFile = $reflection->getFileName();

			$fileContent = file_get_contents($classFile);
			$tokens = token_get_all($fileContent);
			$hasHalt = FALSE;

			foreach($tokens as $token)
			{
				if(is_array($token)
					&& isset($token[0], $token[2])
				) {
					if($token[0] == T_HALT_COMPILER)
					{
						$hasHalt = TRUE;
						$this->haltLine = $token[2] - 1;
					}
				}
			}

			if($hasHalt)
			{
				$template = $tokens[ count($tokens)-1 ][1];
				break;
			}

			$className = get_parent_class($className);
    
			if(!$className || $className == get_class())
			{
				throw new \Exception(sprintf(
					'Cannot locate template. '
					. 'No call to __halt_compiler() found along inheritance chain of %s.'
					, get_called_class()
				));
			}

		} while(!$hasHalt);

		$renderScope = \Closure::Bind(
			function() use($template, $vars, $classFile)
			{
				extract($vars);
				ob_start();

				try{
					eval($template);
				}
				catch(\Exception $e)
				{
					error_log(
						'Exception thrown in template: '
							. $classFile
							. ':'
							. ($e->getLine() + $this->haltLine)
							. PHP_EOL
							. $e->getMessage()
					);

					throw $e;
				}

				$content = ob_get_contents();
				ob_end_clean();

				return $content;
			}
			, $this
			, get_called_class()
		);

		$result = $renderScope();

		if(isset($vars['__debug']) && $vars['__debug'])
		{
			$result = sprintf(
				"<!-- START %s -->\n%s\n<!-- END %s --->"
				, get_called_class()
				, $result
				, get_called_class()
			);
		}

		return $result;
	}
1354698
public function mutateFile(File &$file)
    {
        // If markdown file has a layout associated with it, we need to ensure it gets rendered within that
        if ($file->hasData('layout')) {
            $file->setExt('phtml');     // Templates are managed by the phtml renderer
            $file->setRendered(false);  // Set rendered to false so that within Compile.php's Execute Renderers loop it gets re-rendered
        }
    }
144994
public function load($file, array $vars = array())
    {
        $file = !$file instanceof File
            ? new File("{$file}")
            : $file;
        $file = $file->isRelative()
            ? $this->file($file)
            : $file;
        return \Coast\load($file, array_merge(['app' => $this], $vars));
    }
795181
public function view($file, array $vars = [])
    {
		if ( is_file($file = $this->getPath($file . '.php')) ) {
            $this->data = $vars;
			extract($vars, EXTR_OVERWRITE|EXTR_REFS);
			include($this->cache($file));
            $this->end();
		} else {
            throw new \Exception("Views: File not found $file");
        }

		return $this;
	}
285777
public function render( $file = '' ) {
		reset( $this->assets[ $this->type ] );
		//reset($this->attr[$this->type]);
		$attr = '';

		if ( $this->type == 'js' ) {

			if ( $file != '' ) {
				$key     = array_search( $file, $this->assets[ $this->type ] );
				$version = $this->versions[ $this->type ][ $key ];
				$asset   = $this->assets[ $this->type ][ $key ];

				if ( preg_match( "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $asset ) ) {
					$asset_url = $asset;
				} else {
					$asset_url = $this->BaseUrl . Config::get( 'path.assets' ) . "/$this->type/" . $asset . '.' .
					             $this->type . $version;
				}

				while ( list( $at, $val ) = each( $this->attr[ $this->type ][ $key ] ) ) {
					$attr .= $at . '="' . $val . '" ';
				}

				if ( $key ) {
					return '<script type="text/javascript" src="' . $asset_url . '" ' . $attr . '></script>' . "\n";
				}

			} else {
				$render = '';

				while ( list( $key, $value ) = each( $this->assets[ $this->type ] ) ) {
					$asset   = $this->assets[ $this->type ][ $key ];
					$version = $this->versions[ $this->type ][ $key ];

					if ( preg_match( "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $asset ) ) {
						$asset_url = $asset;
					} else {
						$asset_url = $this->BaseUrl . Config::get( 'path.assets' ) . "/$this->type/" . $asset . '.' .
						             $this->type . $version;
					}

					$attr = '';

					while ( list( $at, $val ) = each( $this->attr[ $this->type ][ $key ] ) ) {
						$attr .= $at . '="' . $val . '" ';
					}

					$render .= '<script type="text/javascript" src="' . $asset_url . '" ' . $attr . '></script>' . "\n";
				}

				return $render;
			}

		} elseif ( $this->type == 'css' ) {

			if ( $file != '' ) {
				$key     = array_search( $file, $this->assets[ $this->type ] );
				$asset   = $this->assets[ $this->type ][ $key ];
				$version = $this->versions[ $this->type ][ $key ];

				if ( preg_match( "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $asset ) ) {
					$asset_url = $asset;
				} else {
					$asset_url = $this->BaseUrl . Config::get( 'path.assets' ) . "/$this->type/" . $asset . '.' .
					             $this->type . $version;
				}

				while ( list( $at, $val ) = each( $this->attr[ $this->type ][ $key ] ) ) {
					$attr .= $at . '="' . $val . '" ';
				}

				if ( $key ) {
					return '<link rel="stylesheet" href="' . $asset_url . '" ' . $attr . ' />' . "\n";
				}

			} else {
				$render = '';

				while ( list( $key, $value ) = each( $this->assets[ $this->type ] ) ) {
					$attr    = '';
					$asset   = $this->assets[ $this->type ][ $key ];
					$version = $this->versions[ $this->type ][ $key ];

					if ( preg_match( "/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $asset ) ) {
						$asset_url = $asset;
					} else {
						$asset_url = $this->BaseUrl . Config::get( 'path.assets' ) . "/$this->type/" . $asset . '.' .
						             $this->type . $version;
					}

					while ( list( $at, $val ) = each( $this->attr[ $this->type ][ $key ] ) ) {
						$attr .= $at . '="' . $val . '" ';
					}

					$render .= '<link rel="stylesheet" href="' . $asset_url . '" ' . $attr . ' />' . "\n";
				}

				return $render;
			}
		}

	}
341020
function setTemplateFile($file){
		$this->_template->setTemplateFile(ROOT . DS . 'application' . DS . 'views' . DS . $file);
		
	}
955711
public function render($template, array $vars = [], array $blocks = [])
    {
        $directory = rtrim($this->directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
        $file = $directory . trim($template, DIRECTORY_SEPARATOR) . $this->suffix;
        $vars += $this->vars;

        $sandbox = new Template($this, $file, $blocks, $this->helpers);
        return $sandbox->compile($vars);
    }
599333
protected function formatTemplate($template, array $vars)
    {
        $output = $template;
        $token = $this->buildToken();

        foreach ($vars as $key => $value) {
            $replacement = sprintf($token, $key);

            if (!$this->isFlagEnabled(self::ALLOW_NEWLINES)) {
                $value = $this->sanitizeNewlines($value);
            }

            if (false !== strpos($output, $replacement)) {
                $output = str_replace($replacement, $value, $output);
            }
        }

        $output = $this->removeExtraTokens($token, $output);

        return $output;
    }