Issue #13839 has been updated by se8 (Sbastien Durand).


Yes templating is not a key factor for large modern PHP projects, but you need to go back in late 90s. That was the start of the web. A lot of people wanted to publish their things, so they wrote HTML pages. Often they knew nothing about programming, but it was so easy to enhance their website with PHP logic here and there. That's how PHP became largely popular and beat Perl for web use, because it was so simple to get started (yes the drawback was beginners doing a mess of spaghetti code). In Perl you had to use a CGI lib and to painfully concat html strings.

A lot of modern PHP frameworks built their own template engines, like this one called "Twig". Can you image that this thing is about 12 000 loc? I think it's overkill, it's like rebuilding a language on top of an other language. Why would you want to reach for a 12 000 loc library when plain old vanilla PHP already does that?

In its core, templating and text manipulation is just about 2 things: expressions and statements. Right now, Ruby only offers #{expression}, but we can't embed %{statement}.

Yes I agree that templating may not be very beautiful visually, because you are mixing two languages like HTML and a backend language, but it's the most straightforward way of enriching documents with programming logic, without having to reinvent a new library (like WML) on top of your stack.

:)

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

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