conarwelsh/mustache-l4

Laravel 4 的 Mustache.php 封装器

此包的官方仓库似乎已不存在,因此该包已被冻结。

dev-master 2013-10-02 15:21 UTC

This package is not auto-updated.

Last update: 2021-01-03 18:05:09 UTC


README

Laravel 4 的 Mustache.php 封装器

安装

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

"require": {
	"laravel/framework": "4.0.*",
	"conarwelsh/mustache-l4": "dev-master"
}

运行 composer update,或者如果是全新的项目,则运行 composer install

添加服务提供者

app/config/app.php

...

'Conarwelsh\MustacheL4\MustacheL4ServiceProvider',
	
...

您已设置完成!

使用方法

Mustache-L4 仅是对 Mustache.php 库的封装,将其集成到 Laravel 4 中。

Mustache-L4 会将自己注册到 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 conarwelsh/mustache-l4

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