A minimal alternative to ERB!

Hi guys,

I have tried to understand how ERB works and I have designed my own
template engine : {% statement %} and {{ expression }}.

I want to share the code with you :

https://gist.github.com/astraction/6351299

Only 33 lines of code (Ruby is so powerful !) and it should be much
faster than ERB (but I haven’t done any benchmarks).

It’s mainly for learning purpose. Of course, there is much room for
improvement (caching the compiled code, helpers methods, feel free to
help me).

Sébastien

On Tue, Aug 27, 2013 at 11:36 AM, Sbastien D.
[email protected]wrote:

faster than ERB (but I haven’t done any benchmarks).

It’s mainly for learning purpose. Of course, there is much room for
improvement (caching the compiled code, helpers methods, feel free to
help me).

Your regexes are flawed because they are too greedy:

irb(main):001:0> ‘{{a}}{{b}}’.scan(/{%(.+)%}|{{(.+)}}|([^{}]+)/)
=> [[nil, “a}}{{b”, nil]]

You should at least use reluctant quantifier:

irb(main):002:0> ‘{{a}}{{b}}’.scan(/{%(.+?)%}|{{(.+?)}}|([^{}]+?)/)
=> [[nil, “a”, nil], [nil, “b”, nil]]

Cheers

robert

Eagle eye !

:slight_smile:

Thanks Robert ! I will fix that.

See the new code here :

https://gist.github.com/astraction/6352528

(Sorry, wrong move, I had to recreate a new gist repository.)

Neat way to grok parsing in ruby! Nice script. Thanks for sharing.

You have to know that exists others alternatives.
I prefer haml, is very powerful and fast.
https://github.com/haml/haml

On Tue, Aug 27, 2013 at 7:00 AM, Hari C. [email protected] wrote:

I prefer haml, is very powerful and fast.
https://github.com/haml/haml

Haml is anything but fast. It blows your method caches any time you
render
a template:

https://github.com/haml/haml/pull/615

Hi,

On 27 Αυγ 2013, at 21:46 , Tony A. [email protected] wrote:

On Tue, Aug 27, 2013 at 7:00 AM, Hari C. [email protected] wrote:
I prefer haml, is very powerful and fast.
https://github.com/haml/haml

Haml is anything but fast. It blows your method caches any time you render a
template:

https://github.com/haml/haml/pull/615


Tony A.

There are a dozen of similar projects to fast-type HTML:

slim: http://slim-lang.com
less: http://www.lesscss.org
liquid: http://liquidmarkup.org
markaby: http://markaby.github.io

and for CSS most notably:
SASS

Panagiotis (atmosx) Atmatzidis

email: [email protected]
URL: http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5

That’s a neat script, Sébastien! Thanks for sharing. Learned something
new today. :slight_smile: