| 148 |
lars |
1 |
# Laravel.Smarty
|
|
|
2 |
|
|
|
3 |
Smarty Template Engine for Laravel
|
|
|
4 |
(Support for Laravel5.x - Laravel8.x and Lumen)
|
|
|
5 |
|
|
|
6 |
[](https://travis-ci.org/ytake/Laravel.Smarty)
|
|
|
7 |
[](https://coveralls.io/r/ytake/Laravel.Smarty?branch=master)
|
|
|
8 |
[](https://scrutinizer-ci.com/g/ytake/Laravel.Smarty/?branch=master)
|
|
|
9 |
|
|
|
10 |
[](https://packagist.org/packages/ytake/laravel-smarty)
|
|
|
11 |
[](https://packagist.org/packages/ytake/laravel-smarty)
|
|
|
12 |
[](https://packagist.org/packages/ytake/laravel-smarty)
|
|
|
13 |
|
|
|
14 |
## Installation For Laravel
|
|
|
15 |
Require this package with Composer
|
|
|
16 |
|
|
|
17 |
```bash
|
|
|
18 |
$ composer require ytake/laravel-smarty
|
|
|
19 |
```
|
|
|
20 |
|
|
|
21 |
or composer.json
|
|
|
22 |
|
|
|
23 |
```json
|
|
|
24 |
"require": {
|
|
|
25 |
"ytake/laravel-smarty": "^6.0"
|
|
|
26 |
},
|
|
|
27 |
```
|
|
|
28 |
|
|
|
29 |
*Supported Auto-Discovery(^Laravel5.5)*
|
|
|
30 |
|
|
|
31 |
add Laravel.Smarty Service Providers
|
|
|
32 |
|
|
|
33 |
your config/app.php
|
|
|
34 |
```php
|
|
|
35 |
'providers' => [
|
|
|
36 |
// add smarty extension
|
|
|
37 |
Ytake\LaravelSmarty\SmartyServiceProvider::class,
|
|
|
38 |
// add artisan commands
|
|
|
39 |
Ytake\LaravelSmarty\SmartyConsoleServiceProvider::class,
|
|
|
40 |
]
|
|
|
41 |
```
|
|
|
42 |
|
|
|
43 |
## Installation For Lumen
|
|
|
44 |
Require this package with Composer
|
|
|
45 |
|
|
|
46 |
```bash
|
|
|
47 |
$ composer require ytake/laravel-smarty
|
|
|
48 |
```
|
|
|
49 |
|
|
|
50 |
or composer.json
|
|
|
51 |
|
|
|
52 |
```json
|
|
|
53 |
"require": {
|
|
|
54 |
"ytake/laravel-smarty": "~2.0"
|
|
|
55 |
},
|
|
|
56 |
```
|
|
|
57 |
|
|
|
58 |
register Laravel.Smarty Service Providers
|
|
|
59 |
|
|
|
60 |
your bootstrap/app.php
|
|
|
61 |
```php
|
|
|
62 |
$app->configure('ytake-laravel-smarty');
|
|
|
63 |
$app->register(Ytake\LaravelSmarty\SmartyServiceProvider::class);
|
|
|
64 |
$app->register(Ytake\LaravelSmarty\SmartyConsoleServiceProvider::class);
|
|
|
65 |
```
|
|
|
66 |
|
|
|
67 |
## Configuration
|
|
|
68 |
|
|
|
69 |
### publish configuration file (for Laravel5)
|
|
|
70 |
|
|
|
71 |
```bash
|
|
|
72 |
$ php artisan vendor:publish
|
|
|
73 |
```
|
|
|
74 |
|
|
|
75 |
publish to config directory
|
|
|
76 |
|
|
|
77 |
Of Course, Blade Template can also be used to Render Engine.
|
|
|
78 |
|
|
|
79 |
### configuration file (for Lumen)
|
|
|
80 |
|
|
|
81 |
Copy the `vendor/ytake/laravel-smarty/src/config/ytake-laravel-smarty.php` file to your local config directory
|
|
|
82 |
|
|
|
83 |
### config for Production
|
|
|
84 |
edit config/ytake-laravel-smarty.php
|
|
|
85 |
|
|
|
86 |
```php
|
|
|
87 |
// enabled smarty template cache
|
|
|
88 |
'caching' => true, // default false
|
|
|
89 |
|
|
|
90 |
// disabled smarty template compile
|
|
|
91 |
'force_compile' => false, // default true(for develop)
|
|
|
92 |
```
|
|
|
93 |
|
|
|
94 |
Or
|
|
|
95 |
|
|
|
96 |
add .env file
|
|
|
97 |
```
|
|
|
98 |
SMARTY_CACHE=true
|
|
|
99 |
SMARTY_COMPILE=false
|
|
|
100 |
```
|
|
|
101 |
|
|
|
102 |
edit config/ytake-laravel-smarty.php
|
|
|
103 |
|
|
|
104 |
```php
|
|
|
105 |
'caching' => env('SMARTY_CACHING', false),
|
|
|
106 |
|
|
|
107 |
'force_compile' => env('SMARTY_FORCE_COMPILE', true),
|
|
|
108 |
```
|
|
|
109 |
|
|
|
110 |
and more..!
|
|
|
111 |
|
|
|
112 |
## Basic
|
|
|
113 |
easily use all the methods of Smarty
|
|
|
114 |
|
|
|
115 |
```php
|
|
|
116 |
// laravel5 view render
|
|
|
117 |
view("template.name");
|
|
|
118 |
|
|
|
119 |
// Laravel blade template render(use Facades)
|
|
|
120 |
\View::make('template', ['hello']);
|
|
|
121 |
// use Smarty method
|
|
|
122 |
|
|
|
123 |
\View::assign('word', 'hello');
|
|
|
124 |
\View::clearAllAssign(); // smarty method
|
|
|
125 |
```
|
|
|
126 |
|
|
|
127 |
## View Composer, and View Share
|
|
|
128 |
|
|
|
129 |
```php
|
|
|
130 |
$this->app['view']->composer('index', function (View $view) {
|
|
|
131 |
$view->with('message', 'enable smarty');
|
|
|
132 |
});
|
|
|
133 |
$this->app['view']->share('title', 'laravel-smarty');
|
|
|
134 |
|
|
|
135 |
```
|
|
|
136 |
|
|
|
137 |
```html
|
|
|
138 |
Hello Laravel.Smarty
|
|
|
139 |
|
|
|
140 |
{$title}
|
|
|
141 |
|
|
|
142 |
{$message}
|
|
|
143 |
```
|
|
|
144 |
|
|
|
145 |
## Artisan
|
|
|
146 |
smarty's cache clear, remove compile class from Artisan(cli)
|
|
|
147 |
|
|
|
148 |
### Template cache clear
|
|
|
149 |
```bash
|
|
|
150 |
$ php artisan ytake:smarty-clear-cache
|
|
|
151 |
```
|
|
|
152 |
|
|
|
153 |
| Options | description |
|
|
|
154 |
| ------------- | ------------- |
|
|
|
155 |
| --file (-f) | specify file |
|
|
|
156 |
| --time (-t) | clear all of the files that are specified duration time |
|
|
|
157 |
| --cache_id (-cache) | specified cache_id groups |
|
|
|
158 |
|
|
|
159 |
### Remove compile file
|
|
|
160 |
|
|
|
161 |
```bash
|
|
|
162 |
$ php artisan ytake:smarty-clear-compiled
|
|
|
163 |
```
|
|
|
164 |
|
|
|
165 |
| Options | description |
|
|
|
166 |
| ------------- | ------------- |
|
|
|
167 |
| --file (-f) | specify file |
|
|
|
168 |
| --compile_id (-compile) | specified compile_id |
|
|
|
169 |
|
|
|
170 |
### Template Compiler
|
|
|
171 |
|
|
|
172 |
```bash
|
|
|
173 |
$ php artisan ytake:smarty-optimize
|
|
|
174 |
```
|
|
|
175 |
|
|
|
176 |
| Options | description |
|
|
|
177 |
| ------------- | ------------- |
|
|
|
178 |
| --extension (-e) | specified smarty file extension(default: *.tpl*) |
|
|
|
179 |
| --force | compiles template files found in views directory |
|
|
|
180 |
|
|
|
181 |
## Template Caching
|
|
|
182 |
|
|
|
183 |
**choose file, memcached, Redis**
|
|
|
184 |
(default file cache driver)
|
|
|
185 |
|
|
|
186 |
```php
|
|
|
187 |
// smarty cache driver "file", "memcached", "redis"
|
|
|
188 |
'cache_driver' => 'file',
|
|
|
189 |
|
|
|
190 |
// memcached servers
|
|
|
191 |
'memcached' => [
|
|
|
192 |
[
|
|
|
193 |
'host' => '127.0.0.1',
|
|
|
194 |
'port' => 11211,
|
|
|
195 |
'weight' => 100
|
|
|
196 |
],
|
|
|
197 |
],
|
|
|
198 |
|
|
|
199 |
// redis configure
|
|
|
200 |
'redis' => [
|
|
|
201 |
[
|
|
|
202 |
'host' => '127.0.0.1',
|
|
|
203 |
'port' => 6379,
|
|
|
204 |
'database' => 0,
|
|
|
205 |
],
|
|
|
206 |
],
|
|
|
207 |
```
|
|
|
208 |
|
|
|
209 |
### example
|
|
|
210 |
[registerFilter in ServiceProvider](https://gist.github.com/ytake/e8c834e88473ea3f10e7)
|
|
|
211 |
[registerFilter in Controller](https://gist.github.com/ytake/1a6f1d5312b552bc83ff)
|
|
|
212 |
[layout.sample](https://gist.github.com/ytake/11345539)
|
|
|
213 |
[layout.extends.sample](https://gist.github.com/ytake/11345614)
|