Issue #13839 has been updated by se8 (Sbastien Durand).
Thanks for your feedback Jeremy. :) (I have much gratitude for all your work in Ruby!)
Yes, I didn't think of this as a replacement for a full featured template engine (with cache, helpers and so), but more as a quick and powerful way to embed Ruby in various documents.
I was listening a talk by Rasmus Lerdorf, the creator of PHP. He said that one of the most appealing feature he added to PHP was to include trivial templating out of the box. And it's true, in Ruby or Python, it's harder to get started, you have to require an external library and you are overwhelmed by all these competing choices: Haml ? ERB (which variant?)
But I don't know really how you guys feel about that, I am not a language designer. :)
By the way Nao submitted a patch for improving string interpolation performance:
https://bugs.ruby-lang.org/issues/13587
Good idea on replacing += with <<, it really improved the code speed for large strings:
~~~
Warming up --------------------------------------
Template-small 8.929k i/100ms
Erubi-small 3.866k i/100ms
Calculating -------------------------------------
Template-small 91.792k (¡Þ 0.9%) i/s - 464.308k in 5.058681s
Erubi-small 39.139k (¡Þ 2.4%) i/s - 197.166k in 5.040678s
Comparison:
Template-small: 91792.2 i/s
Erubi-small: 39139.1 i/s - 2.35x slower
Warming up --------------------------------------
Template-small-cache 9.166k i/100ms
Erubi-small-cache 8.856k i/100ms
Calculating -------------------------------------
Template-small-cache 94.974k (¡Þ 1.6%) i/s - 476.632k in 5.019886s
Erubi-small-cache 91.008k (¡Þ 0.7%) i/s - 460.512k in 5.060385s
Comparison:
Template-small-cache: 94973.6 i/s
Erubi-small-cache: 91008.5 i/s - 1.04x slower
Warming up --------------------------------------
Template-large 1.000 i/100ms
Erubi-large 1.000 i/100ms
Calculating -------------------------------------
Template-large 19.171 (¡Þ 5.2%) i/s - 96.000 in 5.023298s
Erubi-large 2.366 (¡Þ 0.0%) i/s - 12.000 in 5.075102s
Comparison:
Template-large: 19.2 i/s
Erubi-large: 2.4 i/s - 8.10x slower
Warming up --------------------------------------
Template-large-cache 1.000 i/100ms
Erubi-large-cache 1.000 i/100ms
Calculating -------------------------------------
Template-large-cache 19.560 (¡Þ10.2%) i/s - 97.000 in 5.014184s
Erubi-large-cache 16.434 (¡Þ12.2%) i/s - 81.000 in 5.040727s
Comparison:
Template-large-cache: 19.6 i/s
Erubi-large-cache: 16.4 i/s - same-ish: difference falls within error
~~~
----------------------------------------
Feature #13839: String Interpolation Statements
https://bugs.ruby-lang.org/issues/13839#change-66284
* Author: se8 (Sbastien Durand)
* Status: Open
* Priority: Normal
* Assignee:
* Target version:
----------------------------------------
Hello!
Here is a KISS implementation of a template engine in Ruby:
~~~ ruby
class Template
attr_reader :input
def initialize(input)
@input = input
end
def output
"output = %\0" + @input.gsub("{%", "\0\n").gsub("%}", "\noutput += %\0") + "\0"
end
def render(binding)
eval(output, binding)
end
end
~~~
Usage:
~~~ text
{% if true %}
Hello #{'World'}
{% end %}
Template.new('...').render(binding)
~~~
It's kind of a hack on top of Ruby string interpolation, so it's hell fast (~4 times faster than ERB).
Could it be a good idea to implement this kind of statements directly in Ruby string interpolation? Maybe a syntax like that:
~~~ text
"%{3.times do}Hello #{'World'}%{end}"
~~~
So Ruby would have a fast minimal native template engine, with #{expressions} and %{statements}:
~~~ text
eval(File.read("..."), binding)
~~~
--
https://bugs.ruby-lang.org/
Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>