/** * @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); }
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_); }
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); } }