Latte\Compiler::expandMacro PHP Method

expandMacro() public method

Expands macro and returns node & code.
public expandMacro ( $name, $args, $modifiers = NULL, $nPrefix = NULL ) : MacroNode
return MacroNode
    public function expandMacro($name, $args, $modifiers = NULL, $nPrefix = NULL)
    {
        $inScript = in_array($this->context, [self::CONTEXT_HTML_JS, self::CONTEXT_HTML_CSS], TRUE);
        if (empty($this->macros[$name])) {
            $hint = ($t = Helpers::getSuggestion(array_keys($this->macros), $name)) ? ", did you mean {{$t}}?" : '';
            throw new CompileException("Unknown macro {{$name}}{$hint}" . ($inScript ? ' (in JavaScript or CSS, try to put a space after bracket or use n:syntax=off)' : ''));
        }
        if (preg_match('#\\|(no)?safeurl(?!\\w)#i', $modifiers, $m)) {
            $hint = $m[1] ? '|nocheck' : '|checkurl';
            $modifiers = str_replace($m[0], $hint, $modifiers);
            trigger_error("Modifier {$m['0']} is deprecated, please replace it with {$hint}.", E_USER_DEPRECATED);
        }
        if (strpbrk($name, '=~%^&_')) {
            if (in_array($this->context, [self::CONTEXT_HTML_ATTRIBUTE_URL, self::CONTEXT_HTML_ATTRIBUTE_UNQUOTED_URL], TRUE)) {
                if (!Helpers::removeFilter($modifiers, 'nosafeurl|nocheck') && !preg_match('#\\|datastream(?=\\s|\\||\\z)#i', $modifiers)) {
                    $modifiers .= '|checkurl';
                }
            }
            if (!Helpers::removeFilter($modifiers, 'noescape')) {
                $modifiers .= '|escape';
                if ($inScript && $name === '=' && preg_match('#["\'] *\\z#', $this->tokens[$this->position - 1]->text)) {
                    throw new CompileException("Do not place {$this->tokens[$this->position]->text} inside quotes.");
                }
            }
        }
        if ($nPrefix === MacroNode::PREFIX_INNER && !strcasecmp($this->htmlNode->name, 'script')) {
            $context = [$this->contentType, self::CONTEXT_HTML_JS];
        } elseif ($nPrefix === MacroNode::PREFIX_INNER && !strcasecmp($this->htmlNode->name, 'style')) {
            $context = [$this->contentType, self::CONTEXT_HTML_CSS];
        } elseif ($nPrefix) {
            $context = [$this->contentType, self::CONTEXT_HTML_TEXT];
        } else {
            $context = [$this->contentType, $this->context];
        }
        foreach (array_reverse($this->macros[$name]) as $macro) {
            $node = new MacroNode($macro, $name, $args, $modifiers, $this->macroNode, $this->htmlNode, $nPrefix);
            $node->context = $context;
            $node->startLine = $nPrefix ? $this->htmlNode->startLine : $this->getLine();
            if ($macro->nodeOpened($node) !== FALSE) {
                return $node;
            }
        }
        throw new CompileException('Unknown ' . ($nPrefix ? 'attribute ' . Parser::N_PREFIX . ($nPrefix === MacroNode::PREFIX_NONE ? '' : "{$nPrefix}-") . $name : 'macro {' . $name . ($args ? " {$args}" : '') . '}'));
    }