Allow loops and conditions to appear anywhere in a line

This commit is contained in:
Kijin Sung 2023-10-17 03:28:20 +09:00
parent 66980edab1
commit 3c15589fb8
3 changed files with 23 additions and 15 deletions

View file

@ -616,13 +616,12 @@ class TemplateParser_v2
* Convert loop and condition directives.
*
* Loops and conditions can be written inside HTML comments (XE-style)
* or without comments (Blade-style). If using the Blade-style syntax,
* each directive must appear in its own line.
* or without comments (Blade-style).
*
* It is highly recommended that ending directives match the starting
* directive, e.g. @if ... @endif. However, for compatibility with legacy
* templates, Rhymix will automatically find out which loop you are
* trying to end if you simply write @end. Either way, your loops must
* trying to close if you simply write @end. Either way, your loops must
* balance out or you will see 'unexpected end of file' errors.
*
* XE-style syntax:
@ -638,13 +637,26 @@ class TemplateParser_v2
*/
protected function _convertLoopDirectives(string $content): string
{
// Convert block directives.
$regexp = '#(?:^[\x09\x20]*|<!--)@((?:end)?(?:if|unless|for|foreach|forelse|while|switch|once|isset|unset|empty|admin|auth|member|guest|desktop|mobile)|else|elseif|case|default|continue|break|end)\x20?(?:\((.+?)\))?(?:\x20*-->|[\x09\x20]*$)#sm';
// Generate the list of directives to match.
foreach (self::$_loopdef as $directive => $def)
{
$directives[] = $directive;
if (count($def) > 1)
{
$directives[] = 'end' . $directive;
}
}
usort($directives, function($a, $b) { return strlen($b) - strlen($a); });
$directives = implode('|', $directives) . '|end';
// Convert both XE-style and Blade-style directives.
$parentheses = self::_getRegexpForParentheses(2);
$regexp = '#(?:<!--)?(?<!@)@(' . $directives . ')\x20?(' . $parentheses . ')?(?:[\x20\x09]*-->)?#';
$content = preg_replace_callback($regexp, function($match) {
// Collect the necessary information.
$directive = $match[1];
$args = isset($match[2]) ? self::_convertVariableScope($match[2]) : '';
$args = isset($match[2]) ? self::_convertVariableScope(substr($match[2], 1, strlen($match[2]) - 2)) : '';
$stack = null;
$code = null;

View file

@ -23,7 +23,7 @@
<div class="foobar"|unless="isset($baz)">
@if ($foo || $bar)
<p>Hello {$foo|noescape}</p>
<p>Hello @if ($bar){$foo|noescape}@endif</p>
<p>{{ implode('|', array_map(function(\$i) { return strtoupper(\$i); }, $bar)) }}</p>
@end
</div>
@ -35,9 +35,7 @@
<span <!--@if($k >= 2)-->class="{$val}"<!--@end-->></span>
@endempty
</div>
@empty
<div>Nothing here...</div>
@end
@empty<div>Nothing here...</div>@end
@each('incl/eachtest', $bar, 'var')
@each('incl/eachtest', [], 'anything', 'incl/empty')

View file

@ -23,21 +23,19 @@
<div<?php if (!(isset($__Context->baz))): ?> class="foobar"<?php endif; ?>>
<?php if ($__Context->foo || $__Context->bar): ?>
<p>Hello <?php echo $__Context->foo ?? ''; ?></p>
<p>Hello <?php if ($__Context->bar): ?><?php echo $__Context->foo ?? ''; ?><?php endif; ?></p>
<p><?php echo htmlspecialchars(implode('|', array_map(function($i) { return strtoupper($i); }, $__Context->bar)), \ENT_QUOTES, 'UTF-8', false); ?></p>
<?php endif; ?>
</div>
<?php $__tmp_042521f3da7d65 = Context::get('bar') ?? []; if($__tmp_042521f3da7d65): foreach ($__tmp_042521f3da7d65 as $__Context->k => $__Context->val): ?>
<?php $__tmp_8eaec5a37bc467 = Context::get('bar') ?? []; if($__tmp_8eaec5a37bc467): foreach ($__tmp_8eaec5a37bc467 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" />
<span <?php if ($__Context->k >= 2): ?>class="<?php echo htmlspecialchars($__Context->val ?? '', \ENT_QUOTES, 'UTF-8', false); ?>"<?php endif; ?>></span>
<?php endif; ?>
</div>
<?php endforeach; else: ?>
<div>Nothing here...</div>
<?php endif; ?>
<?php endforeach; else: ?><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'); ?>