str() public static method

public static str ( $string )
示例#1
0
 static function generator($compiler, $args)
 {
     if (count($args) != 1) {
         $compiler->Error("templatetag only needs one parameter");
     }
     if (Haanga_AST::is_var($args[0])) {
         $type = $args[0]['var'];
         if (!is_string($type)) {
             $compiler->Error("Invalid parameter");
         }
     } else {
         if (Haanga_AST::is_str($args[0])) {
             $type = $args[0]['string'];
         }
     }
     switch ($type) {
         case 'openblock':
             $str = '{%';
             break;
         case 'closeblock':
             $str = '%}';
             break;
         case 'openbrace':
             $str = '{';
             break;
         case 'closebrace':
             $str = '}';
             break;
         case 'openvariable':
             $str = '{{';
             break;
         case 'closevariable':
             $str = '}}';
             break;
         case 'opencomment':
             $str = '{#';
             break;
         case 'closecomment':
             $str = '#}';
             break;
         default:
             $compiler->Error("Invalid parameter");
             break;
     }
     $code = hcode();
     $compiler->do_print($code, Haanga_AST::str($str));
     return $code;
 }
示例#2
0
 function generate_variable_name($variable, $special = true)
 {
     if (is_array($variable)) {
         switch ($variable[0]) {
             case 'forloop':
                 if (!$special) {
                     return array('var' => $variable);
                 }
                 if (!$this->forid) {
                     throw new Exception("Invalid forloop reference outside of a loop");
                 }
                 switch ($variable[1]['object']) {
                     case 'counter':
                         $this->forloop[$this->forid]['counter'] = TRUE;
                         $variable = 'forcounter1_' . $this->forid;
                         break;
                     case 'counter0':
                         $this->forloop[$this->forid]['counter0'] = TRUE;
                         $variable = 'forcounter0_' . $this->forid;
                         break;
                     case 'last':
                         $this->forloop[$this->forid]['counter'] = TRUE;
                         $this->forloop[$this->forid]['last'] = TRUE;
                         $variable = 'islast_' . $this->forid;
                         break;
                     case 'first':
                         $this->forloop[$this->forid]['first'] = TRUE;
                         $variable = 'isfirst_' . $this->forid;
                         break;
                     case 'revcounter':
                         $this->forloop[$this->forid]['revcounter'] = TRUE;
                         $variable = 'revcount_' . $this->forid;
                         break;
                     case 'revcounter0':
                         $this->forloop[$this->forid]['revcounter0'] = TRUE;
                         $variable = 'revcount0_' . $this->forid;
                         break;
                     case 'parentloop':
                         unset($variable[1]);
                         $this->forid--;
                         $variable = $this->generate_variable_name(array_values($variable));
                         $variable = $variable['var'];
                         $this->forid++;
                         break;
                     default:
                         throw new Exception("Unexpected forloop.{$variable[1]}");
                 }
                 /* no need to escape it */
                 $this->var_is_safe = TRUE;
                 break;
             case 'block':
                 if (!$special) {
                     return array('var' => $variable);
                 }
                 if ($this->in_block == 0) {
                     throw new Exception("Can't use block.super outside a block");
                 }
                 if (!$this->subtemplate) {
                     throw new Exception("Only subtemplates can call block.super");
                 }
                 /* no need to escape it */
                 $this->var_is_safe = TRUE;
                 return Haanga_AST::str(self::$block_var);
                 break;
             default:
                 /* choose array or objects */
                 if ($special) {
                     // this section is resolved on the parser.y
                     return array('var' => $variable);
                 }
                 for ($i = 1; $i < count($variable); $i++) {
                     $var_part = array_slice($variable, 0, $i);
                     $def_arr = TRUE;
                     if (is_array($variable[$i])) {
                         if (isset($variable[$i]['class'])) {
                             // no type guess for static properties
                             continue;
                         }
                         if (isset($variable[$i]['object'])) {
                             $def_arr = FALSE;
                         }
                         if (!Haanga_AST::is_var($variable[$i])) {
                             $variable[$i] = current($variable[$i]);
                         } else {
                             $variable[$i] = $this->generate_variable_name($variable[$i]['var']);
                         }
                     }
                     $is_obj = $this->var_is_object($var_part, 'unknown');
                     if ($is_obj === TRUE || $is_obj == 'unknown' && !$def_arr) {
                         $variable[$i] = array('object' => $variable[$i]);
                     }
                 }
                 break;
         }
     } else {
         if (isset($this->var_alias[$variable])) {
             $variable = $this->var_alias[$variable]['var'];
         }
     }
     return hvar($variable)->getArray();
 }
 protected function generate_variable_name($variable)
 {
     if (is_array($variable)) {
         switch ($variable[0]) {
             case 'forloop':
                 if (!$this->forid) {
                     $this->Error("Invalid forloop reference outside of a loop");
                 }
                 switch ($variable[1]) {
                     case 'counter':
                         $this->forloop[$this->forid]['counter'] = TRUE;
                         $variable = 'forcounter1_' . $this->forid;
                         break;
                     case 'counter0':
                         $this->forloop[$this->forid]['counter0'] = TRUE;
                         $variable = 'forcounter0_' . $this->forid;
                         break;
                     case 'last':
                         $this->forloop[$this->forid]['counter'] = TRUE;
                         $this->forloop[$this->forid]['last'] = TRUE;
                         $variable = 'islast_' . $this->forid;
                         break;
                     case 'first':
                         $this->forloop[$this->forid]['first'] = TRUE;
                         $variable = 'isfirst_' . $this->forid;
                         break;
                     case 'revcounter':
                         $this->forloop[$this->forid]['revcounter'] = TRUE;
                         $variable = 'revcount_' . $this->forid;
                         break;
                     case 'revcounter0':
                         $this->forloop[$this->forid]['revcounter0'] = TRUE;
                         $variable = 'revcount0_' . $this->forid;
                         break;
                     case 'parentloop':
                         unset($variable[1]);
                         $this->forid--;
                         $variable = $this->generate_variable_name(array_values($variable));
                         $variable = $variable['var'];
                         $this->forid++;
                         break;
                     default:
                         $this->Error("Unexpected forloop.{$variable[1]}");
                 }
                 /* no need to escape it */
                 $this->var_is_safe = TRUE;
                 break;
             case 'block':
                 if ($this->in_block == 0) {
                     $this->Error("Can't use block.super outside a block");
                 }
                 if (!$this->subtemplate) {
                     $this->Error("Only subtemplates can call block.super");
                 }
                 /* no need to escape it */
                 $this->var_is_safe = TRUE;
                 return Haanga_AST::str(self::$block_var);
                 break;
         }
     } else {
         if (isset($this->var_alias[$variable])) {
             $variable = $this->var_alias[$variable];
         }
     }
     return hvar($variable)->getArray();
 }