Beispiel #1
0
 public function testStrStartsWith()
 {
     $this->assertFalse(py_str_starts_with('abcd', 'q'));
     $this->assertFalse(py_str_starts_with(' abcd', 'ab'));
     $this->assertFalse(py_str_starts_with(' фыв', 'ab'));
     $this->assertFalse(py_str_starts_with('', 'abcd'));
     $this->assertTrue(py_str_starts_with('source', 'so'));
 }
Beispiel #2
0
 /**
  * @param string $var
  * @throws TemplateSyntaxError
  */
 public function __construct($var)
 {
     $this->var = $var;
     $this->literal = null;
     $this->lookups = null;
     $this->translate = False;
     $this->message_context = null;
     try {
         // First try to treat this variable as a number.
         if (!is_numeric($var)) {
             throw new ValueError();
         }
         $this->literal = (double) $var;
         /*
          * So it's a float... is it an int? If the original value contained a
          * dot or an "e" then it was a float, not an int.
          */
         if (strpos($var, '.') === False && strpos(strtolower($var), 'e') === False) {
             $this->literal = (int) $this->literal;
         }
         // "2." is invalid
         if (py_str_ends_with($var, '.')) {
             throw new ValueError();
         }
     } catch (ValueError $e) {
         // A ValueError means that the variable isn't a number.
         if (py_str_starts_with($var, '_(') && py_str_ends_with($var, ')')) {
             // The result of the lookup should be translated at rendering time.
             $this->translate = True;
             $var = py_slice($var, 2, -1);
         }
         // If it's wrapped with quotes (single or double), then we're also dealing with a literal.
         try {
             $this->literal = mark_safe(unescape_string_literal($var));
         } catch (ValueError $e) {
             // Otherwise we'll set self.lookups so that resolve() knows we're dealing with a bonafide variable
             if (strpos($var, DjaBase::VARIABLE_ATTRIBUTE_SEPARATOR . '_') !== False || $var[0] == '_') {
                 throw new TemplateSyntaxError('Variables and attributes may not begin with underscores: \'' . $var . '\'');
             }
             $this->lookups = explode(DjaBase::VARIABLE_ATTRIBUTE_SEPARATOR, $var);
         }
     }
 }
Beispiel #3
0
    }
}
$lib->tag('if', function ($parser, $token) {
    /**
     * @var Parser $parser
     * @var Token $token
     */
    // {% if ... %}
    $bits = py_slice($token->splitContents(), 1);
    $condition = new TemplateIfParser($parser, $bits);
    $condition = $condition->parse();
    $nodelist = $parser->parse(array('elif', 'else', 'endif'));
    $conditions_nodelists = array(array($condition, $nodelist));
    $token = $parser->nextToken();
    // {% elif ... %} (repeatable)
    while (py_str_starts_with($token->contents, 'elif')) {
        $bits = py_slice($token->splitContents(), 1);
        $condition = new TemplateIfParser($parser, $bits);
        $condition = $condition->parse();
        $nodelist = $parser->parse(array('elif', 'else', 'endif'));
        $conditions_nodelists[] = array($condition, $nodelist);
        $token = $parser->nextToken();
    }
    // {% else %} (optional)
    if ($token->contents == 'else') {
        $nodelist = $parser->parse(array('endif'));
        $conditions_nodelists[] = array(null, $nodelist);
        $token = $parser->nextToken();
    }
    // {% endif %}
    assert($token->contents == 'endif');