public function run($viewContent, array $options = array()) { $extendsBlock = '{% extends "' . self::DEFAULT_LAYOUT . '" %}'; $contentStartBlock = '{% block content %}'; $contentEndBlock = '{% endblock %}'; $viewContent = $extendsBlock . $contentStartBlock . $viewContent . $contentEndBlock; if (array_key_exists('layout', $options)) { $layoutFileName = $options['layout']; } else { $layoutFileName = self::DEFAULT_LAYOUT; } $layoutFilePath = Application::getBasePath() . DIRECTORY_SEPARATOR . Application::getConfigItem('viewsFolder') . DIRECTORY_SEPARATOR . $layoutFileName; if (!file_exists($layoutFilePath)) { throw new \Exception('layout file could not be found'); } $layoutContent = file_get_contents($layoutFilePath); $this->twigLoaderArray = new \Twig_Loader_Array(['layout.html' => $layoutContent, 'view.html' => $viewContent]); $loader = new \Twig_Loader_Chain([$this->twigLoaderFileSystem, $this->twigLoaderArray]); $twig = new \Twig_Environment($loader); $twig->registerUndefinedFunctionCallback(function ($name) { if (function_exists($name)) { return new \Twig_SimpleFunction($name, function () use($name) { return call_user_func_array($name, func_get_args()); }); return false; } }); echo $twig->render('view.html', $options); return; }
private static function setAssetsFolder($assetsFolder = '') { if (empty($assetsFolder)) { $assetsFolder = Application::getBasePath() . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . Application::getConfigItem('webFolder') . DIRECTORY_SEPARATOR . Application::getConfigItem('assetsFolder'); } if (!is_dir($assetsFolder)) { throw new \Exception('assetsFolder is not a folder'); } self::$assetsFolder = $assetsFolder; }
protected function getView($view) { if (!is_string($view) || empty($view)) { throw new \Exception('view file name parameter could not be blank'); } $controller = get_class($this); $pattern = '#([\\w]+)' . Application::getConfigItem('controllerPostfix') . '$#'; preg_match($pattern, $controller, $matches); $controller = $matches[1]; $viewFilePath = Application::getBasePath() . DIRECTORY_SEPARATOR . Application::getConfigItem('viewsFolder') . DIRECTORY_SEPARATOR . $controller . DIRECTORY_SEPARATOR . $view; if (!file_exists($viewFilePath)) { } return file_get_contents($viewFilePath); }