avatarguru / mustache-l5
用于 Laravel 5 的 Mustache.php 包装器
Requires
- php: >=5.3.0
- illuminate/support: ~4|~5
- mustache/mustache: ~2.5
This package is auto-updated.
Last update: 2024-09-19 11:57:34 UTC
README
用于 Laravel v4 | v5 的 Mustache.php 包装器
安装
将 mustache-l5 添加为依赖项到您的 composer.json
文件
"require": { "laravel/framework": "4.2.*", "avatarguru/mustache-l5": "dev-master" }
运行 composer update
,或者如果是全新项目,运行 composer install
添加服务提供者
app/config/app.php
... 'Avatarguru\MustacheL5\MustacheL5ServiceProvider', ...
一切设置完成!
用法
Mustache-L5 仅仅是 Mustache.php 库的包装器,将其集成到 Laravel 4。
Mustache-L5 会与 Laravel 视图类注册自己,提供与 Laravel 的无缝集成。您可以使用 Mustache 就像使用 Blade 一样!Laravel 视图类将根据视图文件的扩展名选择正确的模板引擎。因此,要渲染 Mustache 文件,只需确保您的视图具有 .mustache
文件扩展名。Mustache-L4 将处理其余部分。
您甚至可以混合使用模板引擎。例如,您可能有一个 Blade 布局文件,并想要嵌套一个 Mustache 视图,这完全可以!但是请注意,Mustache 不像 Blade 那样理解区块部分。Mustache 视图将被渲染到名为您传递给视图的任何部分的变量中。例如,如果您这样做:
$this->layout->nest('content', 'some.view'); $this->layout->nest('sidebar', 'some.sidebar');
解析后的 some.view
文件的内容将在模板文件中通过一个名为 $content
的变量可用。解析后的 some.sidebar
的内容将在模板文件中,通过一个名为 $sidebar
的变量可用。
默认情况下,Mustache 部分也是使用 Laravel 的 ViewFinder 加载的,所以您可以使用点表示法指定视图。
{{#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)); });
配置
您可以通过发布配置文件来修改传递给 Mustache.php 的配置选项。
php artisan config:publish avatarguru/mustache-l5
此配置文件中的所有选项都直接传递给 Mustache_Engine 构造函数,因此您可以使用与 Mustache.php 一起使用的任何选项。