Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/arnaud-lb/MtHaml
Multi target HAML (HAML for PHP, Twig, <your language here>)
https://github.com/arnaud-lb/MtHaml
haml haml-php php twig
Last synced: 4 days ago
JSON representation
Multi target HAML (HAML for PHP, Twig, <your language here>)
- Host: GitHub
- URL: https://github.com/arnaud-lb/MtHaml
- Owner: arnaud-lb
- License: other
- Created: 2011-05-14T19:14:51.000Z (over 13 years ago)
- Default Branch: master
- Last Pushed: 2022-10-23T09:03:16.000Z (about 2 years ago)
- Last Synced: 2024-09-08T12:15:57.103Z (2 months ago)
- Topics: haml, haml-php, php, twig
- Language: PHP
- Homepage:
- Size: 345 KB
- Stars: 359
- Watchers: 23
- Forks: 53
- Open Issues: 28
-
Metadata Files:
- Readme: README.markdown
- Changelog: CHANGELOG
- License: LICENSE
Awesome Lists containing this project
- awesome-php - MtHaml - A PHP implementation of the HAML template language. (Table of Contents / Templating)
- awesome-projects - MtHaml - A PHP implementation of the HAML template language. (PHP / Templating)
- awesome-php - MtHaml - A PHP implementation of the HAML template language. (Table of Contents / Templating)
- awesome-php-cn - MtHaml - HAML模板语言的PHP实现. (目录 / 模板 Templating)
README
# Multi target HAML
[![Build Status](https://secure.travis-ci.org/arnaud-lb/MtHaml.png)](http://travis-ci.org/arnaud-lb/MtHaml)
MtHaml is a PHP implementation of the [HAML language][1] which can target multiple languages.
Currently supported targets are PHP and [Twig][4], 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:
``` haml
%ul#users
- for user in users
%li.user
= user.name
Email: #{user.email}
%a(href=user.url) Home page
```Rendered:
``` jinja
-
{{ user.name }}
Email: {{ user.email }}
Home page
{% for user in users %}
{% endfor %}
```
## HAML/PHP:
``` haml
%ul#users
- foreach($users as $user)
%li.user
= $user->getName()
Email: #{$user->getEmail()}
%a(href=$user->getUrl()) Home page
```
Rendered:
``` php
-
getName(); ?>
Email: getEmail(); ?>
Home page
```
## Usage
PHP:
``` php
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][4]:
``` php
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][7] and [MtHaml with Twig](https://github.com/arnaud-lb/MtHaml/wiki/Use-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:
``` haml
Email #{$user->getEmail()}
%a(href=$user->getUrl()) Home page
```
``` php
Email getEmail(), ENT_QUOTES, 'UTF-8'); ?>
Home page
```
## Twig
Using [Twig][4] 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, ...
``` haml
- 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
addExtension(new MtHaml\Support\Twig\Extension());
$twig->render("rendered_twig_template.twig");
```
## Syntax
The syntax is the same as [HAML/Ruby][1]'s syntax, except that PHP or Twig have to be used where Ruby is expected.
See the [tutorial][2] and the [reference][3]
## 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:
``` haml
%p something
:javascript
some.javascript.code("#{var|escape('js')}");
```
``` jinja
something
//<![CDATA[
some.javascript.code("{{ var|escape('js') }}");
//]]>
```
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
false,
), array(
'coffee' => $coffeeFilter,
));
```
## Sass
[Sass][6] 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).
[1]: http://haml-lang.com/
[2]: http://haml-lang.com/tutorial.html
[3]: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html
[4]: http://www.twig-project.org/
[5]: http://haml-lang.com/docs/yardoc/file.HAML_REFERENCE.html#attribute_methods
[6]: http://sass-lang.com/
[7]: https://github.com/arnaud-lb/MtHaml/blob/master/examples/README.md