Review by English Native Speakers Is Required
This page has not been edited by the English native speakers yet.
We are sorry for the non-perfect English language.
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.
String Building Methods Comparison
String Concatenation (Native)
givenName = "Takeshi"
familyName = "Tokugawa"
p("Good morning, " + givenName + " " + familyName)
Sprintf (Native)
givenName = "Takeshi"
familyName = "Tokugawa"
outputString = "Good morning, %s %s" % (unquote(givenName) unquote(familyName))
p(outputString)
buildString
(YDF)
p(
buildString(
"Good morning, ${ givenName } ${ familyName }",
{
givenName: "Takeshi",
familyName: "Tokugawa"
}
)
)