Skip to content

Commit

Permalink
[latte] BlockMacros: added $snippetAttribute for custom HTML attribut…
Browse files Browse the repository at this point in the history
…e with snippet id [Closes nette/application#242]
  • Loading branch information
dg committed Jan 3, 2020
1 parent 14448d7 commit 1444925
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions latte/src/Latte/Macros/BlockMacros.php
Expand Up @@ -22,6 +22,9 @@
*/
class BlockMacros extends MacroSet
{
/** @var string */
public $snippetAttribute = 'id';

/** @var array */
private $namedBlocks = [];

Expand Down Expand Up @@ -240,12 +243,12 @@ public function macroBlock(MacroNode $node, PhpWriter $writer)
$enterCode = '$this->global->snippetDriver->enter(' . $writer->formatWord($name) . ', "' . SnippetDriver::TYPE_DYNAMIC . '");';

if ($node->prefix) {
$node->attrCode = $writer->write("<?php echo ' id=\"' . htmlSpecialChars(\$this->global->snippetDriver->getHtmlId({$writer->formatWord($name)})) . '\"' ?>");
$node->attrCode = $writer->write("<?php echo ' $this->snippetAttribute=\"' . htmlSpecialChars(\$this->global->snippetDriver->getHtmlId({$writer->formatWord($name)})) . '\"' ?>");
return $writer->write($enterCode);
}
$node->closingCode .= "\n</div>";
$this->checkExtraArgs($node);
return $writer->write("?>\n<div id=\"<?php echo htmlSpecialChars(\$this->global->snippetDriver->getHtmlId({$writer->formatWord($name)})) ?>\"><?php " . $enterCode);
return $writer->write("?>\n<div $this->snippetAttribute=\"<?php echo htmlSpecialChars(\$this->global->snippetDriver->getHtmlId({$writer->formatWord($name)})) ?>\"><?php " . $enterCode);

} else {
$node->data->leave = true;
Expand Down Expand Up @@ -275,8 +278,8 @@ public function macroBlock(MacroNode $node, PhpWriter $writer)

// static snippet/snippetArea
if ($node->name === 'snippet' || $node->name === 'snippetArea') {
if ($node->prefix && isset($node->htmlNode->attrs['id'])) {
throw new CompileException('Cannot combine HTML attribute id with n:snippet.');
if ($node->prefix && isset($node->htmlNode->attrs[$this->snippetAttribute])) {
throw new CompileException("Cannot combine HTML attribute $this->snippetAttribute with n:snippet.");
}
$node->data->name = $name = '_' . $name;
}
Expand Down Expand Up @@ -306,11 +309,11 @@ public function macroBlock(MacroNode $node, PhpWriter $writer)
if (isset($node->htmlNode->macroAttrs['foreach'])) {
trigger_error('Combination of n:snippet with n:foreach is invalid, use n:inner-foreach.', E_USER_WARNING);
}
$node->attrCode = $writer->write('<?php echo \' id="\' . htmlSpecialChars($this->global->snippetDriver->getHtmlId(%var)) . \'"\' ?>', (string) substr($name, 1));
$node->attrCode = $writer->write("<?php echo ' $this->snippetAttribute=\"' . htmlSpecialChars(\$this->global->snippetDriver->getHtmlId(%var)) . '\"' ?>", (string) substr($name, 1));
return $writer->write($include, $name);
}
$this->checkExtraArgs($node);
return $writer->write("?>\n<div id=\"<?php echo htmlSpecialChars(\$this->global->snippetDriver->getHtmlId(%var)) ?>\"><?php $include ?>\n</div><?php ",
return $writer->write("?>\n<div $this->snippetAttribute=\"<?php echo htmlSpecialChars(\$this->global->snippetDriver->getHtmlId(%var)) ?>\"><?php $include ?>\n</div><?php ",
(string) substr($name, 1), $name
);

Expand Down

0 comments on commit 1444925

Please sign in to comment.