Example #1
0
 /**
  * @param Context $context
  * @param bool $ignore_failures
  *
  * @return mixed
  */
 public function resolve($context, $ignore_failures = False)
 {
     if ($this->var instanceof Variable) {
         try {
             $obj = $this->var->resolve($context);
         } catch (VariableDoesNotExist $e) {
             if ($ignore_failures) {
                 $obj = null;
             } else {
                 if (Dja::getSetting('TEMPLATE_STRING_IF_INVALID')) {
                     if (DjaBase::$invalid_var_format_string === null) {
                         DjaBase::$invalid_var_format_string = strpos(Dja::getSetting('TEMPLATE_STRING_IF_INVALID'), '%s') !== False;
                     }
                     if (DjaBase::$invalid_var_format_string) {
                         return sprintf(Dja::getSetting('TEMPLATE_STRING_IF_INVALID'), $this->var);
                     }
                     return Dja::getSetting('TEMPLATE_STRING_IF_INVALID');
                 } else {
                     $obj = Dja::getSetting('TEMPLATE_STRING_IF_INVALID');
                 }
             }
         }
     } else {
         $obj = $this->var;
     }
     foreach ($this->filters as $filter) {
         list($func, $args, $n_) = $filter;
         $arg_vals = array();
         foreach ($args as $arg_data) {
             /** @var $arg Variable|string */
             list($lookup, $arg) = $arg_data;
             if (!$lookup) {
                 $arg_vals[] = mark_safe($arg);
             } else {
                 $arg_vals[] = $arg->resolve($context);
             }
         }
         if (py_getattr($func, 'expects_localtime', False)) {
             $obj = localtime($obj, $context->use_tz);
         }
         $func_ = $func->closure;
         if (py_getattr($func, 'needs_autoescape', False)) {
             $new_obj = call_user_func_array($func_, array_merge(array($obj, $context->autoescape), $arg_vals));
         } else {
             $new_obj = call_user_func_array($func_, array_merge(array($obj), $arg_vals));
         }
         if (py_getattr($func, 'is_safe', False) && $obj instanceof SafeData) {
             $obj = mark_safe($new_obj);
         } else {
             if ($obj instanceof EscapeData) {
                 $obj = mark_for_escaping($new_obj);
             } else {
                 $obj = $new_obj;
             }
         }
     }
     return $obj;
 }
Example #2
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);
     }
 }