Exemple #1
0
 public function testArrVal()
 {
     $input_arr = array('first', 'second', 'third', 'fourth');
     $this->assertEquals('second', py_arr_get($input_arr, 1));
     $this->assertEquals('third', py_arr_get($input_arr, 2));
     $this->assertEquals('third', py_arr_get($input_arr, -2));
     $this->assertEquals('second', py_arr_get($input_arr, -3));
 }
Exemple #2
0
 public function getBlock($name)
 {
     try {
         return py_arr_get($this->blocks[$name], -1);
     } catch (KeyError $e) {
         // IndexError, KeyError
         return null;
     }
 }
Exemple #3
0
 /**
  * Load the template library module with the given name.
  *
  * If library is not already loaded loop over all templatetags modules to locate it.
  *
  * {% load somelib %} and {% load someotherlib %} loops twice.
  *
  * Subsequent loads eg. {% load somelib %} in the same process will grab the cached module from libraries.
  *
  * @static
  *
  * @param string $library_name
  *
  * @return Library
  * @throws InvalidTemplateLibrary
  */
 public static function getLibrary($library_name)
 {
     $lib = py_arr_get(self::$libraries, $library_name, null);
     if (!$lib) {
         $templatetags_modules = self::getTemplatetagsModules();
         $tried_modules = array();
         foreach ($templatetags_modules as $module) {
             $taglib_module = $module . '.' . $library_name;
             $tried_modules[] = $taglib_module;
             $lib = self::importLibrary($taglib_module);
             if ($lib) {
                 self::$libraries[$library_name] = $lib;
                 break;
             }
         }
         if (!$lib) {
             throw new InvalidTemplateLibrary('Template library ' . $library_name . ' not found, tried `' . join(',', $tried_modules) . '`');
         }
     }
     return $lib;
 }
Exemple #4
0
$lib->tag('url', function ($parser, $token) {
    /**
     * @var Parser $parser
     * @var Token $token
     */
    $bits = $token->splitContents();
    if (count($bits) < 2) {
        throw new TemplateSyntaxError('\'url\' takes at least one argument (path to a view)');
    }
    $viewname = $parser->compileFilter($bits[1]);
    $args = array();
    $kwargs = array();
    $asvar = null;
    $bits = py_slice($bits, 2);
    if (count($bits) >= 2 && py_arr_get($bits, -2) == 'as') {
        $asvar = py_arr_get($bits, -1);
        $bits = py_slice($bits, null, -2);
    }
    if (count($bits)) {
        foreach ($bits as $bit) {
            $match = py_re_match(DjaBase::$re_kwarg, $bit);
            if (!$match) {
                throw new TemplateSyntaxError('Malformed arguments to url tag');
            }
            list($name, $value) = $match->groups();
            if ($name) {
                $kwargs[$name] = $parser->compileFilter($value);
            } else {
                $args[] = $parser->compileFilter($value);
            }
        }
Exemple #5
0
 /**
  * @param Context $context
  * @return SafeString|string
  */
 public function render($context)
 {
     return py_arr_get(self::$mapping, $this->tagtype, '');
 }
Exemple #6
0
        return $value . $arg;
    }
    if (is_numeric($value) && is_numeric($arg)) {
        return $value + $arg;
    }
    if (is_array($value) && is_array($arg)) {
        return '[' . join(', ', array_merge($value, $arg)) . ']';
    }
    return '';
}, array('is_safe' => False));
$lib->filter('get_digit', function ($value, $arg) {
    $arg = (string) $arg;
    if (!is_numeric($value) || !is_numeric($arg) || $arg < 1) {
        return $value;
    }
    return py_arr_get(str_split($value), -$arg, 0);
}, array('is_safe' => False));
/*
 * DATES
 */
$lib->filter('date', function ($value, $arg = null) {
    return dja_date($value, $arg);
}, array('is_safe' => False, $expects_localtime = True));
// TODO time
// TODO timesince
// TODO timeuntil
/*
 * LOGIC
 */
$lib->filter('default', function ($value, $arg) {
    return $value ? $value : $arg;