module ECR

Overview

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)
  end
  ecr_file "greeting.ecr"
end

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

Greeting.new("John")
#=> Greeting, John!

Using logical statements:

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

Greeting.new(nil)
#=> Greeting!

Using loops:

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

# greeting.ecr
<% @names.each do |name| %>
  Hi, <%= name %>!
<% end %>

Greeting.new("John","Zoe","Ben")
#=> Hi, John!
#=> Hi, Zoe!
#=> Hi, Ben!

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

Extended Modules

Defined in:

ecr/ecr.cr

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]