ajur-media / template
基于Smarty的AJUR Media项目模板类
1.2.0
2024-02-05 17:36 UTC
Requires
- php: ^7.4 || 8.*
- ext-json: *
- psr/log: ^1.1
- smarty/smarty: ^4.3
Requires (Dev)
- rector/rector: ^0.19.8
This package is not auto-updated.
Last update: 2024-09-16 20:59:52 UTC
README
https://github.com/ajur-media/template https://packagist.org.cn/packages/ajur-media/template
如何使用?
require_once __DIR__ . '/vendor/autoload.php'; $SMARTY = new Smarty(); $SMARTY->setTemplateDir( __DIR__ ); $SMARTY->setCompileDir( __DIR__ . '/cache/'); $SMARTY->setForceCompile(true); // Global + inner templates $global = new \AJUR\Template($SMARTY, $_REQUEST); $global->setTemplate('1.tpl'); $inner = new \AJUR\Template($SMARTY); $inner->setTemplate("2.tpl"); $inner->assign('value_1', 'FOO'); $inner->assign('value_2', 'BAR'); $global->assign("content", $inner->render()); echo $global->render(); // Global + include secondary template: $global = new \AJUR\Template($SMARTY); $global->setTemplate('0.tpl'); $global->assign("file", "2.tpl"); $global->assign('value_1', 'FOO'); $global->assign('value_2', 'BAR'); echo $global->render(); // JSON $global = new \AJUR\Template($SMARTY); $global->assign("file", "2.tpl"); $global->assign('value_1', 'FOO'); $global->assign('value_2', 'BAR'); // or use helper $global->assignJSON([ 'file' => '2.tpl', 'value_1' => 'FOO', 'value_2' => 'BAR', ]); $global->setRenderType(\AJUR\Template::CONTENT_TYPE_JSON); echo $global->render();
模板插件
为了正确运行插件,需要在php.ini中设置参数 opcache.save_comments = 1
将参数传递给(注册的)插件
修改器
有两种方式,具体取决于函数的注册方式
A) 通过列举参数(省略的参数将使用默认值)
{$size|size_format:decimals:separator:separator}
此时函数应该这样定义
public static function size_format(int $size, int $decimals = 0, string $decimal_separator = '.', string $thousands_separator = ','):string;
B) 通过数组参数
{$size|size_format:[3,',','-']}
此时函数应该这样定义
public static function sf(int $size, array $params):string { $decimals = $params['decimals'] ?? 3; $decimal_separator = $params['decimal_separator'] ?? '.'; $thousands_separator = $params['thousands_separator'] ?? ','; // ... }
函数
使用方法
{sum a=11 b=14}
方法必须这样定义
function sum($params) { return ($params['a'] ?? 0) + ($params['b'] ?? 0); }
@todo