Use the same extension when including template file in v2

This commit is contained in:
Kijin Sung 2023-10-15 02:55:53 +09:00
parent 4e8c184d0c
commit 02639e9aa0
2 changed files with 16 additions and 6 deletions

View file

@ -19,6 +19,7 @@ class Template
public $absolute_dirname;
public $relative_dirname;
public $filename;
public $extension;
public $exists;
public $absolute_path;
public $relative_path;
@ -105,20 +106,24 @@ class Template
if ($extension !== 'auto')
{
$filename .= '.' . $extension;
$this->extension = $extension;
}
elseif (Storage::exists($this->absolute_dirname . $filename . '.html'))
{
$filename .= '.html';
$this->extension = 'html';
$this->exists = true;
}
elseif (Storage::exists($this->absolute_dirname . $filename . '.blade.php'))
{
$filename .= '.blade.php';
$this->extension = 'blade.php';
$this->exists = true;
}
else
{
$filename .= '.html';
$this->extension = 'html';
}
}
@ -126,11 +131,15 @@ class Template
$this->filename = $filename;
$this->absolute_path = $this->absolute_dirname . $filename;
$this->relative_path = $this->relative_dirname . $filename;
if ($this->extension === null)
{
$this->extension = preg_match('/\.(?:blade\.php|[a-z]+)$/i', $filename, $m) ? $m[1] : '';
}
if ($this->exists === null)
{
$this->exists = Storage::exists($this->absolute_path);
}
if ($this->exists && preg_match('/\.blade\.php$/', $filename))
if ($this->exists && $this->extension === 'blade.php')
{
$this->config->version = 2;
$this->config->autoescape = true;

View file

@ -302,7 +302,7 @@ class TemplateParser_v2
*
* Blade-style syntax:
* @include('view')
* @include('view.html', $vars)
* @include('view.blade.php', $vars)
*
* @param string $content
* @return string
@ -315,7 +315,7 @@ class TemplateParser_v2
$attrs = self::_getTagAttributes($match[1]);
$path = $attrs['src'] ?? ($attrs['target'] ?? null);
if (!$path) return $match[0];
$tpl = '<?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "' . $path . '"); ';
$tpl = '<?php $__tpl = new \Rhymix\Framework\Template($this->relative_dirname, "' . $path . '", "' . ($this->template->extension ?: 'auto') . '"); ';
$tpl .= !empty($attrs['vars']) ? ' $__tpl->setVars(' . $attrs['vars'] . '); ' : '';
$tpl .= 'echo $__tpl->compile(); ?>';
if (!empty($attrs['if']) || !empty($attrs['when']))
@ -332,17 +332,18 @@ class TemplateParser_v2
// Convert Blade-style include directives.
$regexp = '#^[\x09\x20]*(?<!@)@(include(?:If|When|Unless)?)\x20?\((.+?)\)[\x09\x20]*$#sm';
$content = preg_replace_callback($regexp, function($match) {
$extension = $this->template->extension === 'blade.php' ? 'blade.php' : 'html';
if ($match[1] === 'include')
{
$tpl = '<?php (function($__path, $__vars = null) { ';
$tpl .= '$__tpl = new \Rhymix\Framework\Template($this->relative_dirname, $__path); ';
$tpl .= '$__tpl = new \Rhymix\Framework\Template($this->relative_dirname, $__path, "' . $extension . '"); ';
$tpl .= 'if ($__vars) $__tpl->setVars($__vars); ' ;
$tpl .= 'echo $__tpl->compile(); })(' . $match[2] . '); ?>';
}
elseif ($match[1] === 'includeIf')
{
$tpl = '<?php (function($__path, $__vars = null) { ';
$tpl .= '$__tpl = new \Rhymix\Framework\Template($this->relative_dirname, $__path); ';
$tpl .= '$__tpl = new \Rhymix\Framework\Template($this->relative_dirname, $__path, "' . $extension . '"); ';
$tpl .= 'if (!$__tpl->exists()) return; ';
$tpl .= 'if ($__vars) $__tpl->setVars($__vars); ' ;
$tpl .= 'echo $__tpl->compile(); })(' . $match[2] . '); ?>';
@ -352,7 +353,7 @@ class TemplateParser_v2
$tpl = '<?php (function($__type, $__cond, $__path, $__vars = null) { ';
$tpl .= 'if ($__type === "includeWhen" && !$__cond) return; ';
$tpl .= 'if ($__type === "includeUnless" && $__cond) return; ';
$tpl .= '$__tpl = new \Rhymix\Framework\Template($this->relative_dirname, $__path); ';
$tpl .= '$__tpl = new \Rhymix\Framework\Template($this->relative_dirname, $__path, "' . $extension . '"); ';
$tpl .= 'if ($__vars) $__tpl->setVars($__vars); ' ;
$tpl .= 'echo $__tpl->compile(); })("' . $match[1] . '", ' . $match[2] . '); ?>';
}