百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
F

faml

> 编程语言
开源

更快地实现 Haml 模板语言

186 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

更快地实现 Haml 模板语言

Faml

Faster implementation of Haml template language.

Installation

Add this line to your application's Gemfile:

ruby
gem 'faml'

And then execute:

$ bundle

Or install it yourself as:

$ gem install faml

Usage

You can set several options via Faml::Engine.options .

ruby
# Render in XHTML format
Faml::Engine.options[:format] = :xhtml
# Disable autoclose
Faml::Engine.options[:autoclose] = []
# Disable auto preserve
Faml::Engine.options[:preserve] = []

Rails, sinatra

Just replace your gem 'haml' with gem 'faml' .

faml is only tested with Rails >= 4.0.

middleman

Since middleman has its own renderers, there's no easy way to use faml with middleman currently.

Others

faml provide a tilt interface. If your framework uses tilt, you can use faml by replacing gem 'haml' with gem 'faml' .

Incompatibilities

There are several incompatibilities.

See incompatibilities for details generated by spec files.

Hash attributes

Hash attributes are only supported to "data" attributes.

With original haml, %span{foo: {bar: 'baz'}} is rendered as . With faml, it's rendered as .

Only "data" attributes are converted to hyphenated attributes.

HTML-escape by default

Even with non-Rails project, all string are HTML-escaped.

"ugly" mode only

Only "ugly" mode in original haml is supported.

haml
%div
  %div hello

is always rendered as

xml

It's equivalent to haml's "ugly" mode.

No Haml::Helpers by default

I won't provide helper methods of Haml::Helpers. If you really need helper methods, please open an issue.

Only preserve helper method is supported by engine option. You have to set Faml::Engine.options[:extend_helpers] = true to use preserve .

Others

If you find other incompatibility, please report it to me :-p.

Why is faml faster?

Temple backend

I use temple to achieve faster template rendering. It's used by slim template language & engine which is known as fast.

  1. HamlParser::Parser converts source language (Haml template) to own AST (HamlParser::Ast) .
    • You can see the HamlParser::Ast by running haml_parser template.haml .
  2. Faml::Compiler compiles HamlParser::Ast into Temple AST.
    • You can see the Temple AST by running faml temple template.haml .
  3. Temple compiles its AST into Ruby code.
    • You can see the Ruby code by running faml compile template.haml .
    • During this process, several optimizations are performed such as Temple::Filters::MultiFlattener and Temple::Filters::StaticMerger.

Attribute optimization

Although Haml allows arbitrary Ruby hash in the attribute syntax, most attributes are written in hash literal form. All keys are string or symbol literals (i.e., not dynamic values) in typical case.

haml
%div{class: some_helper_method(current_user)}
  %a{href: foo_path(@item)}= @item.name

There is an optimization chance if we could know the value is String. I introduced incompatibility to expand the chance: all attribute values are converted to String by #to_s except for id, class and data . This will enable us to avoid runtime expensive hash merging and rendering. The runtime hash merging is implemented by C extension in faml.

Internally, attributes are categolized into three types.

  1. Static attributes
    • Both the key and the value are static.
    • Compiled into string literals.
    • Fastest.
    • e.g. %input{checked: false}
  2. Dynamic attributes
    • The key is static but the value isn't.
    • The key is compiled into string literal. The value is interpolated at run-time.
    • Relatively fast.
    • e.g. %input{checked: helper_method(@record)}
  3. Runtime attributes
    • Both the key and the value are non-static expression.
    • The attributes are stringified at runtime.
    • Slow.
    • e.g. %input{helper_method(@record)}

Contributing

  1. Fork it ( https://github.com/eagletmt/faml/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

Ruby

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类编程语言
定价开源

> 相关工具

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言