Example #1
0
 /**
  * Building of a view content including a view file passing it parameters
  *
  * The view file will be included "as-is" so:
  *
  * - it may exists,
  * - it can be either some full HTML, some CSS, some JS containing PHP scripts
  *
  * The parameters will be merged with the object `$default_view_params` and exported
  * in the global context of the view file. For example, if you define a parameter named
  * `param` on a certain value, writing ths following in your view file :
  *
  *     <?php echo $param; ?>
  *
  * will render the value.
  *
  * The best practice is to NOT use the small php tags `<?= ... ?>`.
  *
  * @param string $view The view filename (which must exist)
  * @param array $params An array of the parameters passed for the view parsing
  * @throws \TemplateEngine\TemplateEngineException if the file view can't be found
  * @return string Returns the view file content rendering
  */
 public function render($view, array $params = array())
 {
     $this->setView($view);
     $this->setParams($params);
     $_params = $this->getParams();
     $_view = $this->getTemplate($this->getView());
     $_template = TemplateEngine::getInstance();
     if ($_view && @file_exists($_view)) {
         /*
         echo 'Loading view file "'.$_view.'" passing arguments:';
         echo '<pre>';
         var_dump($_params);
         echo '</pre>';
         */
         if (!empty($_params)) {
             extract($_params, EXTR_OVERWRITE);
         }
         ob_start();
         $_template->includePackagesViewsFunctions();
         include $_view;
         $this->setOutput(ob_get_contents());
         ob_end_clean();
     } else {
         throw new TemplateEngineException(sprintf('View "%s" can\'t be found (searched in "%s")!', $view, implode(', ', $this->getIncludePath())));
     }
     return $this->getOutput();
 }
 public function __construct()
 {
     $this->loader = AssetsLoader::getInstance(__DIR__ . '/..', 'www', __DIR__);
     /*
     echo '<pre>';
     var_export($loader);
     var_export($loader->getAssetsPath());
     var_export($loader->getAssetsWebPath());
     exit('yo');
     */
     $this->template_engine = TemplateEngine::getInstance();
     $this->template_engine->guessFromAssetsLoader($this->loader)->setLayoutsDir(__DIR__ . '/../www/')->setToTemplate('setCachePath', __DIR__ . '/tmp')->setToTemplate('setAssetsCachePath', __DIR__ . '/tmp')->setToView('setIncludePath', __DIR__ . '/views');
 }
 public function __construct()
 {
     $this->loader = AssetsLoader::getInstance(__DIR__ . '/..', 'www', __DIR__, AssetsLoader::PRESETS_NO_CONFLICT);
     /*
     echo '<pre>';
     var_export($loader);
     var_export($loader->getAssetsPath());
     var_export($loader->getAssetsWebPath());
     exit('yo');
     */
     $this->template_engine = TemplateEngine::getInstance(TemplateEngine::THROW_ALL_ERRORS);
     $this->template_engine->setPageLayout(self::$default_page_layout)->guessFromAssetsLoader($this->loader)->setLayoutsDir(__DIR__ . '/../www/')->setToTemplate('setCachePath', __DIR__ . '/tmp')->setToTemplate('setAssetsCachePath', __DIR__ . '/tmp')->setToView('setIncludePath', __DIR__ . '/views');
 }
 public function render($format = 'html')
 {
     if (!empty($this->type)) {
         $_meth = 'render' . ucfirst($this->type);
         if (method_exists($this->profiler, $_meth)) {
             $this->profiler->setCurrentEntity($this->entity);
             return $this->profiler->{$_meth}(!empty($this->format) ? $this->format : $format);
         }
         return var_export($this->entity, 1);
     } else {
         $template_engine = TemplateEngine::getInstance();
         try {
             $template_engine->setToView('setIncludePath', __DIR__ . '/views')->guessFromAssetsLoader(AssetsLoader::getInstance(__DIR__ . '/../../', 'www', defined('_DEVDEBUG_DOCUMENT_ROOT') ? _DEVDEBUG_DOCUMENT_ROOT : __DIR__ . '/../../www'));
         } catch (\Exception $e) {
             return $e->getMessage();
         }
         $template_engine->getTemplateObject('MetaTag')->add('robots', 'none');
         $params = array('debug' => $this, 'reporter' => $template_engine, 'title' => 'The system encountered an error!', 'subheader' => $this->profiler->renderProfilingTitle(), 'slogan' => $this->profiler->renderProfilingInfo(), 'profiler_content' => 'profiler_content.html', 'profiler_footer' => 'profiler_footer.html', 'show_menu' => false, 'show_backtotop_handlers' => false);
         // messages ?
         if (!empty($this->messages)) {
             $params['messages'] = self::renderMessages($this->messages, !empty($this->format) ? $this->format : $format);
         }
         // others
         $params['stacks'] = array();
         $params['menu'] = array();
         foreach (self::getStacks() as $_i => $_stack) {
             $params['menu'][$_stack->getTitle()] = '#' . $_i;
             $params['stacks'][] = self::renderStack($_stack, !empty($this->format) ? $this->format : $format);
         }
         // this will display the layout on screen and exit
         try {
             $params['content'] = $template_engine->render('partial_html.html', $params);
             $str = $template_engine->renderLayout(null, $params);
         } catch (\Exception $e) {
             return $e->getMessage();
         }
         echo $str;
         exit;
     }
     return '';
 }
Example #5
0
 /**
  * Automatic assets loading from an Assets package declare in a `composer.json`
  *
  * @return void
  */
 public function load()
 {
     if (empty($this->_statements)) {
         parent::load();
     }
     /* @var $template_engine \TemplateEngine\TemplateEngine */
     $template_engine = TemplateEngine::getInstance();
     foreach ($this->getOrganizedStatements() as $type => $statements) {
         foreach ($statements as $statement) {
             if ('css' === $type) {
                 $template_object = $template_engine->getTemplateObject('CssFile');
                 $css = $statement->getData();
                 if (isset($css['minified']) && true === $css['minified']) {
                     $template_object->addMinified($css['src'], $css['media']);
                 } else {
                     $template_object->add($css['src'], $css['media']);
                 }
             } elseif ('jsfiles_header' === $type) {
                 $template_object = $template_engine->getTemplateObject('JavascriptFile', 'jsfiles_header');
                 $js = $statement->getData();
                 if (isset($js['minified']) && true === $js['minified'] || isset($js['packed']) && true === $js['packed']) {
                     $template_object->addMinified($js['src']);
                 } else {
                     $template_object->add($js['src']);
                 }
             } elseif (in_array($type, array('js', 'jsfiles_footer'))) {
                 $template_object = $template_engine->getTemplateObject('JavascriptFile', 'jsfiles_footer');
                 $js = $statement->getData();
                 if (isset($js['minified']) && true === $js['minified'] || isset($js['packed']) && true === $js['packed']) {
                     $template_object->addMinified($js['src']);
                 } else {
                     $template_object->add($js['src']);
                 }
             }
         }
     }
 }
 /**
  * Change or set the global page layout
  *
  * @param string $layout The layout name to use
  * @return void
  */
 function _use_layout($layout = null)
 {
     if (empty($layout)) {
         return;
     }
     TemplateEngine::getInstance()->setPageLayout($layout);
 }