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:

ParameterTypeDescription
valuestringInput value.
Example:
{{ "foo-bar" | camel }}
--> fooBar
Try

snake#

Conversion to a snake case.

Signature:

snake(value)

Paramters:

ParameterTypeDescription
valuestringInput value.
Example:
{{ "foo-bar" | snake }}
--> foo_bar
Try

pascal#

Conversion to a pascal case.

Signature:

pascal(value)

Paramters:

ParameterTypeDescription
valuestringInput value.
Example:
{{ "foo-bar" | pascal }}
--> FooBar
Try

kebab#

Conversion to a kebab case.

Signature:

kebab(value)

Paramters:

ParameterTypeDescription
valuestringInput value.
Example:
{{ "foo-bar" | kebab }}
--> foo-bar
Try

constant#

Conversion to a constant case.

Signature:

constant(value)

Paramters:

ParameterTypeDescription
valuestringInput value.
Example:
{{ "foo-bar" | constant }}
--> FOO_BAR
Try

upper#

Conversion to a upper case.

Signature:

upper(value)

Paramters:

ParameterTypeDescription
valuestringInput value.
Example:
{{ "foo-bar" | upper }}
--> FOO-BAR
Try

lower#

Conversion to a lower case.

Signature:

lower(value)

Paramters:

ParameterTypeDescription
valuestringInput value.
Example:
{{ "FOO-BAR" | lower }}
--> foo-bar
Try

plur#

Pluralize a word. See the plur documentation for details.

Signature:

plur(value, count?)

Paramters:

ParameterTypeDescription
valuestringInput value.
countnumberThe 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
Try

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:

ParameterTypeDescription
valuestringInput value.
patternstringPattern string to search.
replacementstringString to overwrite found string
Example:
{{ "foo-bar" | replace "-" "/" }}
--> foo/bar
Try

trim#

Alias for String.prototype.trim.

Signature:

trim(value)

Paramters:

ParameterTypeDescription
valuestringInput value.
Example:
{{ " foo " | trim }}
--> "foo"
Try

ltrim#

Alias for String.prototype.trimStart.

Signature:

ltrim(value)

Paramters:

ParameterTypeDescription
valuestringInput value.
Example:
{{ " foo " | ltrim }}
--> "foo "
Try

rtrim#

Alias for String.prototype.trimEnd.

Signature:

rtrim(value)

Paramters:

ParameterTypeDescription
valuestringInput value.
Example:
{{ " foo " | rtrim }}
--> " foo"
Try

seq#

Generates a numeric array for the specified range. Similar signature to the seq command.

Signature:

seq(first, increment?, last?)

Paramters:

ParameterTypeDescription
firstnumberStart numeric value.
incrementnumberNumeric value to be added repeatedly.
lastnumberLast 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
Try

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:

ParameterTypeDescription
valueany[]Input value.
argsany[]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
Try

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:

ParameterTypeDescription
valueany[]Input value.
Example:
{{ arr := seq 2 | append 1 | append 2 | append 3 }}
{{ arr }}
--> 1,2,1,2,3
{{ arr | uniq }}
--> 1,2,3
Try

split#

Alias for String.prototype.split.

Signature:

split(value, sep)

Paramters:

ParameterTypeDescription
valuestringInput value.
sepstringDelimiter string value to split.
Example:
{{ "1/2/3" | split "/" }}
--> 1,2,3
Try

join#

Alias for Array.prototype.join.

Signature:

join(value, sep)

Paramters:

ParameterTypeDescription
valuestringInput value.
sepstringDelimiter string value to join.
Example:
{{ "1/2/3" | split "/" | join "_" }}
--> 1_2_3
Try

len#

Returns the length of the value. If the length cannot be determined, returns 0.

Signature:

len(value)

Paramters:

ParameterTypeDescription
valueanyInput value.
Example:
{{ "123" | len }}
--> 3
Try

slice#

Returns portion of an array. Similar signature to String.prototype.slice.

Signature:

slice(value, start, end?)

Paramters:

ParameterTypeDescription
valuestring | any[]Input value.
startnumberThe first index to include in the return value.
endnumberThe index to include at the end of the return value.
Example:
{{ "12345" | slice 2 }}
--> 345
{{ "12345" | slice 1 3 }}
--> 23
Try

contains#

Searches for an item in a string or array and returns a boolean value.

Signature:

contains(search, item)

Paramters:

ParameterTypeDescription
searchstring | any[]A string or array to search for.
itemanyItem 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
Try

s2n#

Converts a string to a number.

Signature:

s2n(value)

Paramters:

ParameterTypeDescription
valuestringInput value.
Example:
{{ "123" | s2n }}
--> 123
Try

n2s#

Converts a number to a string.

Signature:

n2s(value)

Paramters:

ParameterTypeDescription
valuenumberInput value.
Example:
{{ 123 | n2s }}
--> "123"
Try

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:

ParameterTypeDescription
valuestringInput value.
nstring | numberPattern string to search. or number of lines.
offsetnumberOffset from search result position.
Example:
{{ "line1
line2
line3" | before "line2" }}
--> line1
Try

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:

ParameterTypeDescription
valuestringInput value.
nstring | numberPattern string to search. or number of lines.
offsetnumberOffset from search result position.
Example:
{{ "line1
line2
line3" | after "line2" }}
--> line3
Try

eval#

Executes the specified code as JavaScript and returns the result.

Signature:

eval(code)

Paramters:

ParameterTypeDescription
codestringSource code.
Example:
{{ eval "1 + 2" }}
--> 3
Try

date#

See the dayjs documentation for format details.

Signature:

date(format?)

Paramters:

ParameterTypeDescription
formatstringDate format.
Example:
{{ date "YYYY/MM/DD" }}
--> 2019/01/06
Try

noop#

Returns an empty string.

Signature:

noop()
Example:
{{ "foo" | upper | noop }}
-->
Try

define#

Defines a local variable in the template scope.

Signature:

define(value, key)

Paramters:

ParameterTypeDescription
valuestringInput value.
keystringVariable key name.
Example:
{{- "foo-bar" | camel | define "name" -}}
{{ name }}
--> fooBar
Try

relative#

Convert the path from the template file to the path from the destination file.

Signature:

relative(path)

Paramters:

ParameterTypeDescription
pathstringThe 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:

ParameterTypeDescription
pathsstring[]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:

ParameterTypeDescription
pathstringRelative path to the file to be read.

Example:

{{ resolve "../src" "common" }}
Edit this page on GitHub

Last edited on

Copyright © 2022 wadackel

Released under the MIT License

Built with Next.js

Source Code