Files
swiftadmin/support/Template.php

65 lines
2.2 KiB
PHP
Raw Normal View History

2022-11-28 19:11:12 +08:00
<?php
namespace support;
use Psr\SimpleCache\InvalidArgumentException;
use support\view\ThinkPHP;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
/**
* 模板类
* Class Template
* @package support
* @method static string fetch(string $template = '', array $vars = [], array $config = [])
*/
class Template extends ThinkPHP
{
/**
* @param string $template
* @param array $vars
* @param string|null $app
2023-04-25 20:11:49 +08:00
* @param string|null $plugin
* @return string
2022-11-28 19:11:12 +08:00
* @throws DataNotFoundException
* @throws DbException
* @throws InvalidArgumentException
* @throws ModelNotFoundException
*/
2023-04-25 20:11:49 +08:00
public static function render(string $template, array $vars, string $app = null, string $plugin = null): string
2022-11-28 19:11:12 +08:00
{
2023-06-19 14:32:30 +08:00
$request = request();
$app = $app === null ? $request->app : $app;
$viewPath = app_path() . "/$app/view/";
$defaultOptions = [
'view_path' => $viewPath,
'cache_path' => runtime_path() . '/views/',
'view_suffix' => 'html'
];
$options = $defaultOptions + config("view.options", []);
$options['taglib_pre_load'] = rtrim($options['taglib_pre_load'], ',');
foreach (get_plugin_list() as $index => $item) {
if (empty($item['status'])) {
continue;
}
$name = $item['name'];
$taglibPath = plugin_path($name . DIRECTORY_SEPARATOR . 'taglib');
$tagList = glob($taglibPath . '*.php');
foreach ($tagList as $key => $tag) {
$tag = pathinfo($tag, PATHINFO_FILENAME);
$options['taglib_pre_load'] .= ',plugin\\' . $name . '\\taglib\\' . $tag;
}
}
$views = new \think\Template($options);
ob_start();
$vars = array_merge(static::$vars, $vars);
$views->fetch($template, $vars);
$content = ob_get_clean();
static::$vars = [];
if (saenv('minify_page') && strtolower($app) == 'index') {
2022-11-28 19:11:12 +08:00
$content = preg_replace('/\s+/i', ' ', $content);
}
return $content;
}
}