protected function compileString(Twig_NodeList $body) { $msg = ''; $vars = array(); foreach ($body->getNodes() as $i => $node) { if ($node instanceof Twig_Node_Text) { $msg .= $node->getData(); } elseif ($node instanceof Twig_Node_Print && $node->getExpression() instanceof Twig_Node_Expression_Name) { $msg .= sprintf('%%%s%%', $node->getExpression()->getName()); $vars[] = $node->getExpression(); } else { throw new Twig_SyntaxError(sprintf('The text to be translated with "trans" can only contain references to simple variable'), $this->lineno); } } return array(trim($msg), $vars); }
public function subparse($test, $drop_needle = false) { $lineno = $this->stream->current->lineno; $rv = array(); while (!$this->stream->eof) { switch ($this->stream->current->type) { case Twig_Token::TEXT_TYPE: $token = $this->stream->next(); $rv[] = new Twig_Text($token->value, $token->lineno); break; case Twig_Token::VAR_START_TYPE: $token = $this->stream->next(); $expr = $this->parseExpression(); $this->stream->expect(Twig_Token::VAR_END_TYPE); $rv[] = new Twig_Print($expr, $token->lineno); break; case Twig_Token::BLOCK_START_TYPE: $this->stream->next(); $token = $this->stream->current; if ($token->type !== Twig_Token::NAME_TYPE) { throw new Twig_SyntaxError('expected directive', $token->lineno); } if (!is_null($test) && call_user_func($test, $token)) { if ($drop_needle) { $this->stream->next(); } return Twig_NodeList::fromArray($rv, $lineno); } if (!isset($this->handlers[$token->value])) { throw new Twig_SyntaxError('unknown directive', $token->lineno); } $this->stream->next(); $node = call_user_func($this->handlers[$token->value], $token); if (!is_null($node)) { $rv[] = $node; } break; default: assert(false, 'Lexer or parser ended up in ' . 'unsupported state.'); } } return Twig_NodeList::fromArray($rv, $lineno); }