danphyxius/mustache-l4

用于Laravel 4的Mustache.php包装器

dev-master 2015-01-09 10:48 UTC

This package is not auto-updated.

Last update: 2024-09-24 15:58:27 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的无缝集成。您可以像使用Blade一样使用Mustache!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相同的所有选项。