zaitsev/mthamlphp

1.1.1 2015-04-07 18:15 UTC

This package is not auto-updated.

Last update: 2024-04-13 14:50:27 UTC


README

Build Status

MtHaml is a PHP implementation of the HAML language which can target multiple languages. This is fork based on MtHaml and MtHaml-more (both runtimes supported if needed) Main target of this fork - implement compiling haml files to php in IDE (to not use any runtime).

differences

This implementation based on and supports all features of MtHaml and adds many new own. But its focused mostly to compile haml files to plain PHP, not to be used as template engine.

You can use grunt task and IDEA plugin to compile haml to php when editing haml-file in IDE.

for basic usage read MtHaml and MtHaml-more documentation.

support IDE highlight

to allow IDE recognize php code in :php section you can use it as

:php <?

simplified array definitions

`['c','d']` is equeal to `array('c','d')`

all statements are equial:

```haml
%i{:data=>['a'=>'a','b'=>$c, 'e'=> true ? $c : $e]}
%i{:data=>{'a'=>'a','b'=>$c, 'e'=> true ? $c : $e}}
%i{:data=>array('a'=>'a','b'=>$c, 'e'=> true ? $c : $e)}
```

attributes mixing

%i.a.b{:class=>['c',$e]}

rendered

<i class="a b <?php echo( implode(' ',array('c',$e))) ;?>"></i>

id attributes with prefixes with '_'

%i#id{:id=>[$c,'2']}

rendered

<i id="id_<?php echo( implode('_',array($c,'2'))) ;?>"></i>

advanced data attribute

%i{:data=>['a'=>'a','b'=>$c, 'e'=> true ? $c : $e]}
%i(data-a="#{$c}" data="#{array($e=>$c)}")
%input{:data=>$data_array}  

rendered

<i <?php foreach(array('a'=>'a','b'=>$c, 'e'=> true ? $c : $e) as $k=>$v) {echo " data-$k=\"$v\" ";} ?>></i>
<i data-a="<?php echo($c) ;?>"<?php foreach(array($e=>$c) as $k=>$v) {echo " data-$k=\"$v\" ";} ?>></i>
<input<?php foreach($data_array as $k=>$v) {echo " data-$k=\"$v\" ";} ?>>

runtime if statement

 %input{:selected=>if(true), :checked=>if($bool_false), :attribute=>if(!$bool_false)}

rendered

<input <?php echo ((true)? "selected" :"") ;?> <?php echo (($bool_false)? "checked" :"") ;?> <?php echo ((!$bool_false)? "attribute" :"") ;?>>

see 05_selected_attribute.test in test/fixtures/environment directory

allowed to mix classes

%i.a.b{:class=>['c','d']}
%i.a.b{:class=>['c','d', true ? 'e': 'f', false ? 'g':'h']}

rendered

<i class="a b <?php echo( implode(' ',array('c','d'))) ;?>"></i>
<i class="a b <?php echo( implode(' ',array('c','d', true ? 'e': 'f', false ? 'g':'h'))) ;?>"></i>

includes

You can include or require haml partials

-#file partial.haml
%p
    %i Included

-#file main.haml
.posts
	@@inlude partial.haml

this is same as

.posts
    %p
        %i Included
    

use @@inlude /path/file to inlude or @@require /path/file to require files. if file not found, require will throw error and stop processing, include will not.

##new tag :haml This section used to manage Run-time settings of compiler. You can change mostly any behavior or haml-parser and render options.

for :haml section YAML syntax used. Symfony YAML Component used to parse configs.

imports

You an use imports: directive to include config files;

file config-1.yaml :

enable_escaper: false
shortcut:
        '?':
            tag: input
            attr: type
includes: ./config-2.yaml

file config-2.yaml:

shortcut:
        '@':
            attr: [role,data-role]

for more info about shortcut: directive see below.

:haml
    includes: ./config-1.yaml
?text.a(value="#{$name}")
%a.cls@admin some text

render

<input class="a" value="<?php echo($name) ;?>" type="text">
<a class="cls" role="admin" data-role="admin">some text</a>

use includes_dir option to set relative path to include configs.

new MtHamlPHP\Environment('php', array('includes_dir' => dirname(__FILE__)));

compiler settings

You can set or change MtHaml Environment options:

%i.a.b{:class=>['c',$e]}
:haml
  escape_attrs : true
%i.a.b{:class=>['c',$e]}

rendered

<i class="a b <?php echo( implode(' ',array('c',$e))) ;?>"></i>
<i class="a b <?php echo ( htmlspecialchars( implode(' ',array('c',$d)),ENT_QUOTES,"UTF-8")) ;?>"></i>

Shortcuts

this inspired by Slim Shortcuts

define shortcut to render tag with attribute:

:haml
    shortcut:
        '?':
            tag : input
            attr : type
?text.a

rendered

<input class="a" type="text">

You can use shortcuts to render attributes of any tags:

:haml
    shortcut:
        '@':
            attr: [class,role,data-role]
%a.cls@admin

rednder

<a class="cls admin" role="admin" data-role="admin"></a>

You can not use PHP code in shortcuts

###Custom helper functions you can use own functions to render attributes And yes, you can define them in :haml section #####common syntax is

helpers:
  'i' :         #<i> tag only
    class       : <?php /* %1$s.%2$s */ echo render_array('%1$s','%2$s',%3$s) ?>    # class attribute of tag <i>
    id          : <?php /* %1$s.%2$s */ echo render_array('%1$s','%2$s',%3$s) ?>    # id attribute of tag <i>
    custom_attr : <?php /* %1$s.%2$s */ echo custom_attr('%1$s','%2$s',%3$s) ?>     # attribute named "custom_attr" of tag <i>
    
  '*':          #all tags          
    class       : <?php /* %1$s.%2$s */ echo all_class('%1$s','%2$s',%3$s) ?>    
    id          : <?php /* %1$s.%2$s */ echo all_id('%1$s','%2$s',%3$s) ?>
    data-       : <?php /* %1$s.%2$s */ echo render_data('%1$s','%2$s',%3$s) ?>
    '*'         : <?php /* %1$s.%2$s */ echo all_attr('%1$s','%2$s',%3$s) ?>        #all attributes of all tags
  a:            #<a> tag only
    '*'         : <?php /* %1$s.%2$s */ echo all_attr('%1$s','%2$s',%3$s) ?>        #all attributes of tag <a>

The order of lookup - tag.attribute, tag.*, *.attribute, *.*

custom helper (renderer) implemented similar to:

echo sprintf('string_with_function_call',$tag_name,$attribte_name,$attribute_value) 

for example:

:php
  function render_array($tag,$attr,$arr){
      $fl=array() ;
      array_walk_recursive(
        $arr
        , function($i,$k) use (&$fl)
            {
	            $fl[]=$i;
            }
      );
      echo $attr.'="'.implode(' ',$fl).'"';
   }
:haml
  helpers:
    i :
        class: <?php echo render_array('%1$s','%2$s',%3$s) ?>
%i.a.b{class=>['c','d']} text

rendered to

<i <?php echo render_array('i','class',array('a','b',['c','d'])) ?> >text</i>

and executed to

<i class="a b c d" >text</i>

####custom helpers used only for interpolated (parsed) attributes

%tag.a.b will not use helpers to render class attribute

%tag.a{:class=>[$c,$d]} will use custom helper

runtme engine selection

use_runtime - if true, compiler will use standart runtime

see MtHaml documentation for more info of runtime usage;

:haml
    use_runtime=>true
#div{:class => array($position,$item2['type'], $item2['urgency']) }
:haml
    use_runtime=>false
#div{:class => array($position,$item2['type'], $item2['urgency']) }

render

<div <?php echo MtHaml\Runtime::renderAttributes(array(array('id', 'div'), array('class', (array($position,$item2['type'], $item2['urgency'])))), 'html5', 'UTF-8', false); ?>></div>
<div id="div" <?php echo( implode(' ',array($position,$item2['type'], $item2['urgency']))) ;?>></div>

#####see 06_Custom_helper.test in test/fixtures/environment directory for more custom helpers examples

##added input type you can use any type as :type_value for type="type_value" after input tag:

%input:text.cls
%input:submit#id(value="valu")
%input:text#id(class="cls" type="search")

rendered

<input class="cls" type="text">
<input id="id" value="valu" type="submit">
<input id="id" class="cls" type="text">

no runtime for data attributes

see 06_Custom_helper.test in test/fixtures/environment directory

##all credits to Arnaud Le Blanc and scil