Issue #13839 has been updated by k0kubun (Takashi Kokubun).

Status changed from Open to Feedback

You wrote:

~~~
{% if true %}
  Hello #{'World'}
{% end %}

Template.new('...').render(binding)
~~~

You should write expected code in valid Ruby syntax. Guessing from "%{3.times do}Hello #{'World'}%{end}", probably you intended:

~~~
<<EOS
{% if true %}
  Hello #{'World'}
{% end %}
EOS
~~~

or just double-quoted one of it.

> 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. 
>
> But I don't know really how you guys feel about that, I am not a language designer. :)

I'm not a language designer too and probably this is Matz matter, but my personal feeling is that PHP's syntax is originally kind of template but Ruby is not. So templating is not Ruby language's responsibility and such work should be done by not syntax but a template engine as ERB has been kept as library.

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

ERB is not "external library" and I recommend you to use standard library ERB if you want standard way for "trivial templating".
Disclaimer: I'm maintaining Haml and ERB.

----------------------------------------
Feature #13839: String Interpolation Statements
https://bugs.ruby-lang.org/issues/13839#change-66285

* Author: se8 (Sbastien Durand)
* Status: Feedback
* 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>