Convert class and style builder into a method of Template class

This commit is contained in:
Kijin Sung 2023-10-17 22:44:27 +09:00
parent 82b9107ca0
commit 55cafc5c33
6 changed files with 61 additions and 24 deletions

View file

@ -47,8 +47,17 @@
<?php endif; ?>
<div class="barContainer" data-bar="<?php echo $this->config->context === 'JS' ? (json_encode($__Context->bar ?? '', \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG | \JSON_HEX_QUOT)) : htmlspecialchars(json_encode($__Context->bar ?? '', \JSON_UNESCAPED_UNICODE | \JSON_UNESCAPED_SLASHES | \JSON_HEX_TAG | \JSON_HEX_QUOT), \ENT_QUOTES, 'UTF-8', false); ?>">
<span<?php (function(array $__defs) { $__values = []; foreach ($__defs as $__key => $__val): if (is_numeric($__key)): $__values[] = $__val; elseif ($__val): $__values[] = $__key; endif; endforeach; if ($__values): echo ' class="'; echo htmlspecialchars(implode(' ', $__values), \ENT_QUOTES, 'UTF-8', false); echo '"'; endif; })((['a-1', 'font-normal' => $__Context->foo, 'text-blue' => false, 'bg-white' => true])); ?>></span>
<span<?php (function(array $__defs) { $__values = []; foreach ($__defs as $__key => $__val): if (is_numeric($__key)): $__values[] = $__val; elseif ($__val): $__values[] = $__key; endif; endforeach; if ($__values): echo ' style="'; echo htmlspecialchars(implode('; ', $__values), \ENT_QUOTES, 'UTF-8', false); echo '"'; endif; })((['border-radius: 0.25rem', 'margin: 1rem' => Context::get('bar'), 'padding: 2rem' => false])); ?>></span>
<span<?php echo $this->_v2_buildAttribute('class', [
'a-1',
'font-normal' => $__Context->foo,
'text-blue' => false,
'bg-gray-200' => true
]); ?>></span>
<span<?php echo $this->_v2_buildAttribute('style', [
'border-radius: 0.25rem',
'margin: 1rem' => Context::get('bar'),
'padding: 2rem' => false,
]); ?>></span>
</div>
<script type="text/javascript"<?php $this->config->context = "JS"; ?>>

View file

@ -52,7 +52,7 @@
<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;]">
<span class="a-1 font-normal bg-white"></span>
<span class="a-1 font-normal bg-gray-200"></span>
<span style="border-radius: 0.25rem; margin: 1rem"></span>
</div>

View file

@ -47,8 +47,17 @@
@enddesktop
<div class="barContainer" data-bar="{$bar|json}">
<span @class(['a-1', 'font-normal' => $foo, 'text-blue' => false, 'bg-white' => true])></span>
<span @style(['border-radius: 0.25rem', 'margin: 1rem' => Context::get('bar'), 'padding: 2rem' => false])></span>
<span @class([
'a-1',
'font-normal' => $foo,
'text-blue' => false,
'bg-gray-200' => true
])></span>
<span @style([
'border-radius: 0.25rem',
'margin: 1rem' => Context::get('bar'),
'padding: 2rem' => false,
])></span>
</div>
<script type="text/javascript">

View file

@ -868,13 +868,13 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
// @class
$source = "<span @class(['a-1', 'font-normal' => \$foo, 'text-blue' => false, 'bg-white' => true])></span>";
$this->assertStringContainsString("implode(' ', \$__values)", $this->_parse($source));
$this->assertStringContainsString("\$this->_v2_buildAttribute(", $this->_parse($source));
$this->assertStringContainsString("\$__Context->foo", $this->_parse($source));
// @style
$source = "<span @style(['border-radius: 0.25rem', 'margin: 1rem' => Context::get('bar')])></span>";
$this->assertStringContainsString("implode('; ', \$__values)", $this->_parse($source));
$this->assertStringContainsString("if (is_numeric(\$__key)):", $this->_parse($source));
$this->assertStringContainsString("\$this->_v2_buildAttribute(", $this->_parse($source));
$this->assertStringContainsString("Context::get('bar')]);", $this->_parse($source));
}
public function testMiscDirectives()
@ -1014,7 +1014,7 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
// 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);
Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2example.compiled.html', $compiled_output);
$expected = file_get_contents(\RX_BASEDIR . 'tests/_data/template/v2example.compiled.html');
$expected = preg_replace('/RANDOM_LOOP_ID/', $tmpvar, $expected);
$this->assertEquals(
@ -1024,7 +1024,7 @@ class TemplateParserV2Test extends \Codeception\Test\Unit
// Get final output
$executed_output = $tmpl->compile();
//Rhymix\Framework\Storage::write(\RX_BASEDIR . 'tests/_data/template/v2example.executed.html', $executed_output);
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');
$expected = preg_replace('/RANDOM_LOOP_ID/', $tmpvar, $expected);
$this->assertEquals(