class Template
class Template
lib/template.tya:2
Template renders small HTML-oriented templates from dictionaries.
Source
# Template renders small HTML-oriented templates from dictionaries.
class Template
# Template.source stores instance state.
# @type Nil
source: nil
# Template.initialize stores template source or path.
# @param source String source value.
# @return Self the initialized object.
initialize: source = nil ->
self.source = source
# Template.close provides the template/Template standard library operation.
# @return Nil the resulting value.
close: ->
Template(nil).rbrace() + Template(nil).rbrace()
# Template.copy_dict provides the template/Template standard library operation.
# @param dict Any dict value.
# @return Dict the resulting value.
copy_dict: dict ->
out = {}
for key in dict.keys()
out[key] = dict[key]
out
# Template.error provides the template/Template standard library operation.
# @param state Any state value.
# @param message String message value.
# @return Any the resulting value.
error: state, message ->
line = 1
col = 1
i = 0
while i < state["pos"]
if state["source"][i] == "\n"
line = line + 1
col = 1
else
col = col + 1
i = i + 1
"template.render:{line}:{col}: {message}"
# Template.escape_html provides the template/Template standard library operation.
# @param text String text value.
# @return Any the resulting value.
escape_html: text ->
out = ""
i = 0
while i < text.byte_len()
c = text[i]
if c == "&"
out = out + "&"
elseif c == "<"
out = out + "<"
else
if c == ">"
out = out + ">"
elseif c == "\""
out = out + """
else
if c == "'"
out = out + "'"
else
out = out + c
i = i + 1
out
# Template.find_block_split provides the template/Template standard library operation.
# @param source String source value.
# @param start Int start value.
# @return Any the resulting value.
find_block_split: source, start ->
pos = start
depth = 0
else_pos = -1
else_end = -1
n = source.byte_len()
while pos < n
open = Template(nil).find_next_tag(source, pos)
if open < 0
raise error("template.render: missing end")
close = Template(nil).find_close(source, open + 2, Template(nil).close())
tag = source.slice(open + 2, close).trim()
if (
Template(nil).starts_text(tag, "if ") or Template(nil).starts_text(tag, "for ")
)
depth = depth + 1
elseif tag == "end"
if depth == 0
return { else: else_pos, else_end: else_end, end: open, end_end: close + 2 }
depth = depth - 1
else
if tag == "else" and depth == 0
else_pos = open
else_end = close + 2
pos = close + 2
raise error("template.render: missing end")
# Template.find_close provides the template/Template standard library operation.
# @param source String source value.
# @param start Int start value.
# @param marker Any marker value.
# @return Any the resulting value.
find_close: source, start, marker ->
i = start
while i + marker.len() <= source.byte_len()
if source.slice(i, i + marker.len()) == marker
return i
i = i + 1
-1
# Template.find_next_tag provides the template/Template standard library operation.
# @param source String source value.
# @param start Int start value.
# @return Any the resulting value.
find_next_tag: source, start ->
i = start
while i + 1 < source.byte_len()
if (
source[i] == Template(nil).lbrace() and source[i + 1] == Template(nil).lbrace()
)
return i
i = i + 1
-1
# Template.is_number provides the template/Template standard library operation.
# @param text String text value.
# @return Boolean the resulting value.
is_number: text ->
if text == ""
return false
i = 0
while i < text.byte_len()
c = ord(text[i])
if c < 48 or c > 57
return false
i = i + 1
true
# Template.lbrace provides the template/Template standard library operation.
# @return Any the resulting value.
lbrace: ->
"{{"
# Template.lookup provides the template/Template standard library operation.
# @param path String path value.
# @param env Any env value.
# @return Any the resulting value.
lookup: path, env ->
p = path.trim()
parts = Template(nil).path_parts(p)
if parts.len() == 0
return { ok: false, value: "" }
first = parts[0]
ok = false
value = nil
locals = env["locals"]
data = env["data"]
options = env["options"]
if locals.has(first)
ok = true
value = locals[first]
elseif data.has(first)
ok = true
value = data[first]
if not ok
if Template(nil).strict(options)
raise error("template.render: missing " + p)
return { ok: false, value: "" }
i = 1
while i < parts.len()
key = parts[i]
if key == ""
return { ok: true, value: value }
if Template(nil).is_number(key)
value = value[key.to_i()]
elseif value.has(key)
value = value[key]
else
if Template(nil).strict(options)
raise error("template.render: missing " + p)
return { ok: false, value: "" }
i = i + 1
{ ok: true, value: value }
# Template.open provides the template/Template standard library operation.
# @return Any the resulting value.
open: ->
Template(nil).lbrace() + Template(nil).lbrace()
# Template.option provides the template/Template standard library operation.
# @param options Dict options value.
# @param name String name value.
# @param default Any default value.
# @return Any the resulting value.
option: options, name, default ->
if options.has(name)
return options[name]
default
# Template.path_parts provides the template/Template standard library operation.
# @param path String path value.
# @return String the resulting value.
path_parts: path ->
parts = []
cur = ""
i = 0
while i < path.byte_len()
c = path[i]
if c == "."
if cur != ""
parts.push(cur)
cur = ""
elseif c == "["
if cur != ""
parts.push(cur)
cur = ""
i = i + 1
idx = ""
while i < path.byte_len() and path[i] != "]"
idx = idx + path[i]
i = i + 1
parts.push(idx)
else
cur = cur + c
i = i + 1
if cur != ""
parts.push(cur)
parts
# Template.raw_close provides the template/Template standard library operation.
# @return Any the resulting value.
raw_close: ->
Template(nil).rbrace() + Template(nil).close()
# Template.raw_open provides the template/Template standard library operation.
# @return Any the resulting value.
raw_open: ->
Template(nil).open() + Template(nil).lbrace()
# Template.rbrace provides the template/Template standard library operation.
# @return Any the resulting value.
rbrace: ->
"}}"
# Template.read_tag provides the template/Template standard library operation.
# @param state Any state value.
# @return Any the resulting value.
read_tag: state ->
state["pos"] = state["pos"] + 2
tag = Template(nil).read_until(state, Template(nil).close())
tag.trim()
# Template.read_until provides the template/Template standard library operation.
# @param state Any state value.
# @param marker Any marker value.
# @return Any the resulting value.
read_until: state, marker ->
src = state["source"]
start = state["pos"]
close = Template(nil).find_close(src, start, marker)
if close < 0
raise error(Template(nil).error(state, "unclosed tag"))
state["pos"] = close + marker.len()
src.slice(start, close)
# render expands source using data and options.
# Values are HTML-escaped unless raw template syntax is used.
# @param source String source value.
# @param data Array data value.
# @param options Dict options value.
# @return Any the resulting value.
render: source, data = nil, options = nil ->
if self.source != nil and data != nil and data.class == Dict
options = data
data = source
source = self.source
if data == nil
data = source
source = self.source
if options == nil
options = {}
Template(nil).render_with_options(source, data, options)
# render_file reads a template file and renders it with data and options.
# Raises when the file cannot be read or the template is invalid.
# @param path String path value.
# @param data Array data value.
# @param options Dict options value.
# @return Any the resulting value.
render_file: path, data = nil, options = nil ->
if self.source != nil and data != nil and data.class == Dict
options = data
data = path
path = self.source
if data == nil
data = path
path = self.source
if options == nil
options = {}
Template(nil).render_with_options(read_file(path), data, options)
# Template.render_for provides the template/Template standard library operation.
# @param state Any state value.
# @param tag Any tag value.
# @param env Any env value.
# @return Any the resulting value.
render_for: state, tag, env ->
parts = tag.split(" ")
if parts.len() != 4 or parts[2] != "in"
raise error(Template(nil).error(state, "invalid for"))
name = parts[1]
path = parts[3]
body_start = state["pos"]
split = Template(nil).find_block_split(state["source"], body_start)
state["pos"] = split["end_end"]
found = Template(nil).lookup(path, env)
if not found["ok"]
return ""
out = ""
for item in found["value"]
child = Template(nil).copy_dict(env["locals"])
child[name] = item
child_env = { data: env["data"], locals: child, options: env["options"] }
out = out
+ Template(nil).render_range(state["source"], body_start, split["end"], child_env)
out
# Template.render_html provides the template/Template standard library operation.
# @param source String source value.
# @param data Array data value.
# @return Any the resulting value.
render_html: source, data = nil ->
if data == nil
data = source
source = self.source
Template(nil).render_with_options(source, data, { escape: "html" })
# Template.render_if provides the template/Template standard library operation.
# @param state Any state value.
# @param tag Any tag value.
# @param env Any env value.
# @return Any the resulting value.
render_if: state, tag, env ->
cond = tag.slice(3, tag.len())
truth = false
found = Template(nil).lookup(cond.trim(), env)
if found["ok"] and found["value"]
truth = true
then_start = state["pos"]
split = Template(nil).find_block_split(state["source"], then_start)
state["pos"] = split["end_end"]
if split["else"] >= 0
if truth
return Template(nil).render_range(state["source"], then_start, split["else"], env)
return Template(nil).render_range(state["source"], split["else_end"], split["end"], env)
if truth
return Template(nil).render_range(state["source"], then_start, split["end"], env)
""
# Template.render_partial provides the template/Template standard library operation.
# @param tag Any tag value.
# @param env Any env value.
# @return Any the resulting value.
render_partial: tag, env ->
rest = tag.slice(8, tag.len()).trim()
if rest.byte_len() < 2 or rest[0] != "\""
raise error("template.render: invalid partial")
i = 1
name = ""
while i < rest.byte_len() and rest[i] != "\""
name = name + rest[i]
i = i + 1
if i >= rest.byte_len()
raise error("template.render: invalid partial")
ctx_path = rest.slice(i + 1, rest.len()).trim()
options = env["options"]
if not options.has("partials")
raise error("template.render: missing partials")
partials = options["partials"]
if not partials.has(name)
raise error("template.render: missing partial " + name)
ctx = env["data"]
if ctx_path != ""
found = Template(nil).lookup(ctx_path, env)
if found["ok"]
ctx = found["value"]
else
ctx = {}
Template(nil).render_with_options(partials[name], ctx, options)
# Template.render_range provides the template/Template standard library operation.
# @param source String source value.
# @param start Int start value.
# @param finish Any finish value.
# @param env Any env value.
# @return Any the resulting value.
render_range: source, start, finish, env ->
part = source.slice(start, finish)
state = { source: part, pos: 0, len: part.byte_len() }
Template(nil).render_until(state, env, "")
# Template.render_raw_tag provides the template/Template standard library operation.
# @param state Any state value.
# @param env Any env value.
# @return Any the resulting value.
render_raw_tag: state, env ->
state["pos"] = state["pos"] + 3
tag = Template(nil).read_until(state, Template(nil).raw_close())
Template(nil).render_value(tag.trim(), env, false)
# Template.render_until provides the template/Template standard library operation.
# @param state Any state value.
# @param env Any env value.
# @param stop Any stop value.
# @return Any the resulting value.
render_until: state, env, stop ->
out = ""
src = state["source"]
while state["pos"] < state["len"]
if stop != "" and Template(nil).starts_tag(state, stop)
return out
if Template(nil).starts(state, Template(nil).raw_open())
out = out + Template(nil).render_raw_tag(state, env)
elseif Template(nil).starts(state, Template(nil).open())
tag = Template(nil).read_tag(state)
if Template(nil).starts_text(tag, "if ")
out = out + Template(nil).render_if(state, tag, env)
elseif Template(nil).starts_text(tag, "for ")
out = out + Template(nil).render_for(state, tag, env)
else
if Template(nil).starts_text(tag, "partial ")
out = out + Template(nil).render_partial(tag, env)
elseif tag == "else" or tag == "end"
raise error(Template(nil).error(state, "unexpected " + tag))
else
out = out + Template(nil).render_value(tag, env, true)
else
out = out + src[state["pos"]]
state["pos"] = state["pos"] + 1
if stop != ""
raise error(Template(nil).error(state, "missing " + stop))
out
# Template.render_value provides the template/Template standard library operation.
# @param path String path value.
# @param env Any env value.
# @param escaped Any escaped value.
# @return Any the resulting value.
render_value: path, env, escaped ->
found = Template(nil).lookup(path, env)
if not found["ok"]
return ""
text = found["value"].to_s()
if (
escaped and Template(nil).option(env["options"], "escape", "none") == "html"
)
return Template(nil).escape_html(text)
text
# Template.render_with_options provides the template/Template standard library operation.
# @param source String source value.
# @param data Array data value.
# @param options Dict options value.
# @return Dict the resulting value.
render_with_options: source, data, options ->
state = { source: source, pos: 0, len: source.byte_len() }
env = { data: data, locals: {}, options: options }
Template(nil).render_until(state, env, "")
# Template.starts provides the template/Template standard library operation.
# @param state Any state value.
# @param text String text value.
# @return Any the resulting value.
starts: state, text ->
src = state["source"]
pos = state["pos"]
if pos + text.byte_len() > state["len"]
return false
i = 0
while i < text.byte_len()
if src[pos + i] != text[i]
return false
i = i + 1
true
# Template.starts_tag provides the template/Template standard library operation.
# @param state Any state value.
# @param tag Any tag value.
# @return Any the resulting value.
starts_tag: state, tag ->
if not Template(nil).starts(state, Template(nil).open())
return false
close = Template(nil).find_close(
state["source"],
state["pos"] + 2,
Template(nil).close()
)
if close < 0
return false
text = state["source"].slice(state["pos"] + 2, close).trim()
text == tag
# Template.starts_text provides the template/Template standard library operation.
# @param text String text value.
# @param prefix Any prefix value.
# @return String the resulting value.
starts_text: text, prefix ->
if prefix.len() > text.byte_len()
return false
text.slice(0, prefix.len()) == prefix
# Template.strict provides the template/Template standard library operation.
# @param options Dict options value.
# @return Any the resulting value.
strict: options ->
options.has("strict") and options["strict"]
Instance Variables
source
Template.source
lib/template.tya:5
Template.source stores instance state.
Source
# Template.source stores instance state.
# @type Nil
source: nil
Methods
close
Template.close()
lib/template.tya:15
Template.close provides the template/Template standard library operation.
Source
# Template.close provides the template/Template standard library operation.
# @return Nil the resulting value.
close: ->
Template(nil).rbrace() + Template(nil).rbrace()
copy_dict
Template.copy_dict(dict)
lib/template.tya:21
Template.copy_dict provides the template/Template standard library operation.
Source
# Template.copy_dict provides the template/Template standard library operation.
# @param dict Any dict value.
# @return Dict the resulting value.
copy_dict: dict ->
out = {}
for key in dict.keys()
out[key] = dict[key]
out
error
Template.error(state, message)
lib/template.tya:31
Template.error provides the template/Template standard library operation.
Source
# Template.error provides the template/Template standard library operation.
# @param state Any state value.
# @param message String message value.
# @return Any the resulting value.
error: state, message ->
line = 1
col = 1
i = 0
while i < state["pos"]
if state["source"][i] == "\n"
line = line + 1
col = 1
else
col = col + 1
i = i + 1
"template.render:{line}:{col}: {message}"
escape_html
Template.escape_html(text)
lib/template.tya:47
Template.escape_html provides the template/Template standard library operation.
Source
# Template.escape_html provides the template/Template standard library operation.
# @param text String text value.
# @return Any the resulting value.
escape_html: text ->
out = ""
i = 0
while i < text.byte_len()
c = text[i]
if c == "&"
out = out + "&"
elseif c == "<"
out = out + "<"
else
if c == ">"
out = out + ">"
elseif c == "\""
out = out + """
else
if c == "'"
out = out + "'"
else
out = out + c
i = i + 1
out
find_block_split
Template.find_block_split(source, start)
lib/template.tya:73
Template.find_block_split provides the template/Template standard library operation.
Source
# Template.find_block_split provides the template/Template standard library operation.
# @param source String source value.
# @param start Int start value.
# @return Any the resulting value.
find_block_split: source, start ->
pos = start
depth = 0
else_pos = -1
else_end = -1
n = source.byte_len()
while pos < n
open = Template(nil).find_next_tag(source, pos)
if open < 0
raise error("template.render: missing end")
close = Template(nil).find_close(source, open + 2, Template(nil).close())
tag = source.slice(open + 2, close).trim()
if (
Template(nil).starts_text(tag, "if ") or Template(nil).starts_text(tag, "for ")
)
depth = depth + 1
elseif tag == "end"
if depth == 0
return { else: else_pos, else_end: else_end, end: open, end_end: close + 2 }
depth = depth - 1
else
if tag == "else" and depth == 0
else_pos = open
else_end = close + 2
pos = close + 2
raise error("template.render: missing end")
find_close
Template.find_close(source, start, marker)
lib/template.tya:105
Template.find_close provides the template/Template standard library operation.
Source
# Template.find_close provides the template/Template standard library operation.
# @param source String source value.
# @param start Int start value.
# @param marker Any marker value.
# @return Any the resulting value.
find_close: source, start, marker ->
i = start
while i + marker.len() <= source.byte_len()
if source.slice(i, i + marker.len()) == marker
return i
i = i + 1
-1
find_next_tag
Template.find_next_tag(source, start)
lib/template.tya:117
Template.find_next_tag provides the template/Template standard library operation.
Source
# Template.find_next_tag provides the template/Template standard library operation.
# @param source String source value.
# @param start Int start value.
# @return Any the resulting value.
find_next_tag: source, start ->
i = start
while i + 1 < source.byte_len()
if (
source[i] == Template(nil).lbrace() and source[i + 1] == Template(nil).lbrace()
)
return i
i = i + 1
-1
initialize
Template.initialize(source = nil)
lib/template.tya:10
Template.initialize stores template source or path.
Source
# Template.initialize stores template source or path.
# @param source String source value.
# @return Self the initialized object.
initialize: source = nil ->
self.source = source
is_number
Template.is_number(text)
lib/template.tya:130
Template.is_number provides the template/Template standard library operation.
Source
# Template.is_number provides the template/Template standard library operation.
# @param text String text value.
# @return Boolean the resulting value.
is_number: text ->
if text == ""
return false
i = 0
while i < text.byte_len()
c = ord(text[i])
if c < 48 or c > 57
return false
i = i + 1
true
lbrace
Template.lbrace()
lib/template.tya:143
Template.lbrace provides the template/Template standard library operation.
Source
# Template.lbrace provides the template/Template standard library operation.
# @return Any the resulting value.
lbrace: ->
"{{"
lookup
Template.lookup(path, env)
lib/template.tya:150
Template.lookup provides the template/Template standard library operation.
Source
# Template.lookup provides the template/Template standard library operation.
# @param path String path value.
# @param env Any env value.
# @return Any the resulting value.
lookup: path, env ->
p = path.trim()
parts = Template(nil).path_parts(p)
if parts.len() == 0
return { ok: false, value: "" }
first = parts[0]
ok = false
value = nil
locals = env["locals"]
data = env["data"]
options = env["options"]
if locals.has(first)
ok = true
value = locals[first]
elseif data.has(first)
ok = true
value = data[first]
if not ok
if Template(nil).strict(options)
raise error("template.render: missing " + p)
return { ok: false, value: "" }
i = 1
while i < parts.len()
key = parts[i]
if key == ""
return { ok: true, value: value }
if Template(nil).is_number(key)
value = value[key.to_i()]
elseif value.has(key)
value = value[key]
else
if Template(nil).strict(options)
raise error("template.render: missing " + p)
return { ok: false, value: "" }
i = i + 1
{ ok: true, value: value }
open
Template.open()
lib/template.tya:189
Template.open provides the template/Template standard library operation.
Source
# Template.open provides the template/Template standard library operation.
# @return Any the resulting value.
open: ->
Template(nil).lbrace() + Template(nil).lbrace()
option
Template.option(options, name, default)
lib/template.tya:197
Template.option provides the template/Template standard library operation.
Source
# Template.option provides the template/Template standard library operation.
# @param options Dict options value.
# @param name String name value.
# @param default Any default value.
# @return Any the resulting value.
option: options, name, default ->
if options.has(name)
return options[name]
default
path_parts
Template.path_parts(path)
lib/template.tya:205
Template.path_parts provides the template/Template standard library operation.
Source
# Template.path_parts provides the template/Template standard library operation.
# @param path String path value.
# @return String the resulting value.
path_parts: path ->
parts = []
cur = ""
i = 0
while i < path.byte_len()
c = path[i]
if c == "."
if cur != ""
parts.push(cur)
cur = ""
elseif c == "["
if cur != ""
parts.push(cur)
cur = ""
i = i + 1
idx = ""
while i < path.byte_len() and path[i] != "]"
idx = idx + path[i]
i = i + 1
parts.push(idx)
else
cur = cur + c
i = i + 1
if cur != ""
parts.push(cur)
parts
raw_close
Template.raw_close()
lib/template.tya:234
Template.raw_close provides the template/Template standard library operation.
Source
# Template.raw_close provides the template/Template standard library operation.
# @return Any the resulting value.
raw_close: ->
Template(nil).rbrace() + Template(nil).close()
raw_open
Template.raw_open()
lib/template.tya:239
Template.raw_open provides the template/Template standard library operation.
Source
# Template.raw_open provides the template/Template standard library operation.
# @return Any the resulting value.
raw_open: ->
Template(nil).open() + Template(nil).lbrace()
rbrace
Template.rbrace()
lib/template.tya:244
Template.rbrace provides the template/Template standard library operation.
Source
# Template.rbrace provides the template/Template standard library operation.
# @return Any the resulting value.
rbrace: ->
"}}"
read_tag
Template.read_tag(state)
lib/template.tya:250
Template.read_tag provides the template/Template standard library operation.
Source
# Template.read_tag provides the template/Template standard library operation.
# @param state Any state value.
# @return Any the resulting value.
read_tag: state ->
state["pos"] = state["pos"] + 2
tag = Template(nil).read_until(state, Template(nil).close())
tag.trim()
read_until
Template.read_until(state, marker)
lib/template.tya:259
Template.read_until provides the template/Template standard library operation.
Source
# Template.read_until provides the template/Template standard library operation.
# @param state Any state value.
# @param marker Any marker value.
# @return Any the resulting value.
read_until: state, marker ->
src = state["source"]
start = state["pos"]
close = Template(nil).find_close(src, start, marker)
if close < 0
raise error(Template(nil).error(state, "unclosed tag"))
state["pos"] = close + marker.len()
src.slice(start, close)
render
Template.render(source, data = nil, options = nil)
lib/template.tya:274
render expands source using data and options. Values are HTML-escaped unless raw template syntax is used.
Source
# render expands source using data and options.
# Values are HTML-escaped unless raw template syntax is used.
# @param source String source value.
# @param data Array data value.
# @param options Dict options value.
# @return Any the resulting value.
render: source, data = nil, options = nil ->
if self.source != nil and data != nil and data.class == Dict
options = data
data = source
source = self.source
if data == nil
data = source
source = self.source
if options == nil
options = {}
Template(nil).render_with_options(source, data, options)
render_file
Template.render_file(path, data = nil, options = nil)
lib/template.tya:292
render_file reads a template file and renders it with data and options. Raises when the file cannot be read or the template is invalid.
Source
# render_file reads a template file and renders it with data and options.
# Raises when the file cannot be read or the template is invalid.
# @param path String path value.
# @param data Array data value.
# @param options Dict options value.
# @return Any the resulting value.
render_file: path, data = nil, options = nil ->
if self.source != nil and data != nil and data.class == Dict
options = data
data = path
path = self.source
if data == nil
data = path
path = self.source
if options == nil
options = {}
Template(nil).render_with_options(read_file(path), data, options)
render_for
Template.render_for(state, tag, env)
lib/template.tya:309
Template.render_for provides the template/Template standard library operation.
Source
# Template.render_for provides the template/Template standard library operation.
# @param state Any state value.
# @param tag Any tag value.
# @param env Any env value.
# @return Any the resulting value.
render_for: state, tag, env ->
parts = tag.split(" ")
if parts.len() != 4 or parts[2] != "in"
raise error(Template(nil).error(state, "invalid for"))
name = parts[1]
path = parts[3]
body_start = state["pos"]
split = Template(nil).find_block_split(state["source"], body_start)
state["pos"] = split["end_end"]
found = Template(nil).lookup(path, env)
if not found["ok"]
return ""
out = ""
for item in found["value"]
child = Template(nil).copy_dict(env["locals"])
child[name] = item
child_env = { data: env["data"], locals: child, options: env["options"] }
out = out
+ Template(nil).render_range(state["source"], body_start, split["end"], child_env)
out
render_html
Template.render_html(source, data = nil)
lib/template.tya:334
Template.render_html provides the template/Template standard library operation.
Source
# Template.render_html provides the template/Template standard library operation.
# @param source String source value.
# @param data Array data value.
# @return Any the resulting value.
render_html: source, data = nil ->
if data == nil
data = source
source = self.source
Template(nil).render_with_options(source, data, { escape: "html" })
render_if
Template.render_if(state, tag, env)
lib/template.tya:345
Template.render_if provides the template/Template standard library operation.
Source
# Template.render_if provides the template/Template standard library operation.
# @param state Any state value.
# @param tag Any tag value.
# @param env Any env value.
# @return Any the resulting value.
render_if: state, tag, env ->
cond = tag.slice(3, tag.len())
truth = false
found = Template(nil).lookup(cond.trim(), env)
if found["ok"] and found["value"]
truth = true
then_start = state["pos"]
split = Template(nil).find_block_split(state["source"], then_start)
state["pos"] = split["end_end"]
if split["else"] >= 0
if truth
return Template(nil).render_range(state["source"], then_start, split["else"], env)
return Template(nil).render_range(state["source"], split["else_end"], split["end"], env)
if truth
return Template(nil).render_range(state["source"], then_start, split["end"], env)
""
render_partial
Template.render_partial(tag, env)
lib/template.tya:366
Template.render_partial provides the template/Template standard library operation.
Source
# Template.render_partial provides the template/Template standard library operation.
# @param tag Any tag value.
# @param env Any env value.
# @return Any the resulting value.
render_partial: tag, env ->
rest = tag.slice(8, tag.len()).trim()
if rest.byte_len() < 2 or rest[0] != "\""
raise error("template.render: invalid partial")
i = 1
name = ""
while i < rest.byte_len() and rest[i] != "\""
name = name + rest[i]
i = i + 1
if i >= rest.byte_len()
raise error("template.render: invalid partial")
ctx_path = rest.slice(i + 1, rest.len()).trim()
options = env["options"]
if not options.has("partials")
raise error("template.render: missing partials")
partials = options["partials"]
if not partials.has(name)
raise error("template.render: missing partial " + name)
ctx = env["data"]
if ctx_path != ""
found = Template(nil).lookup(ctx_path, env)
if found["ok"]
ctx = found["value"]
else
ctx = {}
Template(nil).render_with_options(partials[name], ctx, options)
render_range
Template.render_range(source, start, finish, env)
lib/template.tya:399
Template.render_range provides the template/Template standard library operation.
Source
# Template.render_range provides the template/Template standard library operation.
# @param source String source value.
# @param start Int start value.
# @param finish Any finish value.
# @param env Any env value.
# @return Any the resulting value.
render_range: source, start, finish, env ->
part = source.slice(start, finish)
state = { source: part, pos: 0, len: part.byte_len() }
Template(nil).render_until(state, env, "")
render_raw_tag
Template.render_raw_tag(state, env)
lib/template.tya:408
Template.render_raw_tag provides the template/Template standard library operation.
Source
# Template.render_raw_tag provides the template/Template standard library operation.
# @param state Any state value.
# @param env Any env value.
# @return Any the resulting value.
render_raw_tag: state, env ->
state["pos"] = state["pos"] + 3
tag = Template(nil).read_until(state, Template(nil).raw_close())
Template(nil).render_value(tag.trim(), env, false)
render_until
Template.render_until(state, env, stop)
lib/template.tya:418
Template.render_until provides the template/Template standard library operation.
Source
# Template.render_until provides the template/Template standard library operation.
# @param state Any state value.
# @param env Any env value.
# @param stop Any stop value.
# @return Any the resulting value.
render_until: state, env, stop ->
out = ""
src = state["source"]
while state["pos"] < state["len"]
if stop != "" and Template(nil).starts_tag(state, stop)
return out
if Template(nil).starts(state, Template(nil).raw_open())
out = out + Template(nil).render_raw_tag(state, env)
elseif Template(nil).starts(state, Template(nil).open())
tag = Template(nil).read_tag(state)
if Template(nil).starts_text(tag, "if ")
out = out + Template(nil).render_if(state, tag, env)
elseif Template(nil).starts_text(tag, "for ")
out = out + Template(nil).render_for(state, tag, env)
else
if Template(nil).starts_text(tag, "partial ")
out = out + Template(nil).render_partial(tag, env)
elseif tag == "else" or tag == "end"
raise error(Template(nil).error(state, "unexpected " + tag))
else
out = out + Template(nil).render_value(tag, env, true)
else
out = out + src[state["pos"]]
state["pos"] = state["pos"] + 1
if stop != ""
raise error(Template(nil).error(state, "missing " + stop))
out
render_value
Template.render_value(path, env, escaped)
lib/template.tya:451
Template.render_value provides the template/Template standard library operation.
Source
# Template.render_value provides the template/Template standard library operation.
# @param path String path value.
# @param env Any env value.
# @param escaped Any escaped value.
# @return Any the resulting value.
render_value: path, env, escaped ->
found = Template(nil).lookup(path, env)
if not found["ok"]
return ""
text = found["value"].to_s()
if (
escaped and Template(nil).option(env["options"], "escape", "none") == "html"
)
return Template(nil).escape_html(text)
text
render_with_options
Template.render_with_options(source, data, options)
lib/template.tya:467
Template.render_with_options provides the template/Template standard library operation.
Source
# Template.render_with_options provides the template/Template standard library operation.
# @param source String source value.
# @param data Array data value.
# @param options Dict options value.
# @return Dict the resulting value.
render_with_options: source, data, options ->
state = { source: source, pos: 0, len: source.byte_len() }
env = { data: data, locals: {}, options: options }
Template(nil).render_until(state, env, "")
starts
Template.starts(state, text)
lib/template.tya:476
Template.starts provides the template/Template standard library operation.
Source
# Template.starts provides the template/Template standard library operation.
# @param state Any state value.
# @param text String text value.
# @return Any the resulting value.
starts: state, text ->
src = state["source"]
pos = state["pos"]
if pos + text.byte_len() > state["len"]
return false
i = 0
while i < text.byte_len()
if src[pos + i] != text[i]
return false
i = i + 1
true
starts_tag
Template.starts_tag(state, tag)
lib/template.tya:492
Template.starts_tag provides the template/Template standard library operation.
Source
# Template.starts_tag provides the template/Template standard library operation.
# @param state Any state value.
# @param tag Any tag value.
# @return Any the resulting value.
starts_tag: state, tag ->
if not Template(nil).starts(state, Template(nil).open())
return false
close = Template(nil).find_close(
state["source"],
state["pos"] + 2,
Template(nil).close()
)
if close < 0
return false
text = state["source"].slice(state["pos"] + 2, close).trim()
text == tag
starts_text
Template.starts_text(text, prefix)
lib/template.tya:509
Template.starts_text provides the template/Template standard library operation.
Source
# Template.starts_text provides the template/Template standard library operation.
# @param text String text value.
# @param prefix Any prefix value.
# @return String the resulting value.
starts_text: text, prefix ->
if prefix.len() > text.byte_len()
return false
text.slice(0, prefix.len()) == prefix
strict
Template.strict(options)
lib/template.tya:517
Template.strict provides the template/Template standard library operation.
Source
# Template.strict provides the template/Template standard library operation.
# @param options Dict options value.
# @return Any the resulting value.
strict: options ->
options.has("strict") and options["strict"]