vi-kon / laravel-smarty-view
Laravel 4 的 Smarty 模板引擎
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
- smarty/smarty: 3.1.*
Suggests
- chumper/datatable: Laravel 4 package for the server and client side of datatables at https://datatables.net.cn/
README
Laravel 4 的 Smarty 模板引擎
目录
已知问题
- 无
待办事项
- 修复传入的bug
- 完成文档
功能
- 使用 smarty 模板
- 使用多个模板引擎
安装
将以下行添加到您的 composer.json
文件中
// to your "require" object "vi-kon/laravel-smarty-view": "1.*"
在您的 Laravel 4 项目中,将以下行添加到 app.php
// to your providers array 'ViKon\SmartyView\SmartyViewServiceProvider',
使用方法
只需创建一个扩展名为 .tpl
的新模板文件,并使用 View
类加载。
\View::make('route/to/template/file') ->with("param", null);
在扩展或加载另一个 smarty 模板时,需要带有 view:path
的路径前缀。
{extends file="view:layout-site"}
...
配置文件
在自定义配置中,可以使用 php artisan config:publish vi-kon/laravel-smarty-view
命令轻松发布。该命令将创建新的文件 app/config/packages/vi-kon/laravel-smarty-view/config.php
。该文件需要移动到 app/config/packages/vi-kon/smarty-view/config.php
位置。
config.php
内容
return array( // Enable or disable caching 'caching' => false, // Enable or disable debugging 'debugging' => false, // Set cache lifetime 'cache_lifetime' => 3600, // Check compile 'compile_check' => true, // Set error reporting level in Smarty templates 'error_reporting' => null, // Template directory 'template_dir' => app_path() . '/views', // Cache directory 'cache_dir' => app_path() . '/storage/views/cache', // Compile directory 'compile_dir' => app_path() . '/storage/views/compile', // Plugins directory 'plugins_path' => array(), );
事件
smarty-view.init
在构建 SmartyView 时触发,以添加自定义配置选项。
属性
类型 | 名称 | 描述 |
---|---|---|
Illuminate\Config\Repository |
options |
是 \Config 对象的别名 |
使用
在软件包服务提供商中的示例使用
public function boot() \Event::listen('smarty-view.init', function ($config) { $config->set('smarty-view::plugins_path', array_merge( $config->get('smarty-view::plugins_path'), array(__DIR__ . DIRECTORY_SEPARATOR . 'smarty' . DIRECTORY_SEPARATOR . 'plugins'))); }); }
自定义插件
可用的自定义插件
- auth_check
- auth_user
- config
- datatable
- datatable_column
- form
- form_checkbox
- form_file
- form_hidden
- form_label
- form_password
- form_radio
- form_select
- form_submit
- form_text
- form_textarea
- html_script
- lang
- url
- url_current
- url_full
认证检查
auth_check 标签是
return Auth::check();
返回值类型为 boolean
。
认证用户
auth_user 标签是(如果没有提供 assign
属性)
return Auth::getUser();
返回值类型为 UserInterface
或 null
。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | assign |
将用户数据分配给命名变量而不是返回 | - | - |
配置
config 标签是
return Config::get($key, $default);
返回值类型为 mixed
。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | key |
配置变量键 | x | - |
mixed | default |
如果配置键不存在,则使用默认值 | - | - |
用法
{config key="app.locale"}
数据表
datatable 块标签用于 chumper/datatable Laravel 4 包制作数据表。
插件可以从 lang\{local}\datatable.php
加载本地化(语言)数据。如果语言文件不存在,将使用数据表的默认本地化数据。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | url |
AJAX 数据的完整 URL | - | - |
string | action |
AJAX 数据的路由名称 | - | - |
boolean | searching |
启用或禁用搜索 | - | true |
boolean | lengthChange |
- | - | |
string | class |
将自定义类添加到数据表中 | - | - |
string | view |
用于渲染数据表的自定义视图 | - | "datatable" |
用法
{datatable action="route name" class="table-own" searching=false lengthChange=false} ... {/datatable}
示例 datatable.php 语言文件
return array( 'sEmptyTable' => 'No data available in table', 'sInfo' => 'Showing _START_ to _END_ of _TOTAL_ entries', 'sInfoEmpty' => 'Showing 0 to 0 of 0 entries', 'sInfoFiltered' => '(filtered from _MAX_ total entries)', 'sInfoPostFix' => '', 'sInfoThousands' => ',', 'sLengthMenu' => 'Show _MENU_ entries', 'sLoadingRecords' => 'Loading...', 'sProcessing' => 'Processing...', 'sSearch' => 'Search:', 'sZeroRecords' => 'No matching records found', 'oPaginate' => array( 'sFirst' => 'First', 'sLast' => 'Last', 'sNext' => 'Next', 'sPrevious' => 'Previous', ), 'oAria' => array( 'sSortAscending' => '=> activate to sort column ascending', 'sSortDescending' => '=> activate to sort column descending', ), );
数据表列
将列添加到数据表。必须在 {datatable}
块中声明,否则不会工作。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | label |
列标签 | x | - |
string | token |
用于翻译的列标签标记 | x | - |
boolean | sortable |
启用或禁用列排序 | - | true |
boolean | orderable |
启用或禁用列排序(别名) | - | true |
string | width |
列宽度 | - | "auto" |
string | class |
列类(单独的 td 类) |
- | - |
string | type |
列类型(html,字符串,数值,日期) | - | "html" |
只需提供 label
或 token
之一。
用法
{datatable_column token="language/file.and.token" width="120px" sortable=false}
表单
form 块标签用于打开和关闭表单
return Form::open($params) . $content . Form::close()
返回值类型为 string
,包含生成的 HTML 表单。
属性
将所有参数作为 HTML 属性传递给 Form::open
方法。
用法
{form action="route name" class="form-horizontal" role="form"} ... {/form}
HTML 输出
<form action="generated route from name" method="POST" accept-charset="UTF-8" class="form-horizontal" role="form"> ... </form>
表单复选框
form_checkbox
是 Form::checkbox
的别名。
返回值类型为 string
,包含生成的 HTML 复选框输入字段。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | _name |
HTML name 属性 |
x | - |
mixed | _value |
复选框 value 属性 |
- | null |
boolean | _checked |
如果为 true ,则复选框将被选中,否则不会 |
- | False |
boolean | _populate |
如果 true ,将使用旧输入数据 |
- | False |
用法
{form_checkbox _name="field-checkbox" _checked=true class="checkbox"}
表单文件
form_file
是 Form::file
的别名。
返回值类型为 string
,包含生成的 HTML 文件输入字段。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | _name |
HTML name 属性 |
x | - |
用法
{form_file _name="field-file"}
表单隐藏字段
form_hidden
是 Form::hidden
的别名。
返回值类型为 string
,包含生成的 HTML 隐藏输入字段。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | _name |
HTML name 属性 |
x | - |
mixed | _value |
隐藏字段的 value 属性 |
- | null |
boolean | _populate |
如果 true ,将使用旧输入数据 |
- | False |
用法
{form_hidden _name="field-hidden" value="hidden-value"}
表单密码
form_password
是 Form::password
的别名。
返回值类型为 string
,包含生成的 HTML 密码输入字段。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | _name |
HTML name 属性 |
x | - |
用法
{form_password _name="field-password"}
表单单选框
form_radio
是 Form::radio
的别名。
返回值类型为 string
,包含生成的 HTML 单选框输入字段。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | _name |
HTML name 属性 |
x | - |
mixed | _value |
单选框的 value 属性 |
- | null |
boolean | _checked |
如果 true ,单选框将被选中,否则不会 |
- | False |
boolean | _populate |
如果 true ,将使用旧输入数据 |
- | False |
用法
{form_radio _name="field-radio" _checked=true class="radio"}
表单选择框
form_select
是 Form::select
或 Form::selectRange
的别名。
返回值类型为 string
,包含生成的 HTML 选择框字段。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | _name |
HTML name 属性 |
x | - |
string | _default |
单选框的 value 属性 |
- | null |
string[] | _list |
选择项作为关联数组 | - | array() |
mixed | _selected |
选择的索引值 | - | array() |
boolean | _range |
如果 true ,单选框将生成为选择范围 |
- | False |
string | _begin |
范围起始值 | 如果范围为真 | "" |
string | _end |
范围结束值 | 如果范围为真 | "" |
boolean | _populate |
如果 true ,将使用旧输入数据 |
- | False |
用法
{form_select _name="field-checkbox" _list=$data}
{form_select _name="field-checkbox" _range=true _begin=5 _end=100 class="checkbox"}
表单文本
form_text
是 Form::text
的别名。
返回值类型为 string
,包含生成的 HTML 文本字段。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | _name |
HTML name 属性 |
x | - |
string | _value |
文本的 value 属性 |
- | null |
boolean | _populate |
如果 true ,将使用旧输入数据 |
- | False |
用法
{form_text _name="field-text" class="text"}
表单文本区域
form_textarea
是 Form::textarea
的别名。
返回值类型为 string
,包含生成的 HTML 文本区域字段。
属性
类型 | 名称 | 描述 | 必需 | 默认 |
---|---|---|---|---|
string | _name |
HTML name 属性 |
x | - |
string | _value |
文本区域的 value 属性 |
- | null |
boolean | _populate |
如果 true ,将使用旧输入数据 |
- | False |
用法
{form_textarea _name="field-textarea" class="textarea"}
许可协议
本软件包采用MIT许可协议授权