Project

General

Profile

Actions

Feature #13839

closed

String Interpolation Statements

Added by Anonymous over 6 years ago. Updated over 6 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
[ruby-core:82471]

Description

Hello!

Here is a KISS implementation of a template engine in 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:

{% 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:

"%{3.times do}Hello #{'World'}%{end}"

So Ruby would have a fast minimal native template engine, with #{expressions} and %{statements}:

eval(File.read("..."), binding)
Actions

Also available in: Atom PDF

Like0
Like0Like0Like0Like0Like0Like0Like0Like0Like0