public function compile(\Twig_Compiler $compiler) { if (PHP_VERSION_ID >= 70000) { $compiler->raw('is_numeric(')->subcompile($this->getNode('node'))->raw(')'); return; } // Forward support towards PHP 7. // Strings in hexadecimal notation are no longer regarded as `numeric`, // for example: `is_numeric('0xf4c3b00c')` returns `false`. // @see https://secure.php.net/manual/en/function.is-numeric.php $var = $compiler->getVarName(); $compiler->raw(sprintf('(is_numeric($%s = ', $var))->subcompile($this->getNode('node'))->raw(sprintf(') && (!is_string($%s) || (strlen($%s) < 2 || (\'x\' !== $%s[1] && \'X\' !== $%s[1]))))', $var, $var, $var, $var)); }
public function compile(Twig_Compiler $compiler) { $compiler->addDebugInfo($this); if ($this->hasNode('variables')) { $varsName = $compiler->getVarName(); $compiler->write(sprintf('$%s = ', $varsName))->subcompile($this->getNode('variables'))->raw(";\n")->write(sprintf("if (!is_array(\$%s)) {\n", $varsName))->indent()->write("throw new Twig_Error_Runtime('Variables passed to the \"with\" tag must be a hash.');\n")->outdent()->write("}\n"); if ($this->getAttribute('only')) { $compiler->write("\$context = array('_parent' => \$context);\n"); } else { $compiler->write("\$context['_parent'] = \$context;\n"); } $compiler->write(sprintf("\$context = array_merge(\$context, \$%s);\n", $varsName)); } else { $compiler->write("\$context['_parent'] = \$context;\n"); } $compiler->subcompile($this->getNode('body'))->write("\$context = \$context['_parent'];\n"); }
public function compile(Twig_Compiler $compiler) { $left = $compiler->getVarName(); $right = $compiler->getVarName(); $compiler->raw(sprintf('(is_string($%s = ', $left))->subcompile($this->getNode('left'))->raw(sprintf(') && is_string($%s = ', $right))->subcompile($this->getNode('right'))->raw(sprintf(') && (\'\' === $%2$s || 0 === strpos($%1$s, $%2$s)))', $left, $right)); }