• Stars
    star
    187
  • Rank 201,516 (Top 5 %)
  • Language
    Ruby
  • License
    MIT License
  • Created over 9 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Faster implementation of Haml template language

Faml

Gem Version Build Status Coverage Status Code Climate

Faster implementation of Haml template language.

Installation

Add this line to your application's Gemfile:

gem 'faml'

And then execute:

$ bundle

Or install it yourself as:

$ gem install faml

Usage

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

# 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 <span foo-bar='baz'></span> . With faml, it's rendered as <span foo='{:bar=&gt;&quot;baz&quot;}'></span> .

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.

%div
  %div hello

is always rendered as

<div>
<div>hello</div>
</div>

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.

%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

More Repositories

1

switch_point

Switching database connection between readonly one and writable one
Ruby
593
star
2

ghcmod-vim

Happy Haskell programming on Vim, powered by ghc-mod
Vim Script
434
star
3

neco-ghc

A completion plugin for Haskell, using ghc-mod
Vim Script
363
star
4

hako

Deploy Docker container
Ruby
276
star
5

project-euler-c

solutions for Project Euler in C
C
60
star
6

android-repository-history

History of Android repository XML
Rust
29
star
7

guzuta

Custom repository manager for ArchLinux pacman
Rust
27
star
8

eagletmt-recutils

My recutils
Rust
27
star
9

misc

ใ„ใ‚ใ„ใ‚
Rust
24
star
10

comic_walker

Client library for ComicWalker
Ruby
23
star
11

aws-lambda-rie-gateway

Convert HTTP request to API Gateway payload for aws-lambda-rie
Rust
15
star
12

libssh-ruby

Ruby binding for libssh
C
14
star
13

envop

Set environment variables from 1Password Secure Notes
Rust
13
star
14

haml_parser

Parser of Haml template language
Ruby
12
star
15

unite-haddock

unite.vim source for haddock
Vim Script
12
star
16

coqtop-vim

Interact with coqtop within Vim
Vim Script
11
star
17

slack-gtk

Native desktop client for slack.com
C++
10
star
18

mitamae-secrets

itamae-secrets for mitamae
Ruby
9
star
19

PKGBUILDs

my PKGBUILDs for Arch Linux
Shell
8
star
20

slack-thread-expander

Expand threaded messages without "Also sent to the channel"
Rust
7
star
21

onlinejudge-vim

enjoy online judges with Vim
Vim Script
6
star
22

kaede

Scheduler for recpt1 recorder using Syoboi Calendar
Ruby
5
star
23

ghci-vim

interact with GHCi
Vim Script
5
star
24

dotfiles

my dotfiles
Vim Script
5
star
25

assert_trait

Macro for static assert that values implements traits.
Rust
5
star
26

barbeque_client-rs

Barbeque client for Rust
Rust
4
star
27

contests

Programming contest is fun
C++
3
star
28

idolmap

Rust
3
star
29

akabei

Custom repository manager for ArchLinux pacman
Ruby
3
star
30

mruby-etc

etc module for mruby
C
3
star
31

django-runner

Run arbitrary Python script within Django environment
Python
3
star
32

dump-tf-schema

Dump schema of Terraform providers
Rust
3
star
33

activerecord-attribute_converter

Transparent conversion for ActiveRecord
Ruby
3
star
34

s3_assets_uploader

Upload Rails assets to S3
Ruby
3
star
35

faraday_middleware-cookiejar

Faraday middleware for managing cookies
Ruby
2
star
36

arch.wanko.cc

http://arch.wanko.cc
Rust
2
star
37

ttcoder

TokyoTechCoder
Ruby
2
star
38

revision_plate-golang

Serve application's REVISION
Go
2
star
39

setup

Ruby
2
star
40

poj-vim

Vim script for POJ
Vim Script
1
star
41

denv-rust

Rust
1
star
42

mitamae-pacman

Provide MItamae::InlineBackends::PacmanBackend for faster package management
C
1
star
43

procon

My submissions
C++
1
star
44

dvb-recpt1

recpt1 for earth-pt1 driver
C++
1
star
45

btrfs-usage

Show detailed btrfs usage in machine-readable format
C
1
star
46

eagletmt.github.com

1
star
47

uim-skk-server-completion

uim-skk ใง server completion ใงใใ‚‹ใ‚ˆใ†ใซใ™ใ‚‹
1
star
48

skkserv-rust

Rust
1
star
49

xz-rust

Pure Rust implementation of reading/writing xz files (WIP)
Rust
1
star
50

fastladder-bookwalker

Rust
1
star
51

hako-etcenv

Provide variables from etcenv to hako
Ruby
1
star
52

sentry-notify-hipchat

A Sentry notification plugin for HipChat
Python
1
star
53

sentry-breakdown

Render Sentry project breakdown of usage report (WIP)
Rust
1
star
54

rundeck-simple-slack-notification

Groovy
1
star
55

ruby-minisat

Ruby binding for MiniSat
Ruby
1
star
56

fastladder-pixiv

Rust
1
star
57

tower-cookie-store

Cookie session store for Tower and Axum
Rust
1
star
58

bf-interp

brainf*ck interpreter written in Haskell
Haskell
1
star
59

hako-vault

Provide variables from Vault to hako
Ruby
1
star
60

project-euler-haskell

solutions for Project Euler in Haskell
Haskell
1
star