Add support for fragment in template v2

This commit is contained in:
Kijin Sung 2023-10-17 22:25:53 +09:00
parent a6afa3a61d
commit 82b9107ca0
5 changed files with 71 additions and 2 deletions

View file

@ -59,6 +59,10 @@ class TemplateParser_v2
"if (!isset(\$GLOBALS['tplv2_once']['%uniq'])):",
"\$GLOBALS['tplv2_once']['%uniq'] = true; endif;",
],
'fragment' => [
'ob_start(); $__last_fragment_name = %s;',
"\$this->_fragments[\$__last_fragment_name] = ob_get_flush();",
],
'isset' => ['if (isset(%s)):', 'endif;'],
'unset' => ['if (!isset(%s)):', 'endif;'],
'empty' => ['if (empty(%s)):', 'endif;'],
@ -98,6 +102,7 @@ class TemplateParser_v2
$content = $this->_convertRelativePaths($content);
$content = $this->_convertPHPSections($content);
$content = $this->_convertVerbatimSections($content);
$content = $this->_convertFragments($content);
$content = $this->_convertClassAliases($content);
$content = $this->_convertIncludes($content);
$content = $this->_convertAssets($content);
@ -239,6 +244,33 @@ class TemplateParser_v2
return $content;
}
/**
* Convert fragments.
*
* Sections delimited by <fragment name="name"> ... </fragment> (XE-style)
* or @fragment('name') ... @endfragment (Blade-style) are stored
* separately when executed, and can be accessed through getFragment()
* afterwards. They are, of course, also included in the primary output.
*
* @param string $content
* @return string
*/
protected function _convertFragments(string $content): string
{
// Convert XE-style fragment code. Blade-style is handled elsewhere.
$regexp = '#<fragment\s+name="([^"]+)"\s*/?>(.*?)</fragment>#s';
$content = preg_replace_callback($regexp, function($match) {
$name = trim($match[1]);
$content = $match[2];
$tpl = '<?php ob_start(); \$__last_fragment_name = ' . var_export($name, true) . '; ?>';
$tpl .= $content;
$tpl .= '<?php $this->_fragments[\$__last_fragment_name] = ob_get_flush(); ?>';
return $tpl;
}, $content);
return $content;
}
/**
* Convert class aliases.
*

View file

@ -28,14 +28,16 @@
<?php endif; ?>
</div>
<?php $__tmp_RANDOM_LOOP_ID = Context::get('bar') ?? []; if($__tmp_RANDOM_LOOP_ID): $__loop_RANDOM_LOOP_ID = $this->_v2_initLoopVar("RANDOM_LOOP_ID", $__tmp_RANDOM_LOOP_ID); foreach ($__tmp_RANDOM_LOOP_ID as $__Context->k => $__Context->val): ?>
<?php ob_start(); $__last_fragment_name = 'rhymix'; ?>
<?php $__tmp_64b3371f38fea1 = Context::get('bar') ?? []; if($__tmp_64b3371f38fea1): $__loop_64b3371f38fea1 = $this->_v2_initLoopVar("64b3371f38fea1", $__tmp_64b3371f38fea1); foreach ($__tmp_64b3371f38fea1 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 $this->_v2_incrLoopVar($__loop_RANDOM_LOOP_ID); endforeach; $this->_v2_removeLoopVar($__loop_RANDOM_LOOP_ID); unset($__loop_RANDOM_LOOP_ID); else: ?><div>Nothing here...</div><?php endif; ?>
<?php $this->_v2_incrLoopVar($__loop_64b3371f38fea1); endforeach; $this->_v2_removeLoopVar($__loop_64b3371f38fea1); unset($__loop_64b3371f38fea1); else: ?><div>Nothing here...</div><?php endif; ?>
<?php $this->_fragments[$__last_fragment_name] = ob_get_flush(); ?>
<?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'); ?>

View file

@ -0,0 +1,21 @@
<div>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span ></span>
</div>
<div>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span ></span>
</div>
<div>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span class="is"></span>
</div>
<div>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span class="da"></span>
</div>
<div>
<img src="/rhymix/tests/_data/template/bar/rhymix.svg" alt="unit tests are cool" />
<span class="BEST!"></span>
</div>

View file

@ -28,6 +28,7 @@
@end
</div>
@fragment('rhymix')
@forelse (Context::get('bar') as $k => $val)
<div>
@empty ($nosuchvar)
@ -36,6 +37,7 @@
@endempty
</div>
@empty<div>Nothing here...</div>@end
@endfragment
@each('incl/eachtest', $bar, 'var')
@each('incl/eachtest', [], 'anything', 'incl/empty')

View file

@ -1011,6 +1011,7 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
$tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'v2example.html');
$tmpl->disableCache();
// Get compiled code
$compiled_output = $tmpl->compileDirect('./tests/_data/template', 'v2example.html');
$tmpvar = preg_match('/\$__tmp_([0-9a-f]{14})/', $compiled_output, $m) ? $m[1] : '';
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2example.compiled.html', $compiled_output);
@ -1021,6 +1022,7 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
$this->_normalizeWhitespace($compiled_output)
);
// Get final output
$executed_output = $tmpl->compile();
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2example.executed.html', $executed_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2example.executed.html');
@ -1030,6 +1032,16 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
$this->_normalizeWhitespace($executed_output)
);
// Get fragment from output
$fragment_output = $tmpl->getFragment('rhymix');
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2example.fragment.html', $fragment_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2example.fragment.html');
$expected = preg_replace('/RANDOM_LOOP_ID/', $tmpvar, $expected);
$this->assertEquals(
$this->_normalizeWhitespace($expected),
$this->_normalizeWhitespace($fragment_output)
);
// Loop variable
$tmpl = new \Rhymix\Framework\Template('./tests/_data/template', 'v2loops.html');
$tmpl->disableCache();