mervick / yii2-mthaml
Yii2框架的MtHaml集成
0.1.3
2015-03-26 07:00 UTC
Requires
- php: >=5.4.0
- mthaml/mthaml: ~1.7
- yiisoft/yii2: *
Requires (Dev)
- cebe/markdown: ~1.0.1
- coffeescript/coffeescript: ~1
- erusev/parsedown: *
- fluxbb/commonmark: ~1@dev
- gregwar/rst: ~1
- kzykhys/ciconia: ~1
- leafo/lessphp: *
- leafo/scssphp: *
- leafo/scssphp-compass: dev-master
- league/commonmark: >=0.5
- michelf/php-markdown: ~1.3
- oyejorge/less.php: *
- twig/extensions: ~1.1.0
- twig/twig: ~1.11
Suggests
- leafo/scssphp-compass: If you want to use Compass library with Scss
- twig/extensions: If you want to use Twig extensions
This package is not auto-updated.
Last update: 2024-09-14 16:18:54 UTC
README
Yii2框架的MtHaml集成。
扩展提供了一个 ViewRenders
,允许您使用Haml/Twig视图模板引擎,通过使用 多目标HAML (MtHaml) 库。
要求
- YII 2.0
- PHP 5.4+
- Composer
使用Composer安装
建议通过运行 composer 来安装
composer require mervick/yii2-mthaml "*"
也用于使用twig
composer require twig/twig "~1.11"
用法
将以下内容添加到您的 config/main.php
文件中
return [ //.... 'components' => [ 'view' => [ 'renderers' => [ 'haml' => [ 'class' => 'mervick\mthaml\HamlViewRenderer', ], 'twig' => [ 'class' => 'mervick\mthaml\TwigViewRenderer', ], ], ], ], ];
在控制器中渲染
class SiteController extends Controller { //.... public function actionIndex() { return $this->render('index.haml', $params); // or if your want to use twig // return $this->render('index.twig', $params); } //.... }
MtHaml选项
这是默认选项
//.... 'renderers' => [ 'haml' => [ 'class' => 'mervick\mthaml\HamlViewRenderer', 'cachePath' => '@runtime/Haml/cache', 'debug' => false, 'options' => [ 'format' => 'html5', // MtHaml escapes everything by default 'enable_escaper' => true, 'escape_html' => true, 'escape_attrs' => true, 'cdata' => true, 'autoclose' => array('meta', 'img', 'link', 'br', 'hr', 'input', 'area', 'param', 'col', 'base'), 'charset' => 'UTF-8', 'enable_dynamic_attrs' => true, ], ], 'twig' => [ 'class' => 'mervick\mthaml\TwigViewRenderer', 'cachePath' => '@runtime/Twig/cache', 'debug' => false, 'options' => [ // Same as for haml, except "enable_escaper" // Twig extension already supports auto escaping, so it turned off for MtHaml 'enable_escaper' => false, ], ], //....
过滤器
过滤器接受纯文本输入(支持 #{...}
插值),并将其转换或包装。 了解更多
以下过滤器默认可用
css
:用样式标签包装cdata
:用CDATA标记包装escaped
:html转义javascript
:用脚本标签包装php
:将输入作为php代码执行plain
:作为纯文本渲染preserve
:保留预格式化文本
默认未启用的过滤器
coffee
:将coffeescript编译成javascript- 依赖
coffeescript/coffeescript "~1"
(CoffeeScript)
- 依赖
less
:将Lesscss编译成CSS- 依赖于以下之一
oyejorge/less.php "*"
(OyejorgeLess)
或leafo/lessphp "*"
(LeafoLess)
- 依赖于以下之一
scss
:将scss转换为css- 依赖
leafo/scssphp "*"
(Scss)
此外,要使用 Compass,请添加leafo/scssphp-compass "dev-master"
- 依赖
markdown
:将markdown转换为html- 依赖于以下之一
michelf/php-markdown "~1.3"
(MichelfMarkdown,MichelfMarkdownExtra)
或cebe/markdown "~1.0.1"
(CebeMarkdown,CebeMarkdownExtra,CebeGithubMarkdown)
或erusev/parsedown "*"
(Parsedown)
或league/commonmark ">=0.5"
(CommonMark)
或kzykhys/ciconia "~1"
(CiconiaMarkdown)
或fluxbb/commonmark "~1@dev"
(FluxBBMarkdown)
- 依赖于以下之一
rest
:将reStructuredText转换为html- 依赖
gregwar/rst "~1"
(ReST)
- 依赖
要启用非默认过滤器,您必须首先通过composer安装它们。
例如,安装coffee过滤器
composer require coffeescript/coffeescript "~1"
之后,将其包含到您的配置文件中
//.... 'renderers' => [ 'haml' => [ 'class' => 'mervick\mthaml\HamlViewRenderer', 'cachePath' => '@runtime/Haml/cache', 'options' => [ //.... ], 'filters' => [ // shorten 'coffee' => 'CoffeeScript', // also you can specify filter options 'coffee' => [ 'filter' => 'CoffeeScript', 'options' => [ 'header' => false, 'trace' => '@runtime/Haml/coffee-trace.log', ], ], ], ], //....
所有过滤器及其默认选项的列表
'coffee' => [ // Package: "coffeescript/coffeescript" 'filter' => 'CoffeeScript', 'options' => [ // add a "Generated by..." header 'header' => true, // reference to token stream (debugging) 'tokens' => null, // file to write parser trace to (debugging) 'trace' => null, ], ], 'less' => [ // Package: "oyejorge/less.php" 'filter' => 'OyejorgeLess', 'options' => [ // whether to compress 'compress' => false, // whether units need to evaluate correctly 'strictUnits' => false, // whether math has to be within parenthesis 'strictMath' => false, // option - whether to adjust URL's to be relative 'relativeUrls' => true, // whether to add args into url tokens 'urlArgs' => [], 'numPrecision' => 8, // import dirs 'importDirs' => [], // cache dir 'cacheDir' => null ], ], 'less' => [ // Package: "leafo/lessphp" 'filter' => 'LeafoLess', 'options' => [ // import dirs 'importDirs' => [], ], ], 'scss' => [ // Package: "leafo/scssphp" 'filter' => 'Scss', 'options' => [ // import dirs 'importDirs' => [], // enable Compass integration, depends on "leafo/scssphp-compass" 'enableCompass' => false, ], ], 'markdown' => [ // Package: "michelf/php-markdown" 'filter' => 'MichelfMarkdown', 'options' => [ 'forceOptimization' => false, 'empty_element_suffix' => " />", 'tab_width' => 4, 'no_markup' => false, 'no_entities' => false, 'predef_urls' => [], 'predef_titles' => [], ], ], 'markdown' => [ // Package: "michelf/php-markdown" 'filter' => 'MichelfMarkdownExtra', 'options' => [ // Same as for MichelfMarkdown ], ], 'markdown' => [ // Package: "cebe/markdown" 'filter' => 'CebeMarkdown', 'options' => [ 'forceOptimization' => false, 'html5' => false, ], ], 'markdown' => [ // Package: "cebe/markdown" 'filter' => 'CebeMarkdownExtra', 'options' => [ 'forceOptimization' => false, 'html5' => false, ], ], 'markdown' => [ // Package: "cebe/markdown" 'filter' => 'CebeGithubMarkdown', 'options' => [ 'forceOptimization' => false, 'html5' => false, 'enableNewlines' => false, ], ], 'markdown' => [ // Package: "kzykhys/ciconia" 'filter' => 'CiconiaMarkdown', 'options' => [ 'forceOptimization' => false, 'tabWidth' => 4, 'nestedTagLevel' => 3, 'strict' => false, ], ], 'markdown' => [ // Package: "league/commonmark" 'filter' => 'CommonMark', 'options' => [ 'forceOptimization' => false, ], ], 'markdown' => [ // Package: "fluxbb/commonmark" 'filter' => 'FluxBBMarkdown', 'options' => [ 'forceOptimization' => false, ], ], 'markdown' => [ // Package: "erusev/parsedown" 'filter' => 'Parsedown', 'options' => [ 'forceOptimization' => false, ], ], 'rest' => [ // Package: "gregwar/rst" 'filter' => 'ReST', // no options ],
Twig扩展
《Twig扩展》是一个库,为Twig提供了一些有用的扩展。
通过Composer安装
composer require twig/extensions "~1.1.0"
将以下行添加到配置文件中
//.... 'renderers' => [ 'twig' => [ 'class' => 'mervick\mthaml\TwigViewRenderer', 'options' => [ //... ], 'extensions' => [ 'Text', # Provides useful filters for text manipulation; 'I18n', # Adds internationalization support via the gettext library; 'Intl', # Adds a filter for localization of DateTime objects; 'Array', # Provides useful filters for array manipulation; 'Date', # Adds a filter for rendering the difference between dates. ], ], //....
注意!
在模板内部,您必须使用 $view
而不是 $this
,例如
-use backend\assets\AppAsset -use yii\helpers\Html -AppAsset::register($view) -$view->beginPage() !!! %html{:lang=>Yii::$app->language} %head %meta{:charset=>Yii::$app->charset} %meta(name="viewport" content="width=device-width, initial-scale=1") !=Html::csrfMetaTags() %title =Html::encode($view->title) -$view->head() %body -$view->beginBody() !=$content -$view->endBody() -$view->endPage()
许可证
MtHaml扩展适用于Yii2框架,采用MIT许可证发布。