2017 © Pedro Peláez
 

library mthaml-more

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

image

scil/mthaml-more

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  • Monday, January 19, 2015
  • by scil
  • Repository
  • 1 Watchers
  • 2 Stars
  • 170 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 13 Versions
  • 0 % Grown

The README.md

MtHamlMore

Add some features like snippet to MtHaml, main purpose is “Don't Reinvent the Wheel"., (*1)

Both php and Twig are supported., (*2)

Install method: add "scil/mthaml-more": "*" to composer.json, see details at docs., (*3)

Main Feature :snip

if there is a box structure which is used many times, you can define it as a snip,for example, (*4)

$box=<<<S
.title
    @@@
.body
    @@@
S;

write haml like so:, (*5)

@box
    _ this is title
    _ this is content

output html :, (*6)

<div class="title">
  this is title
</div>
<div class="body">
  this is content
</div>

'@@@' is a placeholder where you can put your own content,and you could define default value for it, or even set global placeholder default value use option 'globalDefaultPlaceholderValue', that's useful if you want all placeholder rendered to empty string when you forget apply placeholder value., (*7)

second example: inline placeholder snips:, (*8)

  $title="%h1 welcome to {@@}.";
  $title2="%h1 this is a placeholder with default value. welcome to {@:MtHamlMore(default)@}.";
  $title3="%h1 this is a named placeholder.welcome to {@name@}.";
  $title4="%h1 welcome to {@name:MtHamlMore(default)@}.";
  $div='.{@@}';

haml:, (*9)

@title Moon
@title2 Moon
@title2
@title3
  _name Moon
@title4
  _name Sun
@title4
@div box

output:, (*10)

<h1>welcome to Moon.</h1>
<h1>this is a placeholder with default value. welcome to Moon.</h1>
<h1>this is a placeholder with default value. welcome to MtHamlMore(default).</h1>
<h1>this is a named placeholder.welcome to Moon.</h1>
<h1>welcome to Sun.</h1>
<h1>welcome to MtHamlMore(default).</h1>
<div class="box"></div>

third example, (*11)

@grid(grid="4 -4 4" fluid="1")
  _ %h1 4
  _ 4 offset 4

This is calling a snip named grid, and two arguments. Usually, I use snip @grid to define grid layout. 'fluid="1"' is fluid layout, 'grid="4 -4 4" is one type of 12 columns grid. What this statement output depends on how your snip writes. In 'examples/snips/php.php', there is an snip which defines Twitter Bootstrop v2 grid. In case of this,output would be, (*12)

<div class="row-fluid show-grid">
    <div class="span4">
      <h1>4</h1>
    </div>
    <div class="span4 offset4">
      4 offset 4
    </div>
</div>

Attribute values can be writed as named placeholdervalue, (*13)

@call(attri="hello")

is equal with, (*14)

@call
  _attri hello

Please note, this form is not treated as attribute value, (*15)

@call
  _attri
    hello

And, (*16)

@call
  _attri |
    hello |
    haml |

equals with, (*17)

@call(attri="hello haml ")

see more examples at : "docs/0. Snip Examples.md", (*18)

Extra Feature 1 : HtmlTag

html tags can be used like haml tag ,not only, (*19)

%div
  <p> hello </p>

which is supported by MtHaml, but also, (*20)

<div>
    %p hello
</div>

This feature enables you to copy any html code into a haml file, only make sure code apply haml indent syntax., (*21)

code: MtHamlMore\Parser::parseHtmlTag, (*22)

Extra Feature 2 : reduce runtime

Sometimes there are some 'MtHaml\Runtime' in php files produced by MtHaml, if you dislike it ,you may try, (*23)

$compiled = compilePhpMoreHaml(
    file_get_contents($hamlfile),
    array( ),
    array(
        'filename'=>$hamlfile,
        'reduce_runtime' => true,
));

'reduce_runtime'=>true could reduce the appearance of 'MtHaml\Runtime',or replace it with \MtHamlMoreRuntime\Runtime::renderAttribute which is much simpler., (*24)

It's not perfect,but works in normal situation., (*25)

Works well for these haml:, (*26)

%input(selected)

%input(selected=true)

%a(title="title" href=$href) Stuff

%script{:type => "text/javascript", :src => "javascripts/script_#{2 + 7}"}

%span.ok(class="widget_#{$widget['number']}")

.item{:class => $item['is_empty'] ? "empty":null}

%div.add{:class => [$item['type'], $item == $sortcol ? ['sort', $sortdir]:null] } Contents

#div{:class => array($item['type'], $item['urgency']), :id => array($item['type'], $item['number']>3?'big' :'small') }

%a{:data => array('author_id' => $data_id,'abc'=>array('ok'=>3,'no'=>$data_id+1))}


Not works, (*27)

  1. there is 'AttributeInterpolation' or 'AttributeList' in php files produced by MtHaml. I have not encounter this so far., (*28)

  2. (welcome add your find), (*29)

This feature supported only for php ,not Twig., (*30)

code: * MtHamlMore\NodeVisitor\PhpRenderer::renderDynamicAttributes * MtHamlMoreRuntime\Runtime::renderAttribute, (*31)

option 'reduce_runtime_array_tolerant'

The :class and :id attributes can be specified as a Ruby array, like, (*32)

#div{:class => array($position,$item2['type'], $item2['urgency']), :id => array($item2['type'], $item2['number']>3?'big' :'small') }

if no one of $position, $item2['type'], $item2['urgency'] or $item2['type'] is an array, you could add 'reduce_runtime_array_tolerant'=>true, to 3rd argument of compilePhpMoreHaml. Then array flatten is not needed in this case., (*33)

code: MtHamlMore\NodeVisitor\PhpRenderer::returnJoinedValueForClassId, (*34)

when option 'reduce_runtime_array_tolerant' is true , only these situations will use array flatten right now:, (*35)

  • if or else is an array in 'condition?if:else',like
%div.add{:class => array($item['type'], $item == $sortcol ? array('sort', $sortdir):null) } Contents
  • (add your needs in : MtHamlMore\NodeVisitor\PhpRenderer::maybeArrayReturnedNode)

Extra Feature 3 : prepare

if you set options 'prepare'=>true , MtHamlMore will first execute php code defined by {% %} and {= =}., (*36)

like this:, (*37)

{% $address='http://program-think.blogspot.com';$name='scil' %}
%div my name is {= $name =}, i love this IT blog {= $address =} which is blocked by GFW

executed to, (*38)

<?php $address='http://program-think.blogspot.com';$name='scil' ; ?>
%div my name is <?php echo $name ; ?>, i love this IT blog <?php echo $address ; ?> which is blocked by GFW

then, to, (*39)

%div my name is scil, i love this IT blog http://program-think.blogspot.com which is blocked by GFW

this is normal haml code,which will be compiled to, (*40)



my name is scil, i love this it blog http://program-think.blogspot.com which is blocked by GFW

notice: {% .. %} must monopolize one line, because regular expression uses '^' and '$'., (*41)

code: MtHamlMore\Environment::prepare, (*42)

The Versions

19/01 2015

dev-master

9999999-dev https://github.com/scil/MtHamlMore

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml mthaml snippet

12/10 2014

v0.7

0.7.0.0 https://github.com/scil/MtHamlMore

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml mthaml snippet

09/10 2014

v0.6.1

0.6.1.0 https://github.com/scil/MtHamlMore

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml mthaml snippet

09/10 2014

v0.6

0.6.0.0 https://github.com/scil/MtHamlMore

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml mthaml snippet

09/10 2014

v0.5.4

0.5.4.0 https://github.com/scil/MtHamlMore

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml mthaml snippet

09/10 2014

v0.5.3

0.5.3.0 https://github.com/scil/MtHamlMore

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml mthaml snippet

18/12 2013

v0.5.2

0.5.2.0 https://github.com/scil/MtHamlMore

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml mthaml snippet

17/12 2013

v0.5.1

0.5.1.0 https://github.com/scil/MtHamlMore

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml mthaml snippet

17/12 2013

v0.5

0.5.0.0 https://github.com/scil/MtHamlMore

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml mthaml snippet

08/12 2013

v0.4

0.4.0.0 https://github.com/scil/MtHamlMore

Add more features like snippet to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml snippet

01/11 2013

v0.3

0.3.0.0 https://github.com/scil/MtHamlSnip

Add Snip function to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml

04/09 2013

v0.2

0.2.0.0 https://github.com/scil/MtHamlSnip

Add Snip function to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml

21/08 2013

v0.1

0.1.0.0 https://github.com/scil/MtHamlSnip

Add Snip function to MtHaml, one way to "Don't Reinvent the Wheel"

  Sources   Download

MIT

The Requires

 

by Avatar scil

haml