Latte 2 → 3: vlastni makro vystup v ruznych contextem
- hrach
- Člen | 1838
Latte 2 zjednoduseno (snad ne moc)
public function macroResource(MacroNode $node, PhpWriter $writer)
{
$url = $node->tokenizer->fetchWord();
return $writer->write('echo %escape(%var)', $url);
}
Pouziti:
<script src={resource init.min.js}></script>
<script>
var chat = {resource chat.min.js};
</script>
Vystup:
<script src="init.min.js"></script>
<script>
var chat = "chat.min.js";
</script>
V Latte3 se snazim takto:
class ResourceNode extends StatementNode
{
public ExpressionNode $url;
public static function create(Tag $tag): ResourceNode
{
$tag->expectArguments();
$node = $tag->node = new ResourceNode();
$node->url = $tag->parser->parseUnquotedStringOrExpression();
return $node;
}
public function print(PrintContext $context): string
{
return $context->format("echo %escape(%node)", $this->url);
}
public function &getIterator(): \Generator
{
yield $this->url;
}
}
Vystup:
<script src="init.min.js"></script>
<script>
var chat = chat.min.js;
</script>
Pripadne me pokusy konci s tim, ze attribut ma apostrofy navic.