zaitsev / mthamlphp
PHP版的HAML
Requires
- php: >=5.3.0
- mthaml/mthaml: *
- symfony/yaml: ~2.6
Requires (Dev)
- cebe/markdown: ~1
- coffeescript/coffeescript: ~1
- erusev/parsedown: *
- fluxbb/commonmark: ~1@dev
- gregwar/rst: ~1
- kzykhys/ciconia: ~1
- leafo/lessphp: *
- leafo/scssphp: *
- league/commonmark: >=0.5
- michelf/php-markdown: ~1.3
- oyejorge/less.php: *
- phpunit/phpunit: 4.3.5
- twig/twig: ~1.11
Conflicts
- mthaml/mthaml-bundle: <1.1.0
This package is not auto-updated.
Last update: 2024-09-28 17:43:44 UTC
README
MtHaml是一种PHP实现,用于HAML语言,可以针对多种语言。这是基于MtHaml和MtHaml-more(如果需要,支持所有运行时)的分支。此分支的主要目标——在IDE中编译haml文件到PHP(以不使用任何运行时)。
差异
此实现基于并支持MtHaml(MtHaml)的所有功能,并增加了许多新的功能。但主要专注于将haml文件编译为纯PHP,而不是用作模板引擎。
您可以使用grunt任务和IDEA插件在IDE中编辑haml文件时编译haml到PHP。
有关基本用法,请参阅MtHaml和MtHaml-more的文档。
支持IDE高亮显示
为了使IDE识别:php部分中的PHP代码,您可以使用以下方式
:php <?
简化数组定义
`['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)}
```
属性混合
%i.a.b{:class=>['c',$e]}
渲染
<i class="a b <?php echo( implode(' ',array('c',$e))) ;?>"></i>
带有前缀'_'的id属性
%i#id{:id=>[$c,'2']}
渲染
<i id="id_<?php echo( implode('_',array($c,'2'))) ;?>"></i>
高级数据属性
%i{:data=>['a'=>'a','b'=>$c, 'e'=> true ? $c : $e]} %i(data-a="#{$c}" data="#{array($e=>$c)}") %input{:data=>$data_array}
渲染
<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\" ";} ?>>
运行时if语句
%input{:selected=>if(true), :checked=>if($bool_false), :attribute=>if(!$bool_false)}
渲染
<input <?php echo ((true)? "selected" :"") ;?> <?php echo (($bool_false)? "checked" :"") ;?> <?php echo ((!$bool_false)? "attribute" :"") ;?>>
请参阅test/fixtures/environment目录中的05_selected_attribute.test
允许混合类
%i.a.b{:class=>['c','d']} %i.a.b{:class=>['c','d', true ? 'e': 'f', false ? 'g':'h']}
渲染
<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>
包含
您可以将或要求haml部分
-#file partial.haml %p %i Included -#file main.haml .posts @@inlude partial.haml
这与此相同
.posts %p %i Included
使用@@inlude /path/file
包含文件或使用@@require /path/file
要求文件。如果文件不存在,require
将抛出错误并停止处理,而include
则不会。
##新的标签:haml 此部分用于管理编译器的运行时设置。您可以更改大多数haml解析器和渲染选项的行为。
对于:haml
部分,使用YAML语法。 使用Symfony YAML组件解析配置。
导入
您可以使用imports:
指令来包含配置文件;
文件config-1.yaml
enable_escaper: false shortcut: '?': tag: input attr: type includes: ./config-2.yaml
文件config-2.yaml
shortcut: '@': attr: [role,data-role]
有关shortcut:
指令的更多信息,请参阅以下内容。
:haml includes: ./config-1.yaml ?text.a(value="#{$name}") %a.cls@admin some text
渲染
<input class="a" value="<?php echo($name) ;?>" type="text"> <a class="cls" role="admin" data-role="admin">some text</a>
使用includes_dir
选项设置包含配置的相对路径。
new MtHamlPHP\Environment('php', array('includes_dir' => dirname(__FILE__)));
编译器设置
您可以设置或更改MtHaml环境选项
%i.a.b{:class=>['c',$e]} :haml escape_attrs : true %i.a.b{:class=>['c',$e]}
渲染
<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>
快捷方式
这受Slim快捷方式的启发
定义用于渲染具有属性的标签的快捷方式
:haml shortcut: '?': tag : input attr : type ?text.a
渲染
<input class="a" type="text">
您可以使用快捷方式来渲染任何标签的属性
:haml shortcut: '@': attr: [class,role,data-role] %a.cls@admin
渲染
<a class="cls admin" role="admin" data-role="admin"></a>
您不能在快捷方式中使用PHP代码
###自定义辅助函数您可以使用自己的函数来渲染属性,并且是的,您可以在:haml
部分中定义它们#####常见语法是
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>
查找顺序 - tag.attribute
,tag.*
,*.attribute
,*.*
自定义辅助(渲染器)实现方式类似于
echo sprintf('string_with_function_call',$tag_name,$attribte_name,$attribute_value)
例如
: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
渲染为
<i <?php echo render_array('i','class',array('a','b',['c','d'])) ?> >text</i>
并执行为
<i class="a b c d" >text</i>
####自定义辅助函数仅用于插值(解析)属性
%tag.a.b
不会使用辅助函数来渲染class属性
%tag.a{:class=>[$c,$d]}
会使用自定义辅助函数
运行时引擎选择
use_runtime
- 如果为true,则编译器将使用标准运行时
有关运行时使用的更多信息,请参阅MtHaml文档;
:haml use_runtime=>true #div{:class => array($position,$item2['type'], $item2['urgency']) } :haml use_runtime=>false #div{:class => array($position,$item2['type'], $item2['urgency']) }
渲染
<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>
#####请参阅test/fixtures/environment目录中的06_Custom_helper.test以获取更多自定义辅助函数示例
##添加输入类型,您可以使用任何类型,如::type_value,在输入标签后添加 type="type_value"
%input:text.cls %input:submit#id(value="valu") %input:text#id(class="cls" type="search")
渲染
<input class="cls" type="text">
<input id="id" value="valu" type="submit">
<input id="id" class="cls" type="text">
数据属性没有运行时
请查看测试目录中的 test/fixtures/environment 目录下的 06_Custom_helper.test
##所有功劳归功于 Arnaud Le Blanc 和 scil