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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Tue, Aug 27, 2013 at 11:36 AM, Sébastien Durand <lists@ruby-forum.com>wrote:

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).

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Eagle eye !

:slight_smile:

Thanks Robert ! I will fix that.

···

--
Posted via http://www.ruby-forum.com/.

See the new code here :

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

···

--
Posted via http://www.ruby-forum.com/.

You have to know that exists others alternatives.
I prefer haml, is very powerful and fast.

···

--
Posted via http://www.ruby-forum.com/.

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

···

--
Posted via http://www.ruby-forum.com/.

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

···

On Tue, Aug 27, 2013 at 7:00 AM, Hari C. <lists@ruby-forum.com> wrote:

I prefer haml, is very powerful and fast.
GitHub - haml/haml: HTML Abstraction Markup Language - A Markup Haiku

--
Tony Arcieri

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

···

On Tue, Aug 27, 2013 at 9:00 AM, Hari C. <lists@ruby-forum.com> wrote:

You have to know that exists others alternatives.
I prefer haml, is very powerful and fast.
GitHub - haml/haml: HTML Abstraction Markup Language - A Markup Haiku

--
Posted via http://www.ruby-forum.com/\.

Hi,

I prefer haml, is very powerful and fast.
GitHub - haml/haml: HTML Abstraction Markup Language - A Markup Haiku

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

Refactoring if internals to not flush inline caches at runtime by dbussink · Pull Request #615 · haml/haml · GitHub

--
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

···

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:

--
The wise man said: "Never argue with an idiot. They bring you down to their level and beat you with experience."