예제 #1
0
 /**
  * Returns a list of filters to add to the existing list.
  *
  * @return array An array of filters
  */
 public function getFilters()
 {
     $translation_filter = new \Twig_SimpleFilter('trans', function (\Twig_Environment $env, $string) {
         $globals = $env->getGlobals();
         $translation = $globals['env']['translation'];
         return \Skeleton\I18n\Translation::translate($string, $translation);
     }, ['needs_environment' => true]);
     return [$translation_filter];
 }
예제 #2
0
 /**
  * Render
  *
  * @access public
  * @param string $template
  * @return string $html
  */
 public function render($template)
 {
     if (strpos($template, '.') === false) {
         throw new \Exception('Please provide a valid template filename. Incorrect filename "' . $template . '"');
     }
     list($filename, $extension) = explode('.', basename($template));
     switch ($extension) {
         case 'twig':
             $renderer = new \Skeleton\Template\Twig\Twig();
             break;
         case 'tpl':
             $renderer = new \Skeleton\Template\Smarty\Smarty();
             break;
         default:
             throw new \Exception('Unknown template type');
     }
     if (count($this->template_directories) == 0) {
         throw new \Exception('No template directory set, please set $template->set_template_directory()');
     }
     // Set the template path
     foreach ($this->template_directories as $template_directory) {
         $renderer->add_template_directory($template_directory['directory'], $template_directory['namespace']);
     }
     // Pass the environment variables to the template renderer
     if (count($this->environment) > 0) {
         foreach ($this->environment as $key => $value) {
             $renderer->add_environment($key, $value);
         }
     }
     // Pass the variables to the template renderer
     foreach ($this->variables as $key => $value) {
         $renderer->assign($key, $value);
     }
     // Set the translation object
     if ($this->translation !== null) {
         $renderer->set_translation($this->translation);
     } else {
         if (class_exists('\\Skeleton\\I18n\\Translation') and class_exists('Skeleton\\Core\\Application')) {
             try {
                 $language = \Skeleton\Core\Application::Get()->language;
                 $application_name = \Skeleton\Core\Application::Get()->name;
                 $translation = \Skeleton\I18n\Translation::Get($language, $application_name);
                 $renderer->set_translation($translation);
             } catch (\Exception $e) {
             }
         }
     }
     return $renderer->render($template);
 }