public function evalTalesExpression($expression) { if ($this->_talesMode == 'php') { return PHPTAL_TalesInternal::php($expression); } return phptal_tales($expression); }
/** translates array of alternative expressions into single PHP expression. Identical to phptal_tales() for singular expressions. */ function phptal_tale($expression, $nothrow = false) { $r = phptal_tales($expression, true); if (!is_array($r)) { return $r; } // this weird ternary operator construct is to execute noThrow inside the expression return '($ctx->noThrow(true)||1?' . _phptal_tale_wrap($r, $nothrow) . ':"")'; }
public static function SplitSRC(&$src, $nothrow) { $pos = strpos($src, '|'); if ($pos !== false) { $exp = phptal_tales(substr($src, $pos + 1), $nothrow); $src = substr($src, 0, $pos); if (!is_array($exp)) { $exp = array($exp); } } else { $exp = array(); } return $exp; }
function testNamespaceConstant() { if (version_compare(PHP_VERSION, '5.3', '<')) { $this->markTestSkipped(); } $this->assertEquals('My\\Foo::TAU', phptal_tales('php:My\\Foo::TAU')); $this->assertEquals('$ctx->date_filter->isFilterApplied(\\My\\Foo::TODAY)', phptal_tales("php: date_filter.isFilterApplied(\\My\\Foo::TODAY)")); }
function registry_test_callback2($arg, $nothrow) { return '"ok2" . ' . phptal_tales($arg); }
public static function path($expression, $nothrow = false) { $expression = trim($expression); if ($expression == 'default') { return PHPTAL_TALES_DEFAULT_KEYWORD; } if ($expression == 'nothing') { return PHPTAL_TALES_NOTHING_KEYWORD; } if ($expression == '') { return PHPTAL_TALES_NOTHING_KEYWORD; } // split OR expressions terminated by a string if (preg_match('/^(.*?)\\s*?\\|\\s*?(string:.*?)$/sm', $expression, $m)) { list(, $expression, $string) = $m; } else { if (preg_match('/^(.*?)\\s*?\\|\\s*\'((?:[^\'\\\\]|\\\\.)*)\'\\s*$/sm', $expression, $m)) { list(, $expression, $string) = $m; $string = 'string:' . stripslashes($string); } } // split OR expressions $exps = preg_split('/\\s*?\\|\\s*?/sm', $expression); // if (many expressions) or (expressions or terminating string) found then // generate the array of sub expressions and return it. if (count($exps) > 1 || isset($string)) { $result = array(); foreach ($exps as $exp) { $result[] = phptal_tales(trim($exp), true); } if (isset($string)) { $result[] = phptal_tales($string, true); } return $result; } // only one expression to process // first evaluate ${foo} inside the expression and threat the expression // as if it was a string to interpolate $expression = self::string($expression); $expression = substr($expression, 1, -1); $pos = strpos($expression, '/'); // if no sub part for this expression, just optimize the generated code // and access the $ctx->var if ($pos === false) { if (!self::checkExpressionPart($expression)) { throw new PHPTAL_Exception("Invalid TALES path: '{$expression}', expected variable name"); } return '$ctx->' . $expression; } // otherwise we have to call phptal_path() to resolve the path at runtime // extract the first part of the expression (it will be the phptal_path() // $base and pass the remaining of the path to phptal_path() $next = substr($expression, 0, $pos); $expression = substr($expression, $pos + 1); if (!self::checkExpressionPart($next)) { throw new PHPTAL_Exception("Invalid TALES path: '{$next}/{$expression}', expected '{$next}' to be variable name"); } // return php code invoking phptal_path($next, $expression, $notrhow) return 'phptal_path($ctx->' . $next . ', \'' . $expression . '\'' . ($nothrow ? ', true' : '') . ')'; }
public static function path($expression, $nothrow = false) { $expression = trim($expression); if ($expression == 'default') { return PHPTAL_TALES_DEFAULT_KEYWORD; } if ($expression == 'nothing') { return PHPTAL_TALES_NOTHING_KEYWORD; } if ($expression == '') { return PHPTAL_TALES_NOTHING_KEYWORD; } // split OR expressions terminated by a string if (preg_match('/^(.*?)\\s*\\|\\s*?(string:.*)$/sm', $expression, $m)) { list(, $expression, $string) = $m; } else { if (preg_match('/^(.*?)\\s*\\|\\s*\'((?:[^\'\\\\]|\\\\.)*)\'\\s*$/sm', $expression, $m)) { list(, $expression, $string) = $m; $string = 'string:' . stripslashes($string); } } // split OR expressions $exps = preg_split('/\\s*\\|\\s*/sm', $expression); // if (many expressions) or (expressions or terminating string) found then // generate the array of sub expressions and return it. if (count($exps) > 1 || isset($string)) { $result = array(); foreach ($exps as $exp) { $result[] = phptal_tales(trim($exp), true); } if (isset($string)) { $result[] = phptal_tales($string, true); } return $result; } // see if there are subexpressions, but skip interpolated parts, i.e. ${a/b}/c is 2 parts if (preg_match('/^((?:[^$\\/]+|\\$\\$|\\${[^}]+}|\\$))\\/(.+)$/', $expression, $m)) { if (!self::checkExpressionPart($m[1])) { throw new PHPTAL_ParserException("Invalid TALES path: '{$expression}', expected '{$m[1]}' to be variable name"); } $next = self::string($m[1]); $expression = self::string($m[2]); } else { if (!self::checkExpressionPart($expression)) { throw new PHPTAL_ParserException("Invalid TALES path: '{$expression}', expected variable name"); } $next = self::string($expression); $expression = NULL; } if (preg_match('/^\'[a-z][a-z0-9_]*\'$/i', $next)) { $next = substr($next, 1, -1); } else { $next = '{' . $next . '}'; } // if no sub part for this expression, just optimize the generated code // and access the $ctx->var if ($expression === NULL) { return '$ctx->' . $next; } // otherwise we have to call phptal_path() to resolve the path at runtime // extract the first part of the expression (it will be the phptal_path() // $base and pass the remaining of the path to phptal_path() return 'phptal_path($ctx->' . $next . ', ' . $expression . ($nothrow ? ', true' : '') . ')'; }
/** * Tal to support filtering of an array. * * Example usage: * * <span * tal:define="keys string:key1,key2" * tal:repeat="arr Ztal_Tales_Generic.arrayFilter:string:mode,keys,array * /> * * mode may be: * key - Filter items by the array key. * value - Filter items by the array value. * * The last parameter is the original array to filter, this should be a * PHPTAL variable. * * @param string $src The original template string. * @param bool $nothrow Whether to throw an exception on error. * * @return string */ public static function arrayFilter($src, $nothrow) { $regex = "/([a-zA-Z:]+)\\s*?,\\s*?([a-zA-Z0-9:]+)\\s*?,\\s*?([^|\$]+)\$/"; $src = trim($src); // If we can't find a match for our parameters simply return NULL. if (!preg_match($regex, $src, $items)) { return phptal_tales('NULL', $nothrow); } // Call the array filtering helper with: // // $items[1] = Type of filtering (e.g. key or value). // $items[2] = PHPTAL variable (array) of items to filter with, or a // string of comma seperated items, // $items[3] = PHPTAL variable of haystack array. // true = Exclude rather than filter. return "Ztal_Tales_Generic::arrayFilterHelper(\n\n\t\t\t" . phptal_tale($items[1], $nothrow) . ",\n\t\t\t" . phptal_tale($items[2], $nothrow) . ",\n\t\t\t" . phptal_tale($items[3], $nothrow) . ")"; }
function phptal_tales_fullurl($src, $nothrow) { $src = trim($src); if (preg_match('/^[a-z0-9_]+:/i', $src)) { $src = phptal_tales($src, $nothrow); } else { if (preg_match('/^[a-z0-9_]+\\(/i', $src)) { $src = phptal_tales('php:' . $src, $nothrow); } else { $src = "'" . $src . "'"; } } return 'Router::url(' . $src . ', true)'; }
/** * Generic tales modifier for Zend_View helpers * * @param string $exp * @param bool $nothrow */ function phptal_tales_helper($exp, $nothrow) { if (preg_match('/^([A-Za-z_]+)\\(([^)]*)\\)/', $exp, $m)) { $args = explode(',', $m[2]); foreach ($args as &$arg) { $arg = phptal_tale($arg, $nothrow); } $exp = 'php:this->' . $m[1] . '(' . implode(', ', $args) . ')'; } else { list($helper, $param) = explode(' ', $exp, 2); $exp = 'php:this->' . $helper . '(' . phptal_tales($param, $nothrow) . ')'; } return phptal_tale($exp, $nothrow); }
/** * Tal extension to allow counting of items. * * Example use within template: * <span class="item" tal:content="Ztal\Tales\Generic.count:array,ticket/posts">1</span> * * @param string $src The original template string. * @param bool $nothrow Whether to throw an exception on error. * * @static * @return string */ public static function count($src, $nothrow) { $break = strpos($src, ','); $command = strtolower(substr($src, 0, $break)); $src = substr($src, $break + 1); $break = strpos($src, '|'); if ($break === false) { $string = $src; $rest = 'NULL'; } else { $string = substr($src, 0, $break); $rest = substr($src, $break + 1); } switch ($command) { case 'string': return 'strlen(' . phptal_tale($src, $nothrow) . ')'; break; default: case 'array': return 'count(' . phptal_tale($src, $nothrow) . ')'; break; } return phptal_tales($rest, $nothrow); }