Here I’m going to illustrate:
Using anyone of programming languages, with only two operations: string concatenation and type conversion, we have no needs to use template languages, such as format in “printf” of C language, mustache, liquid, jinja2 and etc.
You can review the opinion in this article with template languages in web application frameworks, such as vuejs, angularjs, and reactjs and etc.(And reactjs somewhat do it in right ways.)
You can also review the opinion in this article with PHP, JSP, ASP and Nunjucks.
Here I choose “emacs lisp” as the programming language to illustrate the process.
int main(void)
{
char MyName[] = "Wang Bowen";
int MyAge = 26;
printf("Hello world, my name is %s, my age is %d", MyName, MyAge);
return 0;
}
Where the process must parse “Hello world, my name is %s, my age is %d”, find “%s” and “%d”, then replace these with MyName and Myage which is going to be conversed into string.
Whereas we can make the process done using emacs lisp in this way:
(setq MyName "Wang Bowen"
MyAge 26)
(message
(concat "Hello, world, my name is "
MyName
", my age is "
(int-to-string MyAge)))
“message” outputs the concatenated string into console.
“concat” concatenates the strings.
“int-to-string” converses the “int” type variable into string.
All are direct. We don’t need to parse “Hello world, my name is %s, my age is %d”.
Using hash:
{ “MyName”:”Wang Bowen”, “MyAge”:26 }
We have to parse the string:
Hello world, my name is {{MyName}}, my age is {{MyAge}}
Then give the result.
Whereas we can make the process done using emacs lisp in this way:
(setq MyName "Wang Bowen"
MyAge 26)
(message
(concat "Hello, world, my name is "
MyName
", my age is "
(int-to-string MyAge)))
The same as mustache.
Whereas we can make the process done using emacs lisp in this way:
(setq MyName "Wang Bowen"
MyAge 26)
(message
(concat "Hello, world, my name is "
MyName
", my age is "
(int-to-string MyAge)))
The same as liquid.
Whereas we can make the process done using emacs lisp in this way:
(setq MyName "Wang Bowen"
MyAge 26)
(message
(concat "Hello, world, my name is "
MyName
", my age is "
(int-to-string MyAge)))
When we input the data, and want the output as text file, we can directly create the process using programming languages:
Where the “DS” stands for “Data Structure” and the “RDS” stands for “Runnable Data Structure”. Source code in one programming languages can be regarded as “Runnable Data Structure”.
When we want to input some data structure such as “my name” and “my age” and get the output which has stationary format, we can directly use one programming language to create “Runnable Data Structure” in order to deal with the process.
There is no need to create a new template languages to make the process complicated. We can skip the procedure parsing template languages so that the process speeds up, what’s more, it can be easier to make the process in parallel (yes, multiple strings concatenation can be done in parallel).
The thought comes from one article 理论上最好的语言: 封装定则篇 in the project OOWA.
I choose creativecommons as the copyright:
If you like the opinion in this article, please star this article! and propagate it with your collaborators!