Latte\Parser::parse PHP Method

parse() public method

Process all {macros} and .
public parse ( $input ) : latte\Token[]
return latte\Token[]
    public function parse($input)
    {
        if (Helpers::startsWith($input, "")) {
            // BOM
            $input = substr($input, 3);
        }
        $this->input = $input = str_replace("\r\n", "\n", $input);
        $this->offset = 0;
        $this->output = [];
        if (!preg_match('##u', $input)) {
            preg_match('#(?:[\\x00-\\x7F]|[\\xC0-\\xDF][\\x80-\\xBF]|[\\xE0-\\xEF][\\x80-\\xBF]{2}|[\\xF0-\\xF7][\\x80-\\xBF]{3})*+#A', $input, $m);
            $this->offset = strlen($m[0]) + 1;
            throw new \InvalidArgumentException('Template is not valid UTF-8 stream.');
        }
        $this->setSyntax($this->defaultSyntax);
        $this->lastHtmlTag = $this->syntaxEndTag = NULL;
        $tokenCount = 0;
        while ($this->offset < strlen($input)) {
            if ($this->{'context' . $this->context[0]}() === FALSE) {
                break;
            }
            while ($tokenCount < count($this->output)) {
                $this->filter($this->output[$tokenCount++]);
            }
        }
        if ($this->context[0] === self::CONTEXT_MACRO) {
            throw new CompileException('Malformed macro');
        }
        if ($this->offset < strlen($input)) {
            $this->addToken(Token::TEXT, substr($this->input, $this->offset));
        }
        return $this->output;
    }

Usage Example

 /**
  * Extracts translation messages from a file to the catalogue.
  *
  * @param string           $file The path to look into
  * @param MessageCatalogue $catalogue The catalogue
  */
 public function extractFile($file, MessageCatalogue $catalogue)
 {
     $buffer = NULL;
     $parser = new Parser();
     $parser->shortNoEscape = TRUE;
     foreach ($tokens = $parser->parse(file_get_contents($file)) as $token) {
         if ($token->type !== $token::MACRO_TAG || !in_array($token->name, array('_', '/_'), TRUE)) {
             if ($buffer !== NULL) {
                 $buffer .= $token->text;
             }
             continue;
         }
         if ($token->name === '/_') {
             $catalogue->set(($this->prefix ? $this->prefix . '.' : '') . $buffer, $buffer);
             $buffer = NULL;
         } elseif ($token->name === '_' && empty($token->value)) {
             $buffer = '';
         } else {
             $args = new MacroTokens($token->value);
             $writer = new PhpWriter($args, $token->modifiers);
             $message = $writer->write('%node.word');
             if (in_array(substr(trim($message), 0, 1), array('"', '\''), TRUE)) {
                 $message = substr(trim($message), 1, -1);
             }
             $catalogue->set(($this->prefix ? $this->prefix . '.' : '') . $message, $message);
         }
     }
 }
All Usage Examples Of Latte\Parser::parse