alexvasilyev/mthaml
Composer 安装命令:
composer require alexvasilyev/mthaml
包简介
HAML for PHP
关键字:
README 文档
README
MtHaml is a PHP implementation of the HAML language which can target multiple languages.
Currently supported targets are PHP and Twig, and new ones can be added easily.
Mt-Haml implements the exact same syntax as ruby-haml; the only difference is that any supported language can be used everywhere HAML expects Ruby code:
HAML/Twig:
%ul#users - for user in users %li.user = user.name Email: #{user.email} %a(href=user.url) Home page
Rendered:
<ul id="users"> {% for user in users %} <li class="user"> {{ user.name }} Email: {{ user.email }} <a href="{{ user.url }}">Home page</a> </li> {% endfor %} </ul>
HAML/PHP:
%ul#users - foreach($users as $user) %li.user = $user->getName() Email: #{$user->getEmail()} %a(href=$user->getUrl()) Home page
Rendered:
<ul id="users"> <?php foreach($users as $user) { ?> <li class="user"> <?php echo $user->getName(); ?> Email: <?php echo $user->getEmail(); ?> <a href="<?php echo $user->getUrl(); ?>">Home page</a> </li> <?php } ?> </ul>
Usage
PHP:
<?php $haml = new MtHaml\Environment('php'); $executor = new MtHaml\Support\Php\Executor($haml, array( 'cache' => sys_get_temp_dir().'/haml', )); // Compiles and executes the HAML template, with variables given as second // argument $executor->display('template.haml', array( 'var' => 'value', ));
Twig:
<?php $haml = new MtHaml\Environment('twig', array('enable_escaper' => false)); // Use a custom loader, whose responsibility is to convert HAML templates // to Twig syntax, before handing them out to Twig: $hamlLoader = new MtHaml\Support\Twig\Loader($haml, $twig->getLoader()); $twig->setLoader($hamlLoader); // Register the Twig extension before executing a HAML template $twig->addExtension(new MtHaml\Support\Twig\Extension()); // Render templates as usual $twig->render('template.haml', ...);
See examples and MtHaml with Twig
Escaping
MtHaml escapes everything by default. Since Twig already supports auto escaping it is recommended to enable it in Twig and disable it in MtHaml:
new MtHaml\Environment('twig', array('enable_escaper' => false));
HAML/PHP is rendered like this when auto escaping is enabled:
Email #{$user->getEmail()}
%a(href=$user->getUrl()) Home page
Email <?php echo htmlspecialchars($user->getEmail(), ENT_QUOTES, 'UTF-8'); ?> <a href="<?php echo htmlspecialchars($user->getUrl(), ENT_QUOTES, 'UTF-8'); ?>">Home page</a>
Twig
Using Twig in HAML gives more control over what can be executed, what variables and functions are exposed to the templates, etc. This also allows to use all of Twig's awesome features like template inheritance, macros, blocks, filters, functions, tests, ...
- extends "some-template.haml" - macro printSomething() %p something - block body %h1 Title = _self.printSomething()
Integration in Twig
MtHaml comes with an example Twig_Loader that will automatically convert HAML into Twig at loading time (Twig will then compile the resulting Twig script and cache it). Templates with a .haml extension, or whose source starts with {% haml %} will be converted, and the others will be left untouched.
The loader acts as a proxy and takes an other loader as parameter:
<?php $haml = new MtHaml\Environment(...); $twig_loader = new Twig_Loader_Filesystem(...); $twig_loader = new MtHaml\Support\Twig\Loader($haml, $twig_loader);
Runtime support
Compiled MtHaml/Twig templates need support from MtHaml at runtime in some cases. Because of this, a Twig extension must be loaded before executing the templates.
<?php // Register the MtHaml extension before executing the template: $twig->addExtension(new MtHaml\Support\Twig\Extension()); $twig->render("rendered_twig_template.twig");
Syntax
The syntax is the same as HAML/Ruby's syntax, except that PHP or Twig have to be used where Ruby is expected.
See the tutorial and the reference
Performance
MtHaml converts HAML to PHP or Twig code. The resulting code can be cached and executed any number of times, and doesn't depend on HAML at runtime.
MtHaml has no runtime overhead.
Helpers
Helpers in HAML/Ruby are just ruby functions exposed to templates. Any function can be made available to HAML templates by the target language (the function only have to be available at runtime).
In HAML/Twig you can use all of Twig's functions, filters, and tags. In HAML/PHP, you can use all PHP functions.
Filters
Filters take plain text input (with support for #{...} interpolations) and transform it, or wrap it.
Example with the javascript filter:
%p something :javascript some.javascript.code("#{var|escape('js')}");
<p>something</p> <script type="text/javascript"> //<![CDATA[ some.javascript.code("{{ var|escape('js') }}"); //]]> </script>
The following filters are available:
- css: wraps with style tags
- cdata: wraps with CDATA markup
- coffee*: compiles coffeescript to javascript
- escaped: html escapes
- javascript: wraps with script tags
- less*: compiles as Lesscss
- markdown*: converts markdown to html
- php: executes the input as php code
- plain: does not parse the filtered text
- preseve: preserves preformatted text
- scss*: converts scss to css
- twig: executes the input as twig code
Filter marked with * have runtime dependencies and are not enabled by default. Such filters need to be provided to MtHaml\Environment explicitly.
Example with the Coffee filter:
<?php $coffeeFilter = new MtHaml\Filter\CoffeeScript(new CoffeeScript\Compiler); $env = new MtHaml\Environment('twig', array( 'enable_escaper' => false, ), array( 'coffee' => $coffeeFilter, ));
Sass
Sass can be used in PHP projects without problem. It only depends on Ruby and does not need to be installed on production servers. So MtHaml will not re-implement Sass.
Frameworks and CMS support
- CakePHP: https://github.com/TiuTalk/haml
- Drupal: https://github.com/antoinelafontaine/oxide
- FuelPHP: https://github.com/fuel/parser
- Laravel (PHP): https://github.com/BKWLD/laravel-haml
- Laravel (Twig): https://github.com/SimonDegraeve/laravel-twigbridge
- PHPixie: https://github.com/dracony/PHPixie-HAML
- Silex: https://github.com/arnaud-lb/Silex-MtHaml
- Sprockets-PHP: https://github.com/Nami-Doc/Sprockets-PHP
- Symfony2: https://github.com/arnaud-lb/MtHamlBundle
- Yii2 Framework: https://github.com/mervick/yii2-mthaml
- Zend Framework 1: https://github.com/bonndan/mthaml-zf1
- PhileCMS: https://bitbucket.org/jacmoe/templatemthaml
Add yours: https://github.com/arnaud-lb/MtHaml/edit/master/README.markdown
License
MtHaml is released under the MIT license (same as HAML/Ruby).
alexvasilyev/mthaml 适用场景与选型建议
alexvasilyev/mthaml 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 801 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 06 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「HAML」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 alexvasilyev/mthaml 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 alexvasilyev/mthaml 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 alexvasilyev/mthaml 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
jade-esque templates for PHP 5.3
Wraps MtHaml for ease use in Laravel 5 with Blade syntax style
HAML for PHP
This Bundle provides a HAML templating engine
Core files for Theme Juice framework
Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"
统计信息
- 总下载量: 801
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 5
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-06-09