public function render($view, $params = array()) { $controller = $viewPage = null; $this->widgetName = $view; $this->__set('parameters', $params); $controller = Inflector::getClassNameFromNamespace($this->getController()); $controller = strtolower(str_replace('Controller', '', $controller)); list($viewPath, $path) = $this->getPath($controller); /* | We will check if tpl is holding the object of | twig, then we will set twig template | environment */ if (is_object($this->tpl) && is_file($path . $view . $this->templateExtension)) { return $this->setTwigTemplateInstance($controller, $view); } $viewPage = $path . $view . '.view' . EXT; /* | Throw exception is file is not readable */ if (!file_exists($viewPage) && !is_readable($viewPage)) { throw new ViewNotFoundException(); 'The Path ' . $path . $view . '.view' . EXT . ' is invalid.'; } $this->layout = Inflector::toDirectorySeparator($this->layout); if ($this->layout !== '') { // render view page into the layout $this->renderViewLayout($viewPage, $viewPath, $params); return $this; } $this->viewPath = $viewPage; $this->loadView(); return $this; }
/** * Render twig templates * * @param $view * @param array $param * @param bool $return * @return $this */ public function template($view, array $param = [], $return = false) { $this->setTwigEnvironment(); $path = $this->getPath(Inflector::toDirectorySeparator($view), true); /* | We will check if tpl is holding the object of | twig, then we will set twig template | environment */ if (is_object(static::$twigEnvironment) && is_file($path)) { $this->setTwigTemplateInstance($view); } /* | We will check is twig template instance exists | then we will render twig template with parameters */ if (is_object(static::$twigEnvironment) && is_object($this['twig_template'])) { return $return ? $this['twig_template']->render($param) : $this['twig_template']->display($param); } return $this; }
/** * We will get the layout and generate into the application * directory. * * @param $layout * * @return bool */ public function generateLayout($layout) { $layout = Inflector::toDirectorySeparator($layout); $file = $this->getViewTemplatePath() . $layout . DS . $this->viewLayoutName(); $appViewPath = ''; $appViewPath = $this->command->applicationDir . DS . 'Views' . DS; $this->hasDirectory($appViewPath); $this->hasDirectory($appViewPath . 'layouts'); $this->hasDirectory($appViewPath . DS . $layout); file_exists($file) or die("Base layout stub file doesn't exists in Cygnite Core"); $layoutFile = $appViewPath . $layout . DS . $this->viewLayoutName(); if ($this->layoutType == 'php') { $layoutFile = str_replace('.stub', EXT, $layoutFile); } else { $layoutFile = str_replace('.stub', '.twig', $layoutFile); } if (file_exists($layoutFile)) { echo "\n Layout file already exists in {$layoutFile} directory \n"; return true; } /*read operation ->*/ // Open the file to get existing content $fileContent = file_get_contents($file); $handle = null; $handle = fopen($layoutFile, 'w') or die('Cannot open file: ' . $layoutFile); fwrite($handle, $fileContent); fclose($handle); }