Fix template v2 not working in external pages

https://xetown.com/questions/1792598
This commit is contained in:
Kijin Sung 2023-10-31 21:42:27 +09:00
parent d79c2f5140
commit 3233619eac
2 changed files with 13 additions and 40 deletions

View file

@ -187,17 +187,18 @@ class Template
$this->source_type = preg_match('!^((?:m\.)?[a-z]+)/!', $this->relative_dirname, $match) ? $match[1] : null;
$this->path = $this->absolute_dirname;
$this->web_path = \RX_BASEURL . $this->relative_dirname;
$this->_setCachePath();
$this->setCachePath();
}
/**
* Set the path for the cache file.
*
* @param ?string $cache_path
* @return void
*/
protected function _setCachePath()
public function setCachePath(?string $cache_path = null)
{
$this->cache_path = \RX_BASEDIR . 'files/cache/template/' . $this->relative_path . '.compiled.php';
$this->cache_path = $cache_path ?? (\RX_BASEDIR . 'files/cache/template/' . $this->relative_path . '.compiled.php');
if ($this->exists)
{
Debug::addFilenameAlias($this->absolute_path, $this->cache_path);
@ -325,7 +326,7 @@ class Template
$this->absolute_path = \RX_BASEDIR . $override_filename;
$this->relative_path = $override_filename;
$this->exists = Storage::exists($this->absolute_path);
$this->_setCachePath();
$this->setCachePath();
}
// Return error if the source file does not exist.

View file

@ -250,50 +250,22 @@ class PageView extends Page
// Parse as template if enabled.
if ($this->proc_tpl)
{
// Store compiled template in a temporary file.
$oTemplate = Rhymix\Framework\Template::getInstance();
$real_target_dir = dirname($real_target_file);
$tmp_cache_file = preg_replace('/\.cache\.php$/', '.compiled.php', $cache_file);
$content = $oTemplate->compileDirect($real_target_dir . '/', basename($real_target_file));
$success = Rhymix\Framework\Storage::write($tmp_cache_file, $content);
if (!$success)
{
return '';
}
// Include template file in an isolated scope.
$content = '';
// Add the target directory to include_path, so that raw PHP include statements inside the template will work.
$include_path = get_include_path();
$ob_level = ob_get_level();
ob_start();
set_include_path($real_target_dir . PATH_SEPARATOR . $include_path);
call_user_func(function() use($real_target_dir, $tmp_cache_file) {
$__Context = Context::getAll();
$__Context->tpl_path = $real_target_dir;
global $lang;
include $tmp_cache_file;
});
set_include_path($include_path);
while (ob_get_level() > $ob_level)
{
$content .= ob_get_clean();
}
$target_dir = dirname($real_target_file);
set_include_path($target_dir . PATH_SEPARATOR . $include_path);
// Insert comments for debugging.
if(Rhymix\Framework\Debug::isEnabledForCurrentUser() && Context::getResponseMethod() === 'HTML' && !starts_with('<!DOCTYPE', $content) && !starts_with('<?xml', $content))
{
$sign = PHP_EOL . '<!-- Template %s : ' . $target_file . ' -->' . PHP_EOL;
$content = sprintf($sign, 'start') . $content . sprintf($sign, 'end');
}
// Compile the template.
$oTemplate = new Rhymix\Framework\Template($target_dir . '/', basename($real_target_file));
$oTemplate->setCachePath(preg_replace('/\.cache\.php$/', '.compiled.php', $cache_file));
$content = $oTemplate->compile();
}
// Parse as PHP if enabled.
elseif ($this->proc_php)
{
ob_start();
call_user_func(function() use($real_target_file) {
include $real_target_file;
});
include_in_clean_scope($real_target_file);
$content = ob_get_clean();
}