brightmachine / laratash
Laratash是一个Laravel 5+的包装器,用于mustache.php,这是http://mustache.github.io/的PHP实现
Requires
- php: >=5.4.0
- illuminate/filesystem: ~5.0
- illuminate/support: ~5.0
- illuminate/view: ~5.0
- mustache/mustache: ~2.7
This package is not auto-updated.
Last update: 2024-09-28 16:20:37 UTC
README
Laratash是mustache.php的Laravel包装器,这是http://mustache.github.io/的PHP实现
致谢
本项目完全基于Conar Welsh及其贡献者提供的mustache-l4包。
支持
Laravel 5
mustache/mustache 2.7+
安装
将laratash添加为composer.json
文件中的依赖项
"require": { "laravel/framework": "~5.0", "brightmachine/laratash": "~5.0" }
运行composer update
,或者在全新项目中运行composer install
添加服务提供者
打开:config/app.php
... 'Laratash\LaratashServiceProvider', ...
您已经设置完成!
用法
Laratash仅仅是一个包装器,用于将Mustache.php库集成到Laravel 5+中。
Laratash将自己注册到Laravel视图类中,提供与Laravel的无缝集成。您可以像使用Blade一样使用Mustache。Laravel视图类将根据视图的文件扩展名选择正确的模板引擎。因此,您要渲染Mustache文件,只需确保您的视图具有.mustache
文件扩展名即可。Laratash将处理其余部分。
您甚至可以混合使用模板引擎。例如,您可能有一个Blade布局文件,并且您想嵌套一个Mustache视图,这是可以的!但是请注意,Mustache不理解Blade那样的块区域。Mustache视图将被渲染到一个名为您传递给视图的区域的变量中。例如,如果您这样做:
$view->nest('content', 'some.view'); $view->nest('sidebar', 'some.sidebar');
解析的some.view
文件的内容将在模板文件中通过名为$content
的变量可用。解析的some.sidebar
的内容将在模板文件中通过名为$sidebar
的变量可用。
默认情况下,Mustache部分也使用Laravel的视图查找器加载,因此您可以使用点表示法指定视图。
{{#posts}}
{{> posts._post}}
{{/posts}}
除此之外,一切照旧!
示例
-
使用View::make()的示例
app/views/test.mustache
<h1>{{ pageHeading }}</h1> <div> {{ pageContent }} </div>
app/router.php
Route::get('/', function() { return View::make('test', array( 'pageHeading' => 'Rendered with Mustache.php', 'pageContent' => 'But still looks like Laravel!' )); });
-
使用Blade控制器布局的示例
app/views/layouts/master.blade.php
<html> <head></head> <body> {{-- since Mustache does not use sections, the nested section will instead be rendered as a variable --}} {{ content }} </body> </html>
app/views/test.mustache
<h1>{{ pageHeading }}</h1> <div> {{ pageContent }} </div>
app/controllers/TestController.php
<?php class TestController extends BaseController { public $layout = 'layouts.master'; public function index() { $this->layout->nest('content', 'test', array( 'pageHeading' => 'Rendered with Mustache.php', 'pageContent' => 'But still looks like Laravel!' )); } }
-
使用Mustache布局的示例
app/views/posts/_post.mustache
<article> <h2>{{ title }}</h2> <div> {{ content }} </div> </article>
app/views/blog/index.mustache
<html> <head></head> <body> <h1>My Blog</h1> {{#posts}} {{> posts._post}} {{/posts}} </body> </html>
app/routes.php
Route::get('/', function() { $posts = array( array( 'title' => 'This is a Title', 'content' => 'lorem ipsum...' ), array( 'title' => 'This is a another title', 'content' => 'lorem ipsum...' ), array( 'title' => 'This is yet another Title', 'content' => 'lorem ipsum...' ), ); return View::make('blog.index', compact('posts)); });
关于模板数据的说明
Laravel期望视图数据作为数组
传递。
然而,Mustache PHP也允许使用Context对象。
如果您想使用Context对象,请传递一个包含__context
键的数组,这将被使用。例如:
`return view('my.view', ['__context' => new Context]);`
配置
您可以在您的ConfigServiceProvider
中更改传递给Mustache.php的配置选项。例如:
config([
'laratash.cache' => storage_path() . '/framework/views/mustache',
]);
所有的laratash.
选项都直接传递给Mustache_Engine构造函数,因此您可以像使用Mustache.php一样使用任何选项。