buildString
():string
Parameter No. 1
Required- Generic technical name
- targetString
- Type
- string
Parameter No. 2
Required- Generic technical name
- templateVariables
- Type
- object
We are sorry, this this page has not been checked by English native speakers yet.
Allows to build the strings using the syntax similar to ES6 template literals. However, due to Stylus limitations, it is required to pass the associative array-like object via second parameter where the keys must refer to variable's names mentioned in the template string.
p(buildString("Good morning, ${name}", { name: "Takeshi" }))
If the string is short and there are only one-two variables, it is possible to
write the single line of code as in this simplest example.
p(buildString("Good morning, ${ name }", { name: "Takeshi" }))
The regular spaces inside the curly
brackets are allowed.
p(
buildString(
"In the quaint village of ${ villageName }, nestled amidst the rolling hills of the countryside, lived the " +\
"${ familyName } family. " +\
"Mr. ${ givenName } ${ familyName }, a respected member of the community, was known for his " +\
"kindness and generosity.",
{
villageName: "Willowbrook",
familyName: "Thompson",
givenName: "Jonathan"
}
)
)
The basic example will be multiline.
Be careful with the line breaking in Stylus: no spaces
allowed after the backslash character.
p(buildString("You have ${ messagesCount } message(s)", { messagesCount: 2 }))
You can interpolate the numeric values as well.
// INVALID SYNTAX!!!
p(buildString("Good morning, ${name}", { name }));
Unfortunately the Stylus does not support the
shorthand notation for the object properties
which available in ECMAScript languages since
ECMAScript 2015.
interpolationMethodsComparison.$heading
interpolationMethodsComparison.concatenation.$heading
givenName = "Takeshi"
familyName = "Tokugawa"
p("Good morning, " + givenName + " " + familyName)
interpolationMethodsComparison.sprintf.$heading
givenName = "Takeshi"
familyName = "Tokugawa"
outputString = "Good morning, %s %s" % (unquote(givenName) unquote(familyName))
p(outputString)
interpolationMethodsComparison.buildString.$heading
p(
buildString(
"Good morning, ${ givenName } ${ familyName }",
{
givenName: "Takeshi",
familyName: "Tokugawa"
}
)
)