Skip to content

Commit

Permalink
Added tag interpolation (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torben Köhn committed May 20, 2016
1 parent 6097a6f commit 3ca7dd7
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
11 changes: 5 additions & 6 deletions Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -1889,10 +1889,9 @@ protected function compileElement(Node $node)

$phtml = '';

if (!$node->tag)
$node->tag = $this->options['default_tag'];
$tag = $this->interpolate($node->tag ?: $this->options['default_tag']);

$phtml .= "<{$node->tag}";
$phtml .= "<{$tag}";

$htmlMode = $this->isHtml();
$xhtmlMode = $this->isXhtml();
Expand Down Expand Up @@ -2070,14 +2069,14 @@ protected function compileElement(Node $node)
}

$hasChildren = count($node->children) > 0;
$isSelfClosing = in_array($node->tag, $this->options['self_closing_tags']);
$isSelfClosing = in_array($tag, $this->options['self_closing_tags']);

if (!$hasChildren && (!$htmlMode || !$isSelfClosing)) {

if ($anyHtmlMode && !$isSelfClosing) {

//Force closed tag in HTML
$phtml .= "></{$node->tag}>";
$phtml .= "></{$tag}>";

return $phtml;
}
Expand All @@ -2093,7 +2092,7 @@ protected function compileElement(Node $node)
return $phtml;

$phtml .= $this->compileChildren($node->children);
$phtml .= $this->newLine().$this->indent()."</{$node->tag}>";
$phtml .= $this->newLine().$this->indent()."</{$tag}>";

return $phtml;
}
Expand Down
2 changes: 1 addition & 1 deletion Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -1511,7 +1511,7 @@ protected function scanDoctype()
protected function scanTag()
{

foreach ($this->scanToken('tag', '(?<name>[a-zA-Z_][a-zA-Z0-9\-_]*)', 'i') as $token) {
foreach ($this->scanToken('tag', '(?<name>(([a-zA-Z_][a-zA-Z0-9\-_]*)?[\?!#]\{[^\}]+\}([a-zA-Z_][a-zA-Z0-9\-_]*)?|[a-zA-Z_][a-zA-Z0-9\-_]*))', 'i') as $token) {

yield $token;

Expand Down
8 changes: 8 additions & 0 deletions Test/InterpolationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,12 @@ public function testIeConditionals()

$this->assertEquals('<!--[if IE8]><script src="ie8.js"></script><![endif]-->', $this->renderer->compile("<!--[if IE8]>\nscript(src='ie8.js')\n<![endif]-->"));
}

public function testTagInterpolation()
{

$this->assertEquals('<?php $tagName = \'abcdefghi\'?><<?=htmlentities(isset($tagName) ? $tagName : \'\', \ENT_QUOTES, \'UTF-8\')?> class="some-class">Some Content</<?=htmlentities(isset($tagName) ? $tagName : \'\', \ENT_QUOTES, \'UTF-8\')?>>', $this->renderer->compile("\$tagName='abcdefghi'\n#{\$tagName}.some-class Some Content"));
$this->assertEquals('<?php $tagName = \'def\'?><abc<?=htmlentities(isset($tagName) ? $tagName : \'\', \ENT_QUOTES, \'UTF-8\')?>ghi class="some-class">Some Content</abc<?=htmlentities(isset($tagName) ? $tagName : \'\', \ENT_QUOTES, \'UTF-8\')?>ghi>', $this->renderer->compile("\$tagName='def'\nabc#{\$tagName}ghi.some-class Some Content"));

}
}

0 comments on commit 3ca7dd7

Please sign in to comment.