cedx/yii2-mustache Mustache templating for the Yii Framework.


Mustache for Yii


Mustache templating for Yii, high-performance PHP framework.

This package provides a view renderer, the yii\mustache\ViewRenderer class. This renderer allows to use Mustache syntax in view templates.


The latest PHP and Composer versions. If you plan to play with the sources, you will also need the latest Phing version.

Installing via Composer

From a command prompt, run:

$ composer global require fxp/composer-asset-plugin
$ composer require cedx/yii2-mustache

Configuring application

In order to start using Mustache you need to configure the view application component, like the following:

use yii\mustache\{ViewRenderer};
use yii\web\{View};

return [
  'components' => [
    'view' => [
      'class' => View::class,
      'renderers' => [
        'mustache' => ViewRenderer::class

After it's done you can create templates in files that have the .mustache extension (or use another file extension but configure the component accordingly). Unlike standard view files, when using Mustache you must include the extension in your $this->render() controller call:

return $this->render('template.mustache', [ 'model' => 'The view model' ]);

Template Syntax

The best resource to learn Mustache basics is its official documentation you can find at mustache.github.io. Additionally there are Yii-specific syntax extensions described below.


Within Mustache templates the following variables are always defined:

  • app: the Yii::$app instance.
  • this: the current View object.
  • yii.debug: the YII_DEBUG constant.
  • yii.devEnv: the YII_ENV_DEV constant.
  • yii.prodEnv: the YII_ENV_PROD constant.
  • yii.testEnv: the YII_ENV_TEST constant.


  • format: provides a set of commonly used data formatting methods.
  • html: provides a set of methods for generating commonly used HTML tags.
  • i18n: provides features related with internationalization (I18N) and localization (L10N).
  • url: provides a set of methods for managing URLs.


There are two ways of referencing partials:

{{> post }}
{{> @app/views/layouts/2columns }}

In the first case the view will be searched relatively to the current view path. For post.mustache that means these will be searched in the same directory as the currently rendered template.

In the second case we're using path aliases. All the Yii aliases such as @app are available by default.

See also


Mustache for Yii is distributed under the MIT License.



This file contains highlights of what changes on each version of the Mustache for Yii library.

Version 9.0.0

  • Breaking change: classes extending from yii\base\Object now extend from BaseObject.
  • Breaking change: raised the required PHP version.
  • Breaking change: using PHP 7.1 features, like nullable types and void functions.

Version 8.0.0

  • Breaking change: removed the CACHE_KEY_PREFIX constants.
  • Breaking change: removed the ViewRenderer::$cacheId property.
  • Added new unit tests.
  • Added the ViewRenderer::$cache property.
  • Added the ViewRenderer::$enableCaching property.
  • Changed licensing for the MIT License.

Version 7.0.0

  • Breaking change: renamed the yii\mustache\helpers\HTML class to Html.
  • Breaking change: renamed the yii\mustache\helpers\URL class to Url.
  • Updated the package dependencies.

Version 6.1.0

  • Enabled the strict typing.
  • Replaced phpDocumentor documentation generator by ApiGen.
  • Updated the package dependencies.

Version 6.0.0

  • Breaking change: added init() methods for validating the required properties.
  • Breaking change: the Cache class now extends from Object class, and implements the Mustache_Cache interface.
  • Breaking change: the Logger class now extends from Object class, and implements the PSR-3 logger interface.
  • Breaking change: replaced most of getters and setters by properties.
  • Ported the unit test assertions from TDD to BDD.
  • Updated the package dependencies.

Version 5.0.0

  • Breaking change: changed the signature of the constructors of the Cache and Logger classes.
  • Breaking change: removed the jsonSerialize() and __toString() methods.
  • Updated the package dependencies.

Version 4.1.0

  • Replaced the Codacy code coverage service by the Coveralls one.
  • Updated the package dependencies.

Version 4.0.0

  • Breaking change: removed the toJSON() methods.
  • Removed the final modifier from the jsonSerialize() methods.

Version 3.0.0

  • Breaking change: modified the signature of some class constructors.
  • Added public getters/setters to some private properties.
  • Added a fluent interface to the setters.
  • Added the jsonSerialize() and toJSON() methods.
  • Updated the package dependencies.

Version 2.0.0

  • Breaking change: moved the Helper class to the yii\mustache namespace.
  • Optimized the unit tests.
  • Removed dependency on framework messages.

Version 1.0.1

  • Updated the package dependencies.

Version 1.0.0

  • First stable release.

Version 0.5.3

  • Code optimization.
  • Updated the package dependencies.

Version 0.5.2

Version 0.5.1

Version 0.5.0

  • Breaking change: using PHP 7 features, like scalar and return type declarations.

Version 0.4.3

  • Added support for a default message category in I18N helper.

Version 0.4.2

  • Added code coverage.
  • Added new unit tests.
  • Added support for Travis CI continuous integration.
  • Changed licensing for the Apache License Version 2.0.
  • Updated the package dependencies.

Version 0.4.1

  • Added support for SonarQube code analyzer.
  • Replaced the custom build scripts by Phing.

Version 0.4.0

  • Dropped the development dependencies based on Node.js.
  • Replaced the build system by custom scripts.
  • Replaced the documentation system by Doxygen.

Version 0.3.0

  • Breaking change: ported the library API to Yii version 2.
  • Updated Mustache dependency to version 2.8.0.

Version 0.2.0

  • Breaking change: ported the library API to namespaces.

Version 0.1.1

  • Added CMustacheI18nHelper helper for internationalization.
  • Breaking change: moved CMustacheHtmlHelper::getTranslate() method to CMustacheI18nHelper class.
  • Fixed GitHub issue #1
  • Lowered the required PHP version.
  • Updated Mustache dependency to version 2.7.0.

Version 0.1.0

  • Initial release.


GitHub Stars
GitHub Forks



v9.0.0 is the latest of 25 releases

MIT license
11 github stars & 1 github forks
0 downloads in the last day
55 downloads in the last 30 days
2418 total downloads