mayconbordin/l5-mustache

Laravel 5 的 Mustache.php 包装器

v1.0 2016-09-13 15:10 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:50:22 UTC


README

Latest Stable Version Total Downloads Latest Unstable Version License

Laravel 5 的 Mustache.php 包装器。

安装

将 mustache-l5 添加为依赖项到您的 composer.json 文件

"require": {
	"laravel/framework": "5.1.*",
	"mayconbordin/l5-mustache": "dev-master"
}

运行 composer update,或者在全新项目中运行 composer install

添加服务提供者

app/config/app.php

...

'Mayconbordin\L5Mustache\L5MustacheServiceProvider',
	
...

您已经全部设置好了!

用法

Mustache-L5 仅仅是对 Mustache.php 库的包装,将其集成到 Laravel 4。

Mustache-L5 会将自己注册到 Laravel 视图类中,提供与 Laravel 的无缝集成。您可以使用 Mustache 就像使用 Blade 一样!Laravel 视图类将根据视图文件的扩展名选择合适的模板引擎。因此,您只需确保您的视图文件有 .mustache 扩展名,Mustache-L5 会处理其余的工作。

您甚至可以混合和匹配模板引擎。例如,也许您有一个 Blade 布局文件,并且您想嵌套一个 Mustache 视图,这是可以的!但是请注意,Mustache 不像 Blade 那样理解区块部分。Mustache 视图将被渲染到一个名为 whatever section 的变量中。例如,如果您要执行

$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));
      });
    

助手函数

翻译

{{#lang}}posts.title{{/lang}}

{{#choice}}posts.title|2{{/choice}}

配置

您可以通过发布配置文件来更改传递给 Mustache.php 的配置选项。

php artisan config:publish avatarguru/mustache-l5

此配置文件中的所有选项都直接传递给 Mustache_Engine 构造函数,因此您可以使用与 Mustache.php 一样使用的任何选项。