corb / template-manager
laravel 的模板管理器
Requires
- php: >=5.5.9
This package is not auto-updated.
Last update: 2024-09-19 00:07:39 UTC
README
laravel 5 的数据库模板编译器
安装
-
将 corb/template-manager 作为 laravel 项目的依赖项安装
composer require corb/template-manager
-
在 providers 数组中添加服务提供者(config/app.php)
Corb\TemplateManager\TemplateManagerServiceProvider::class,
-
发布供应商文件
$ php artisan vendor:publish --tag=config
这将创建一个名为 template-manager.php 的配置文件
配置文件
-
模型
模型属性是一个包含 Model Namespaces 以访问表列的数组。
`'models' => [ App\User::class ]; ` or `models => [ 'App\User' ]; `
为什么?如果您想实现所见即所得编辑,这个选项可能很有用。
-
use_routes
如果您想使用包路由来获取可用的表字段,请设置为 true(默认)。
'use_routes' => true,
-
路由 URL,默认 'templates'
'route' => 'templates',
示例:
localhost:8000/templates'
// 此 URL 返回所有配置的表及其列 -
template_table
要使用的数据库表名,默认 'templates'。
'template_table' => 'templates',
警告:此配置用于创建数据库迁移。如果您有旧迁移,它将无济于事,所以您可能想删除迁移(需要改进)。
模板表
-
创建迁移
$ php artisan vendor:publish --tag=migrations
此命令将使用配置文件创建新的数据库迁移。记住之前的警告!
-
运行迁移
$ php artisan migrate
就这样,现在您可以存储模板了。
用法
编译模板
将 TemplateManager 类添加到您的代码中
use Corb\TemplateManager\TemplateManager;
创建一个新的 TemplateManager 对象
$template = new TemplateManager;
编译一个新的模板
$compiled_template = $template->parse('test_template', $template_data)
##示例
数据库模板:test_template
Hi <b>{{$user->name}}</b>.<br/>
Thanks to use corb template manager, if you need help
<a href="{{$help_link}}">Click here</a>.
测试代码:routes.php
use Corb\TemplateManager\TemplateManager;
Route::get('/', function () {
$template = new TemplateManager;
$user = new stdClass;
$user->name = 'Foo';
$help_link = 'https://github.com/gabomarin/l5-template-manager';
$data = [
'user' => $user,
'help_link' => $date
];
echo $template->parse('test_template', $data);
});
结果
Hi Foo.
Thanks to use corb template manager, if you need help Click here.
TemplateManager 方法
getModels()
-
获取配置文件中找到的模型
-
返回数组
[ "App\User", "App\Product", ]
getFields()
-
从配置模型获取表列
-
返回数组
[ "users": [ "id", "name", "email", ], "products": [ "id", "name", "category", "sku" ] ]
parse($slug, $data)
-
解析模板
-
参数
- $slug: 模板 slug
- $data: 要在模板中解析的数据数组 - 返回:编译后的模板字符串