mervick/yii2-mthaml

Yii2框架的MtHaml集成

安装量: 1,096

依赖项: 0

建议者: 0

安全性: 0

星标: 8

关注者: 2

分支: 1

开放性问题: 6

类型:yii2-extension

0.1.3 2015-03-26 07:00 UTC

This package is not auto-updated.

Last update: 2024-09-14 16:18:54 UTC


README

Analytics

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许可证发布。