/
ecr.cr
72 lines (71 loc) · 1.64 KB
/
ecr.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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 Crystal.
#
# Using a dash inside `<...>` either eliminates previous indentation or removes the next newline:
#
# * `<%- ... %>`: removes previous indentation
# * `<% ... -%>`: removes next newline
# * `<%-= ... %>`: removes previous indentation
# * `<%= ... -%>`: removes next newline
#
# Quick Example:
#
# require "ecr"
#
# class Greeting
# def initialize(@name : String)
# end
#
# ECR.def_to_s "greeting.ecr"
# end
#
# # greeting.ecr
# Greeting, <%= @name %>!
#
# Greeting.new("John").to_s
# #=> Greeting, John!
#
# Using logical statements:
#
# # greeting.ecr
# <%- if @name -%>
# Greeting, <%= @name %>!
# <%- else -%>
# Greeting!
# <%- end -%>
#
# Greeting.new(nil).to_s
# #=> Greeting!
#
# Using loops:
#
# require "ecr"
#
# class Greeting
# @names : Array(String)
#
# def initialize(*names)
# @names = names.to_a
# end
#
# ECR.def_to_s "greeting.ecr"
# end
#
# # greeting.ecr
# <%- @names.each do |name| -%>
# Hi, <%= name %>!
# <%- end -%>
#
# Greeting.new("John", "Zoe", "Ben").to_s
# #=> Hi, John!
# #=> Hi, Zoe!
# #=> Hi, Ben!
#
# Likewise, other Crystal logic can be implemented in ECR text.
module ECR
end
require "./ecr/macros"