module ECR


Embedded Crystal (ECR) is a template language for embedding Crystal code into other text, that includes but is not limited to HTML. The template is read and transformed at compile time and then embedded into the binary.

There are <%= %> and <% %> syntax. The former will render returned values. The latter will not, but instead serve to control the structure as we do in normal Crystal.

Quick Example:

require "ecr"

class Greeting
  def initialize(@name)
  ecr_file "greeting.ecr"

# greeting.ecr
Greeting, <%= @name %>!"John")
#=> Greeting, John!

Using logical statements:

# greeing.ecr
<% if @name %>
  Greeting, <%= @name %>!
<% else %>
<% end %>
#=> Greeting!

Using loops:

class Greeting
  def initialize(*names)
   @names = names
  ecr_file "greeting.ecr"

# greeting.ecr
<% @names.each do |name| %>
  Hi, <%= name %>!
<% end %>"John","Zoe","Ben")
#=> Hi, John!
#=> Hi, Zoe!
#=> Hi, Ben!

Likewise, other Crystal logic can be implemented in ECR text.

Extended Modules

Defined in:


Constant Summary

DefaultBufferName = "__str__"

Instance Method Summary

Instance Method Detail

def process_file(filename, buffer_name = DefaultBufferName) #

[View source]
def process_string(string, filename, buffer_name = DefaultBufferName) #

[View source]