• Stars
    star
    363
  • Rank 113,252 (Top 3 %)
  • Language
    PHP
  • License
    Other
  • Created almost 13 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Multi target HAML (HAML for PHP, Twig, <your language here>)

Multi target HAML

Build Status

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

Add yours: https://github.com/arnaud-lb/MtHaml/edit/master/README.markdown

License

MtHaml is released under the MIT license (same as HAML/Ruby).

More Repositories

1

php-rdkafka

Production-ready, stable Kafka client for PHP
C
1,968
star
2

php-memory-profiler

Memory profiler for PHP. Helps finding memory leaks in PHP scripts.
C
773
star
3

vim-php-namespace

PHP namespace support for VIM. Types "use" statements for you
Vim Script
252
star
4

php-go

php-go allows to call Go code from PHP, with minimal code boilerplate
C
174
star
5

php-inotify

Inotify bindings for PHP 5, 7, and next
C
39
star
6

MtHamlBundle

Symfony2 HAML bundle
PHP
38
star
7

imagesize.js

Get the size of an image without reading or downloading it entirely
JavaScript
30
star
8

php-sema

A library for semantic analysis of PHP code
PHP
19
star
9

Zwig

Twig / Zend Framework adapter
PHP
16
star
10

alb-oembed

Simple PHP oEmbed consumer library with discovery support
PHP
16
star
11

TwigReflectionBundle

Displays what's in Twig
PHP
15
star
12

goresize

image resizing proxy written in golang
Go
12
star
13

xtrabackup-manager

xtrabackup-manager fork. Original code at https://code.google.com/p/xtrabackup-manager/
PHP
9
star
14

TwigShellBundle

Simple Twig Shell
PHP
8
star
15

Silex-MtHaml

HAML templating for Silex
PHP
8
star
16

fselectmenu

JavaScript
7
star
17

phpketama

Pure-PHP implementation of libketama, a consistent hashing library
PHP
6
star
18

php-rdkafka-doc

PHP
6
star
19

php-inotify-ffi

Pure-PHP inotify binding (FFI-based)
PHP
5
star
20

binsort

Binsort is a tool to sort files of fixed-length binary records
Go
5
star
21

AlbOpenIDServerBundle

OpenID Provider bundle
PHP
5
star
22

php-throttle

php module for throttling file upload speed
C
4
star
23

fdinfo

progress bar for quiet cli tools
C
4
star
24

OpenISETL

Scala/JVM implementation of the ISETL language
Scala
4
star
25

run

`nohup something >/dev/null 2>&1 &`; shortened
C
3
star
26

sfbootstrap

PHP
3
star
27

mediawiki-php-FastStringSearch

Github mirror of PHP extension FastStringSearch - our actual code is hosted with Gerrit (please see https://www.mediawiki.org/wiki/Developer_access for contributing)
C
2
star
28

zf

zf mirror
PHP
1
star
29

iterm-to-neovim

Converts Iterm2 color schemes to neovim g:terminal_color_x
Go
1
star
30

jQuery.event.queueHandler

Queues handlers to be ran after the current event's handlers have been ran
JavaScript
1
star
31

graphql-demo

A Symfony application demoing a simple GraphQL API
PHP
1
star