scil/ mthaml-more
为MtHaml添加更多功能,例如代码片段,这是一种“不要重复造轮子”的方法
Requires
- mthaml/mthaml: >=1.3.0
- nikic/php-parser: 1.*@dev
- phpunit/phpunit-story: 1.*
README
为MtHaml添加一些功能,例如代码片段,主要目的是“不要重复造轮子”。
支持php和Twig。
安装方法:在composer.json中添加"scil/mthaml-more": "*"
,详情请参考文档。
主要功能:snip
如果有经常使用的盒式结构,你可以将其定义为snip,例如
$box=<<<S
.title
@@@
.body
@@@
S;
编写haml如下
@box
_ this is title
_ this is content
输出html
<div class="title">
this is title
</div>
<div class="body">
this is content
</div>
'@@@'是一个占位符,你可以在这里放入自己的内容,你也可以为它定义默认值,甚至可以通过使用选项'globalDefaultPlaceholderValue'设置全局占位符默认值,这在你想让所有占位符渲染为空字符串时非常有用。
第二个示例:行内占位符snip
$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
@title Moon
@title2 Moon
@title2
@title3
_name Moon
@title4
_name Sun
@title4
@div box
输出
<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>
第三个示例
@grid(grid="4 -4 4" fluid="1")
_ %h1 4
_ 4 offset 4
这是调用名为grid的snip和两个参数。通常,我使用snip @grid来定义网格布局。'fluid="1"'是流体布局,'grid="4 -4 4"'是一种12列网格。这个语句的输出取决于你的snip是如何编写的。在'examples/snips/php.php'中有一个定义Twitter Bootstrop v2网格的snip。在这种情况下,输出将是
<div class="row-fluid show-grid">
<div class="span4">
<h1>4</h1>
</div>
<div class="span4 offset4">
4 offset 4
</div>
</div>
属性值可以写成命名占位符值
@call(attri="hello")
等同于
@call
_attri hello
请注意,此表单不视为属性值
@call
_attri
hello
并且
@call
_attri |
hello |
haml |
等于
@call(attri="hello haml ")
请参阅:更多示例: "docs/0. Snip Examples.md"
额外功能1:HtmlTag
html标签可以像haml标签一样使用,不仅
%div
<p> hello </p>
这是MtHaml支持的,还包括
<div>
%p hello
</div>
此功能使您能够将任何html代码复制到haml文件中,只需确保代码适用于haml缩进语法即可。
代码:MtHamlMore\Parser::parseHtmlTag
额外功能2:减少运行时
有时,php文件中会存在一些由MtHaml产生的'MtHaml\Runtime',如果你不喜欢它,你可以尝试
$compiled = compilePhpMoreHaml(
file_get_contents($hamlfile),
array( ),
array(
'filename'=>$hamlfile,
'reduce_runtime' => true,
));
'reduce_runtime'=>true可以减少'MtHaml\Runtime'的出现,或者将其替换为\MtHamlMoreRuntime\Runtime::renderAttribute,这要简单得多。
它不是完美的,但在正常情况下是可行的。
对以下haml工作良好
%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))}
不起作用
-
在MtHaml产生的php文件中有'AttributeInterpolation'或'AttributeList'。到目前为止,我还没有遇到过这种情况。
-
(欢迎添加您的发现)
此功能仅支持php,不支持Twig。
代码
- MtHamlMore\NodeVisitor\PhpRenderer::renderDynamicAttributes
- MtHamlMoreRuntime\Runtime::renderAttribute
选项'reduce_runtime_array_tolerant'
:class和:id属性可以指定为Ruby数组,例如
#div{:class => array($position,$item2['type'], $item2['urgency']), :id => array($item2['type'], $item2['number']>3?'big' :'small') }
如果$position、$item2['type']、$item2['urgency']或$item2['type']中的任何一个不是数组,你可以将 'reduce_runtime_array_tolerant'=>true,
添加到compilePhpMoreHaml的第三个参数中。在这种情况下不需要数组扁平化。
代码:MtHamlMore\NodeVisitor\PhpRenderer::returnJoinedValueForClassId
当选项'reduce_runtime_array_tolerant'为true时,目前只有这些情况会使用数组扁平化
- if或else是'condition?if:else'中的数组,例如
%div.add{:class => array($item['type'], $item == $sortcol ? array('sort', $sortdir):null) } Contents
- (将您的需求添加到:MtHamlMore\NodeVisitor\PhpRenderer::maybeArrayReturnedNode)
额外功能3:准备
如果你设置选项'prepare'=>true,MtHamlMore将首先执行由{% %}和{= =}定义的php代码。
如下
{% $address='http://program-think.blogspot.com';$name='scil' %}
%div my name is {= $name =}, i love this IT blog {= $address =} which is blocked by GFW
执行到
<?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
然后,到
%div my name is scil, i love this IT blog http://program-think.blogspot.com which is blocked by GFW
这是正常的haml代码,它将被编译为
<div>my name is scil, i love this it blog http://program-think.blogspot.com which is blocked by GFW</div>
注意:{% .. %}必须独占一行,因为正则表达式使用'^'和'$'。
代码:MtHamlMore\Environment::prepare