Built-in Helpers
There are many helper functions available for the scaffdog template engine.
Helpers#
camel
#
Conversion to a camel case.
Signature:
camel(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ "foo-bar" | camel }}--> fooBar
snake
#
Conversion to a snake case.
Signature:
snake(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ "foo-bar" | snake }}--> foo_bar
pascal
#
Conversion to a pascal case.
Signature:
pascal(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ "foo-bar" | pascal }}--> FooBar
kebab
#
Conversion to a kebab case.
Signature:
kebab(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ "foo-bar" | kebab }}--> foo-bar
constant
#
Conversion to a constant case.
Signature:
constant(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ "foo-bar" | constant }}--> FOO_BAR
upper
#
Conversion to a upper case.
Signature:
upper(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ "foo-bar" | upper }}--> FOO-BAR
lower
#
Conversion to a lower case.
Signature:
lower(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ "FOO-BAR" | lower }}--> foo-bar
plur
#
Pluralize a word. See the plur documentation for details.
Signature:
plur(value, count?)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
count | number | The count to determine whether to use singular or plural. If omitted, defaults to plural. |
Example:
{{ "dog" | plur }}--> dogs{{ "dog" | plur 1 }}--> dog{{ "dog" | plur 2 }}--> dogs
replace
#
Replace pattern
with replacement
. pattern
is specified as a string, but it is treated as a regular expression.
Signature:
replace(value, pattern, replacement)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
pattern | string | Pattern string to search. |
replacement | string | String to overwrite found string |
Example:
{{ "foo-bar" | replace "-" "/" }}--> foo/bar
trim
#
Alias for String.prototype.trim
.
Signature:
trim(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ " foo " | trim }}--> "foo"
ltrim
#
Alias for String.prototype.trimStart
.
Signature:
ltrim(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ " foo " | ltrim }}--> "foo "
rtrim
#
Alias for String.prototype.trimEnd
.
Signature:
rtrim(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ " foo " | rtrim }}--> " foo"
seq
#
Generates a numeric array for the specified range. Similar signature to the seq
command.
Signature:
seq(first, increment?, last?)
Paramters:
Parameter | Type | Description |
---|---|---|
first | number | Start numeric value. |
increment | number | Numeric value to be added repeatedly. |
last | number | Last numeric value. |
Example:
{{ seq 5 }}--> 1,2,3,4,5{{ seq 0 5 }}--> 0,1,2,3,4,5{{ seq 1 2 10 }}--> 1,3,5,7,9
append
#
Appends elements to an array and returns the resulting array. The array received as an argument is not modified, and immutable operations are performed.
Signature:
append(value, ...args)
Paramters:
Parameter | Type | Description |
---|---|---|
value | any[] | Input value. |
args | any[] | Elements to be added to the array. |
Example:
{{ seq 3 | append 10 }}--> 1,2,3,10{{ seq 3 | append 4 | append 5 }}--> 1,2,3,4,5
uniq
#
Takes duplicates from array elements and returns the resulting array. The array received as an argument is not modified, and immutable operations are performed.
Signature:
uniq(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | any[] | Input value. |
Example:
{{ arr := seq 2 | append 1 | append 2 | append 3 }}{{ arr }}--> 1,2,1,2,3{{ arr | uniq }}--> 1,2,3
split
#
Alias for String.prototype.split
.
Signature:
split(value, sep)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
sep | string | Delimiter string value to split. |
Example:
{{ "1/2/3" | split "/" }}--> 1,2,3
join
#
Alias for Array.prototype.join
.
Signature:
join(value, sep)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
sep | string | Delimiter string value to join. |
Example:
{{ "1/2/3" | split "/" | join "_" }}--> 1_2_3
len
#
Returns the length of the value. If the length cannot be determined, returns 0.
Signature:
len(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | any | Input value. |
Example:
{{ "123" | len }}--> 3
slice
#
Returns portion of an array. Similar signature to String.prototype.slice
.
Signature:
slice(value, start, end?)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | any[] | Input value. |
start | number | The first index to include in the return value. |
end | number | The index to include at the end of the return value. |
Example:
{{ "12345" | slice 2 }}--> 345{{ "12345" | slice 1 3 }}--> 23
contains
#
Searches for an item in a string or array and returns a boolean value.
Signature:
contains(search, item)
Paramters:
Parameter | Type | Description |
---|---|---|
search | string | any[] | A string or array to search for. |
item | any | Item to search for. |
Example:
{{ contains("foo", "o") ? "true" : "false" }}--> true{{ contains("foo", "b") ? "true" : "false" }}--> false{{ contains(seq(3), 2) ? "true" : "false" }}--> true{{ contains(seq(3), 4) ? "true" : "false" }}--> false
s2n
#
Converts a string to a number.
Signature:
s2n(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
Example:
{{ "123" | s2n }}--> 123
n2s
#
Converts a number to a string.
Signature:
n2s(value)
Paramters:
Parameter | Type | Description |
---|---|---|
value | number | Input value. |
Example:
{{ 123 | n2s }}--> "123"
before
#
Returns string before the position by n
. If n
is a number, it is the specified line; if it is a string, it is the first matched line.
See the Injection section for more information on how to make use of it.
Signature:
before(value, n, offset?)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
n | string | number | Pattern string to search. or number of lines. |
offset | number | Offset from search result position. |
Example:
{{ "line1line2line3" | before "line2" }}--> line1
after
#
Returns string after the position by n
. If n
is a number, it is the specified line; if it is a string, it is the first matched line.
See the Injection section for more information on how to make use of it.
Signature:
after(value, n, offset?)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
n | string | number | Pattern string to search. or number of lines. |
offset | number | Offset from search result position. |
Example:
{{ "line1line2line3" | after "line2" }}--> line3
eval
#
Executes the specified code as JavaScript and returns the result.
Signature:
eval(code)
Paramters:
Parameter | Type | Description |
---|---|---|
code | string | Source code. |
Example:
{{ eval "1 + 2" }}--> 3
date
#
See the dayjs documentation for format details.
Signature:
date(format?)
Paramters:
Parameter | Type | Description |
---|---|---|
format | string | Date format. |
Example:
{{ date "YYYY/MM/DD" }}--> 2019/01/06
noop
#
Returns an empty string.
Signature:
noop()
Example:
{{ "foo" | upper | noop }}-->
define
#
Defines a local variable in the template scope.
Signature:
define(value, key)
Paramters:
Parameter | Type | Description |
---|---|---|
value | string | Input value. |
key | string | Variable key name. |
Example:
{{- "foo-bar" | camel | define "name" -}}{{ name }}--> fooBar
relative
#
Convert the path from the template file to the path from the destination file.
Signature:
relative(path)
Paramters:
Parameter | Type | Description |
---|---|---|
path | string | The file path to search. |
Example:
{{ relative "../src/utils" }}
resolve
#
Resolves a sequence of paths or path segments into an absolute path.
Signature:
resolve(...paths)
Paramters:
Parameter | Type | Description |
---|---|---|
paths | string[] | File path segments. |
Example:
{{ resolve "../src" "common" }}
read
#
Read the specified file. The contents of the loaded file are also expanded as a template.
Signature:
read(path)
Paramters:
Parameter | Type | Description |
---|---|---|
path | string | Relative path to the file to be read. |
Example:
{{ resolve "../src" "common" }}
Last edited on