Coder Social home page Coder Social logo

mthaml's Introduction

Multi target HAML

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.name
      Email: #{user.email}
      %a(href=user.url) Home page

Rendered:

<ul id="users">
  {% for user in users %}
    <li>
      {{ 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->getName()
      Email: #{$user->getEmail()}
      %a(href=$user->getUrl()) Home page

Rendered:

<ul id="users">
  <?php foreach($users as $user) { ?>
    <li>
      <?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');
$compiled = $haml->compileString($haml_template, "filename");

Twig:

<?php
$haml = new MtHaml\Environment('twig', array('enable_escaper' => false));
$compiled = $haml->compileString($haml_template, "filename");

See examples

Escaping

MtHaml will escape everything by default. As 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 give 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 a Twig_Loader that will automatically convert HAML into Twig at loading time (Twig will then compile the resulting Twig script and cache it). Scripts starting with {% haml %} will be parsed as HAML, 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($twig_loader);

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 has no overhead as everything is done at compile time. Compiled templates are cacheable and don't even need MtHaml to execute.

Unsupported features

Some features are still to be implemented:

  • merging of dynamically-named attributes: %p{"#{foo}" => "bar", "#{foo}" => "baz"} will render twice the same attribute
  • special merging of the id attribute: %p(id="a" id="b") will render as <p id="b">
  • special handling of the data attribute
  • handling of attribute methods and boolean attributes
  • special handling of arrays, hashes and objects as attribute value
  • indenting of dynamic content (while the HTML will be correctly indented, the generated content will be left untouched)

Helpers

Helpers un 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).

Filters

Supported filters are plain, javascript and css. Others may be added in the future.

Example:

%p something
:javascript
  some.javascript.code("#{var|escape('js')}");
<p>something</p>
<script type="text/javascript">
//<![CDATA[
  some.javascript.code("{{ var|escape('js') }}");
//]]>
</script>

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.

License

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

mthaml's People

Contributors

arnaud-lb avatar

Stargazers

Michael Wills avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.