Add support for Blade-style @each include loop

This commit is contained in:
Kijin Sung 2023-10-17 02:26:06 +09:00
parent eba9a4d333
commit 668feea9e9
7 changed files with 56 additions and 4 deletions

View file

@ -397,6 +397,33 @@ class TemplateParser_v2
return self::_escapeVars($tpl);
}, $content);
// Handle the @each directive.
$parentheses = self::_getRegexpForParentheses(1);
$regexp = '#(?:^[\x09\x20]*|<!--)@each\x20?('. $parentheses . ')(?:\x20*-->|[\x09\x20]*$)#sm';
$content = preg_replace_callback($regexp, function($match) {
// Convert the path if necessary.
$args = self::_convertVariableScope(substr($match[1], 1, strlen($match[1]) - 2));
$extension = $this->template->extension === 'blade.php' ? 'blade.php' : 'html';
$dir = '$this->relative_dirname';
$path = preg_match('#^([\'"])([^\'"]+)\1#', $args, $m) ? $m[2] : '';
if (preg_match('#^\^/?(\w.+)$#s', $path, $mm))
{
$dir = '"' . escape_dqstr(str_contains($mm[1], '/') ? dirname($mm[1]) : '') . '"';
$filename = basename($mm[1]);
$args = preg_replace('#^([\'"])([^\'"]+)\1#', '$1' . $filename . '$1', $args);
}
// Generate the IIFE code.
$tpl = '<?php (function($__dir, $__path, $__vars, $__varname, $__empty = null) { ';
$tpl .= 'if (!$__vars): $__vars = []; if ($__empty): $__path = $__empty; $__vars[] = \'\'; endif; endif; ';
$tpl .= 'foreach ($__vars as $__var): ';
$tpl .= '$__tpl = new \Rhymix\Framework\Template($__dir, $__path, "' . $extension . '"); ';
$tpl .= '$__tpl->setVars([(string)$__varname => $__var]); ' ;
$tpl .= 'echo $__tpl->compile(); endforeach; })(' . $dir . ', ' . $args . '); ?>';
return $tpl;
}, $content);
return $content;
}
@ -749,7 +776,7 @@ class TemplateParser_v2
// Insert JSON and lang codes.
$parentheses = self::_getRegexpForParentheses(2);
$content = preg_replace_callback('#(?<!@)@(json|lang)('. $parentheses . ')#', function($match) {
$content = preg_replace_callback('#(?<!@)@(json|lang)\x20?('. $parentheses . ')#', function($match) {
$args = self::_convertVariableScope(substr($match[2], 1, strlen($match[2]) - 2));
if ($match[1] === 'json')
{

View file

@ -0,0 +1 @@
<div>{$var}</div>

View file

@ -0,0 +1 @@
<div>Empty</div>

View file

@ -39,6 +39,9 @@
<div>Nothing here...</div>
@end
@each('incl/eachtest', $bar, 'var')
@each('incl/eachtest', [], 'anything', 'incl/empty')
@desktop
<p>The full class name is {get_class(new Push)|escape}, {Push::class} really.</p>
@enddesktop

View file

@ -28,7 +28,7 @@
<?php endif; ?>
</div>
<?php $__tmp_19148045bac5d8 = Context::get('bar') ?? []; if($__tmp_19148045bac5d8): foreach ($__tmp_19148045bac5d8 as $__Context->k => $__Context->val): ?>
<?php $__tmp_042521f3da7d65 = Context::get('bar') ?? []; if($__tmp_042521f3da7d65): foreach ($__tmp_042521f3da7d65 as $__Context->k => $__Context->val): ?>
<div>
<?php if (empty($__Context->nosuchvar)): ?>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
@ -39,6 +39,9 @@
<div>Nothing here...</div>
<?php endif; ?>
<?php (function($__dir, $__path, $__vars, $__varname, $__empty = null) { if (!$__vars): $__vars = []; if ($__empty): $__path = $__empty; $__vars[] = ''; endif; endif; foreach ($__vars as $__var): $__tpl = new \Rhymix\Framework\Template($__dir, $__path, "html"); $__tpl->setVars([(string)$__varname => $__var]); echo $__tpl->compile(); endforeach; })($this->relative_dirname, 'incl/eachtest', $__Context->bar, 'var'); ?>
<?php (function($__dir, $__path, $__vars, $__varname, $__empty = null) { if (!$__vars): $__vars = []; if ($__empty): $__path = $__empty; $__vars[] = ''; endif; endif; foreach ($__vars as $__var): $__tpl = new \Rhymix\Framework\Template($__dir, $__path, "html"); $__tpl->setVars([(string)$__varname => $__var]); echo $__tpl->compile(); endforeach; })($this->relative_dirname, 'incl/eachtest', [], 'anything', 'incl/empty'); ?>
<?php if (!$__Context->m): ?>
<p>The full class name is <?php echo htmlspecialchars(get_class(new Rhymix\Framework\Push), \ENT_QUOTES, 'UTF-8', true); ?>, <?php echo htmlspecialchars(Rhymix\Framework\Push::class, \ENT_QUOTES, 'UTF-8', false); ?> really.</p>
<?php endif; ?>

View file

@ -40,6 +40,13 @@
<span class="BEST!"></span>
</div>
<div>Rhy</div>
<div>miX</div>
<div>is</div>
<div>da</div>
<div>BEST!</div>
<div>Empty</div>
<p>The full class name is Rhymix\Framework\Push, Rhymix\Framework\Push really.</p>
<div class="barContainer" data-bar="[&quot;Rhy&quot;,&quot;miX&quot;,&quot;is&quot;,&quot;da&quot;,&quot;BEST!&quot;]"></div>

View file

@ -151,6 +151,16 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
'echo $__tpl->compile(); })("includeUnless", "common/tpl", false, \'foobar.html\', $__Context->vars); ?>'
]);
$this->assertEquals($target, $this->_parse($source));
// Blade-style @each
$source = "@each('incl/eachtest', \$jobs, 'job')";
$target = 'foreach ($__vars as $__var):';
$this->assertStringContainsString($target, $this->_parse($source));
// Blade-style @each with fallback template
$source = "@each('incl/eachtest', \$jobs, 'job', 'incl/empty')";
$target = 'if ($__empty): $__path = $__empty;';
$this->assertStringContainsString($target, $this->_parse($source));
}
public function testAssetLoading()
@ -981,7 +991,7 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
$compiled_output = $tmpl->compileDirect('./tests/_data/template', 'v2example.html');
$tmpvar = preg_match('/(\$__tmp_[0-9a-f]{14})/', $compiled_output, $m) ? $m[1] : '';
//file_put_contents(\RX_BASEDIR . 'tests/_data/template/v2result1.php', $compiled_output);
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2result1.php', $compiled_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2result1.php');
$expected = preg_replace('/(\$__tmp_[0-9a-f]{14})/', $tmpvar, $expected);
@ -990,7 +1000,7 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
$executed_output = $tmpl->compile();
$executed_output = preg_replace('/<!--#Template(Start|End):.+?-->\n/', '', $executed_output);
$tmpvar = preg_match('/(\$__tmp_[0-9a-f]{14})/', $executed_output, $m) ? $m[1] : '';
//file_put_contents(\RX_BASEDIR . 'tests/_data/template/v2result2.php', $executed_output);
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2result2.php', $executed_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2result2.php');
$expected = preg_replace('/(\$__tmp_[0-9a-f]{14})/', $tmpvar, $expected);