コード例 #1
0
ファイル: cache.php プロジェクト: idlesign/dja
 public function render($context)
 {
     try {
         $expire_time = $this->expire_time_var->resolve($context);
     } catch (VariableDoesNotExist $e) {
         throw new TemplateSyntaxError('"cache" tag got an unknown variable: ' . $this->expire_time_var->var);
     }
     if (!is_numeric($expire_time)) {
         throw new TemplateSyntaxError('"cache" tag got a non-integer timeout value: ' . print_r($expire_time, true));
     }
     $expire_time = (int) $expire_time;
     // Build a unicode key for this fragment and all vary-on's.
     $vs_ = array();
     foreach ($this->vary_on as $var) {
         $v_ = new Variable($var);
         $vs_[] = urlencode($v_->resolve($context));
     }
     $args = join(':', $vs_);
     unset($vs_);
     $cache_key = 'template.cache.' . $this->fragment_name . '.' . md5($args);
     $manager = Dja::getCacheManager();
     $value = $manager->get($cache_key);
     if ($value === null) {
         $value = $this->nodelist->render($context);
         $manager->set($cache_key, $value, $expire_time);
     }
     return $value;
 }
コード例 #2
0
ファイル: parser.php プロジェクト: idlesign/dja
 public function testVariableParsing()
 {
     $c = array('article' => array('section' => 'News'));
     $v = new Variable('article.section');
     $this->assertEquals('News', $v->resolve($c));
     $v = new Variable('"News"');
     $this->assertEquals('News', $v->resolve($c));
     $v = new Variable("'News'");
     $this->assertEquals('News', $v->resolve($c));
     // Translated strings are handled correctly.
     $v = new Variable('_(article.section)');
     $this->assertEquals('News', $v->resolve($c));
     $v = new Variable('_("Good News")');
     $this->assertEquals('Good News', $v->resolve($c));
     $v = new Variable("_('Better News')");
     $this->assertEquals('Better News', $v->resolve($c));
     // Escaped quotes work correctly as well.
     $v = new Variable('"Some \\"Good\\" News"');
     $this->assertEquals('Some "Good" News', $v->resolve($c));
     $v = new Variable("'Some 'Better' News'");
     $this->assertEquals("Some 'Better' News", $v->resolve($c));
     // Variables should reject access of attributes beginning with underscores.
     $this->setExpectedException('TemplateSyntaxError');
     new Variable('article._hidden');
 }
コード例 #3
0
ファイル: base.php プロジェクト: idlesign/dja
 /**
  * @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;
 }