Forum: Ruby A minimal alternative to ERB !

56b434ec98414dd9422166b06de1305d?d=identicon&s=25 Sébastien D. (se8)
on 2013-08-27 11:36
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (robert_k78)
on 2013-08-27 11:42
(Received via mailing list)
On Tue, Aug 27, 2013 at 11:36 AM, Sbastien Durand
<lists@ruby-forum.com>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
56b434ec98414dd9422166b06de1305d?d=identicon&s=25 Sébastien D. (se8)
on 2013-08-27 11:49
Eagle eye !

:-)

Thanks Robert ! I will fix that.
56b434ec98414dd9422166b06de1305d?d=identicon&s=25 Sébastien D. (se8)
on 2013-08-27 13:47
See the new code here :

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

(Sorry, wrong move, I had to recreate a new gist repository.)
2fe8ed3b97893fc3531a47658615ddae?d=identicon&s=25 Hari C. (hari_c)
on 2013-08-27 16:00
You have to know that exists others alternatives.
I prefer haml, is very powerful and fast.
https://github.com/haml/haml
9a45896e48a382fe5c656b8873e0dfcb?d=identicon&s=25 Stu (Guest)
on 2013-08-27 19:33
(Received via mailing list)
Neat way to grok parsing in ruby! Nice script. Thanks for sharing.
A74a68807619459925cc1d8e1045c7bd?d=identicon&s=25 Tony Arcieri (Guest)
on 2013-08-27 20:47
(Received via mailing list)
On Tue, Aug 27, 2013 at 7:00 AM, Hari C. <lists@ruby-forum.com> 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
A7fca2db6ff3f4583df98043bd5166f9?d=identicon&s=25 Panagiotis Atmatzidis (Guest)
on 2013-08-28 09:06
(Received via mailing list)
Attachment: signature.asc (840 Bytes)
Hi,

On 27 Αυγ 2013, at 21:46 , Tony Arcieri <tony.arcieri@gmail.com> wrote:

> On Tue, Aug 27, 2013 at 7:00 AM, Hari C. <lists@ruby-forum.com> 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 Arcieri

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:  atma@convalesco.org
URL:  http://www.convalesco.org
GnuPG ID: 0x1A7BFEC5
gpg --keyserver pgp.mit.edu --recv-keys 1A7BFEC5
88112c5766888ac86c9b4336c9506fa2?d=identicon&s=25 Mark Maglana (mark_m46)
on 2013-09-01 20:17
That's a neat script, Sébastien! Thanks for sharing. Learned something
new today. :-)
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.