Пример #1
0
 /**
  * @param $template_name
  * @param null|array $template_dirs
  * @return array
  */
 public function loadTemplate($template_name, $template_dirs = null)
 {
     $key = $template_name;
     if ($template_dirs) {
         // If template directories were specified, use a hash to differentiate
         $key = join('-', array($template_name, sha1(join('|', $template_dirs))));
     }
     if (!isset($this->template_cache[$key])) {
         list($template, $origin) = $this->findTemplate($template_name, $template_dirs);
         if (!py_hasattr($template, 'render')) {
             try {
                 $template = DjaLoader::getTemplateFromString($template, $origin, $template_name);
             } catch (TemplateDoesNotExist $e) {
                 /*
                  * If compiling the template we found raises TemplateDoesNotExist,
                  * back off to returning the source and display name for the template
                  * we were asked to load. This allows for correct identification (later)
                  * of the actual template that does not exist.
                  */
                 return array($template, $origin);
             }
         }
         $this->template_cache[$key] = $template;
     }
     return array($this->template_cache[$key], null);
 }
Пример #2
0
 public function testCorrectExceptionIndex()
 {
     $debug_old_ = Dja::getSetting('TEMPLATE_DEBUG');
     Dja::setSetting('TEMPLATE_DEBUG', True);
     $tests = array(array('{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% endfor %}', array(38, 56)), array('{% load bad_tag %}{% for i in range %}{% for j in range %}{% badsimpletag %}{% endfor %}{% endfor %}', array(58, 76)), array('{% load bad_tag %}{% for i in range %}{% badsimpletag %}{% for j in range %}Hello{% endfor %}{% endfor %}', array(38, 56)), array('{% load bad_tag %}{% for i in range %}{% for j in five %}{% badsimpletag %}{% endfor %}{% endfor %}', array(38, 57)), array('{% load bad_tag %}{% for j in five %}{% badsimpletag %}{% endfor %}', array(18, 37)));
     // {% for j in five %}
     // {% badsimpletag %}
     $context = new Context(array('range' => array(1, 2, 3, 4, 5), 'five' => 5));
     foreach ($tests as $item) {
         list($source, $expected_error_source_index) = $item;
         $template = DjaLoader::getTemplateFromString($source);
         try {
             $template->render($context);
         } catch (RuntimeError $e) {
             // TODO except (RuntimeError, TypeError), e:
             $error_source_index = $e->django_template_source[1];
             $this->assertEquals($expected_error_source_index, $error_source_index);
         }
     }
     Dja::setSetting('TEMPLATE_DEBUG', $debug_old_);
 }
Пример #3
0
/**
 * Restores the original template loaders after
 * :meth:`setup_test_template_loader` has been run.
 */
function restore_template_loaders()
{
    DjaLoader::$template_source_loaders = $GLOBALS['RESTORE_LOADERS_ATTR'];
    unset($GLOBALS['RESTORE_LOADERS_ATTR']);
}
Пример #4
0
 public function render($context)
 {
     try {
         $template_name = $this->template_name->resolve($context);
         $template = DjaLoader::getTemplate($template_name);
         return $this->renderTemplate($template, $context);
     } catch (Exception $e) {
         if (Dja::getSetting('TEMPLATE_DEBUG')) {
             throw $e;
         }
         return '';
     }
 }
Пример #5
0
 /**
  * Simple rendering method.
  *
  * Handles Dja exceptions and renders pretty error
  * page if TEMPLATE_DEBUG = True.
  *
  * @static
  * @param string $template
  * @param array $context
  * @param bool $use_cache Use compiled template object cache.
  * @return string
  * @throws DjaException
  */
 public static function render($template, $context, $use_cache = True)
 {
     Dja::setSetting('TEMPLATE_CACHE', $use_cache);
     try {
         $result = DjaLoader::renderToString($template, $context);
     } catch (DjaException $e) {
         if (!Dja::getSetting('TEMPLATE_DEBUG')) {
             throw $e;
         }
         $result = DjaDebug::getTracebackHtml($template, $e);
     }
     return (string) $result;
 }
Пример #6
0
 public function testTemplates()
 {
     $template_tests = self::getTemplateTests();
     $filter_tests = get_filter_tests();
     /*
      * Quickly check that we aren't accidentally using a name in both
      * template and filter tests.
      */
     $overlapping_names = array();
     $tkeys_ = array_keys($template_tests);
     foreach ($filter_tests as $name => $v) {
         if (array_key_exists($name, $tkeys_)) {
             $overlapping_names[] = $name;
         }
     }
     if (!empty($overlapping_names)) {
         throw new Exception('Duplicate test name(s): ' . join(', ', $overlapping_names));
     }
     $template_tests = array_merge($template_tests, $filter_tests);
     $tpls_ = array();
     foreach ($template_tests as $name => $t) {
         $tpls_[$name] = $t[0];
     }
     $cache_loader = setup_test_template_loader($tpls_, True);
     $failures = array();
     $tests = $template_tests;
     ksort($tests);
     // Turn TEMPLATE_DEBUG off, because tests assume that.
     $old_debug = Dja::getSetting('TEMPLATE_DEBUG');
     Dja::setSetting('TEMPLATE_DEBUG', True);
     // Set TEMPLATE_STRING_IF_INVALID to a known string.
     $old_invalid = Dja::getSetting('TEMPLATE_STRING_IF_INVALID');
     $expected_invalid_str = 'INVALID';
     // Set ALLOWED_INCLUDE_ROOTS so that ssi works.
     $old_allowed_include_roots = Dja::getSetting('ALLOWED_INCLUDE_ROOTS');
     Dja::setSetting('ALLOWED_INCLUDE_ROOTS', array(realpath(dirname(__FILE__))));
     // Warm the URL reversing cache. This ensures we don't pay the cost
     // warming the cache during one of the tests.
     Dja::getUrlDispatcher()->reverse('regressiontests.templates.views.client_action', null, array(), array('id' => 0, 'action' => "update"));
     foreach ($tests as $name => $vals) {
         if (is_array($vals[2])) {
             $normal_string_result = $vals[2][0];
             $invalid_string_result = $vals[2][1];
             if (is_array($invalid_string_result)) {
                 $expected_invalid_str = 'INVALID %s';
                 $invalid_string_result = sprintf($invalid_string_result[0], $invalid_string_result[1]);
                 DjaBase::$invalid_var_format_string = True;
             }
             if (isset($vals[2][2])) {
                 $template_debug_result = $vals[2][2];
             } else {
                 $template_debug_result = $normal_string_result;
             }
         } else {
             $normal_string_result = $vals[2];
             $invalid_string_result = $vals[2];
             $template_debug_result = $vals[2];
         }
         if (isset($vals[1]['LANGUAGE_CODE'])) {
             Dja::getI18n()->activate($vals[1]['LANGUAGE_CODE']);
         } else {
             Dja::getI18n()->activate('en-us');
         }
         foreach (array(array('', False, $normal_string_result), array($expected_invalid_str, False, $invalid_string_result), array('', True, $template_debug_result)) as $itm) {
             list($invalid_str, $template_debug, $result) = $itm;
             Dja::setSetting('TEMPLATE_STRING_IF_INVALID', $invalid_str);
             Dja::setSetting('TEMPLATE_DEBUG', $template_debug);
             foreach (array(False, True) as $is_cached) {
                 $fail_str_ = 'Template test (Cached=' . ($is_cached ? 'TRUE' : 'FALSE') . ', TEMPLATE_STRING_IF_INVALID=\'' . $invalid_str . '\', TEMPLATE_DEBUG=' . ($template_debug ? 'TRUE' : 'FALSE') . '): ' . $name . ' -- FAILED. ';
                 try {
                     try {
                         $test_template = DjaLoader::getTemplate($name);
                     } catch (ShouldNotExecuteException $e) {
                         $failures[] = $fail_str_ . 'Template loading invoked method that shouldn\'t have been invoked.';
                     }
                     try {
                         $output = self::render($test_template, $vals);
                     } catch (ShouldNotExecuteException $e) {
                         $failures[] = $fail_str_ . 'Template loading invoked method that shouldn\'t have been invoked.';
                     }
                 } catch (ContextStackException $e) {
                     $failures[] = $fail_str_ . 'Context stack was left imbalanced';
                     continue;
                 } catch (Exception $e) {
                     $exc_type = get_class($e);
                     $exc_value = $e->getMessage();
                     $exc_tb = $e->getTraceAsString();
                     if ($exc_type != $result) {
                         $tb = $exc_tb;
                         $failures[] = $fail_str_ . 'Got ' . $exc_type . ', exception: ' . $exc_value . "\n" . $tb;
                     }
                     continue;
                 }
                 if ($output != $result) {
                     $failures[] = $fail_str_ . 'Expected [' . $result . '], got [' . $output . ']';
                 }
             }
             $cache_loader->reset();
         }
         if (isset($vals[1]['LANGUAGE_CODE'])) {
             Dja::getI18n()->deactivate();
         }
         if (DjaBase::$invalid_var_format_string) {
             $expected_invalid_str = 'INVALID';
             DjaBase::$invalid_var_format_string = False;
         }
     }
     restore_template_loaders();
     Dja::getI18n()->deactivate();
     Dja::setSetting('TEMPLATE_STRING_IF_INVALID', $old_invalid);
     Dja::setSetting('TEMPLATE_DEBUG', $old_debug);
     Dja::setSetting('ALLOWED_INCLUDE_ROOTS', $old_allowed_include_roots);
     $sep_ = str_pad('', 70, '-');
     $this->assertEquals(array(), $failures, "Tests failed:\n{$sep_}\n" . join("\n{$sep_}\n", $failures));
 }
Пример #7
0
 public function loadTemplate($template_name, $template_dirs = null)
 {
     list($source, $display_name) = $this->loadTemplateSource($template_name, $template_dirs);
     $origin = DjaLoader::makeOrigin($display_name, py_getattr($this, 'loadTemplateSource'), $template_name, $template_dirs);
     try {
         $template = DjaLoader::getTemplateFromString($source, $origin, $template_name);
         return array($template, null);
     } catch (TemplateDoesNotExist $e) {
         /*
          * If compiling the template we found raises TemplateDoesNotExist, back off to
          * returning the source and display name for the template we were asked to load.
          * This allows for correct identification (later) of the actual template that does not exist.
          */
         return array($source, $display_name);
     }
 }