Previously, massFramework had three different levels of template functions for processing String concatenation: format, tag, and ejs. Now we have added a new one, but it is only applicable to backend nodes. js environment (because in the browser environment, FF will remove all comments & hellip ;). It uses multiple... SyntaxHigh
Previously, the mass Framework had three different levels of template functions for processing String concatenation: format, tag, and ejs. Now another new type is added, but it only applies to backend nodes. js environment (because in the browser environment, FF will remove all comments ......). It uses multi-line annotations to simulate the here document in other dynamic languages. The following figure shows its implementation. It is actually a module that relies on the format method of the lang module.
Mass. define ("here_document", "lang", function (){
// Only one "\ *" is allowed at the beginning, and only one "*/" is allowed at the end "*/"
Mass. hereDoc = function (f ){
Var str = f. toString ().
Replace (/^ [^ \/] + \/\*!? /,'').
Replace (/\ * \/[^ \/] + $ /,'');
If (arguments. length> 1 ){
Arguments [0] = str;
Return mass. format. apply (mass, arguments)
}
Return str
}
})
The usage is as follows:
Mass. require ("here_document", function (){
Var a = mass. hereDoc (function (){/*
Var bbb = "ccc"
*/
});
Console. log (a) // output var bbb = "ccc"
});
Two Placeholders are also supported. For details, see here.
Mass. require ("here_document", function (){
Var a = mass. hereDoc (function (){/*
Var # {0} = "ccc"
*/
}, "Ddd ");
Console. log (a) // output var ddd = "ccc"
});
Some more specific applications. For example, in my hand tripod module scaffold. js, You need to generate the corresponding controller and action function through the model name.
For (var controller in mapper. controllers ){
Var object = mapper. controllers [controller];
// Create a controller
Var contents = ["mass. define (\" ", controller," _ controller \ ", function () {\ n", "\ treturn {\ n"]
// Create action
Contents. push (object. actions. map (function (action ){
Return "\ t \" "+ action +" \ ": function (){}"
}). Join (", \ n "));
Contents. push ("\ n \ t} \ n });");
CreateFile (path. join ("app", "controllers", controller + "_ controller. js"), contents. join (""))
//....
}
For example, a controller named comments has seven actions: "index", "create", "new", "edit", "destroy", "update", and "show. Then it generates a comments_controller.js file with the content
Mass. define ("comments_controller", function (){
Return {
"Index": function (){},
"Create": function (){},
"New": function (){},
"Edit": function (){},
"Destroy": function (){},
"Update": function (){},
"Show": function (){}
}
});
But it is not directly written above. It is written in this way by replacing it with the here document:
For (var controller in mapper. controllers ){
Var object = mapper. controllers [controller];
// Create a controller
Var contents = mass. hereDoc (function (){/*
Mass. define ("# {0} _ controller", function (){
Return {
# {1}
}
});*/
}, Controller, object. actions. map (function (action) {// create action
Return "\ t \" "+ action +" \ ": function (){}"
}). Join (", \ n "));
CreateFile (path. join ("app", "controllers", controller + "_ controller. js"), contents)
}
In another application of the tripod, it is more obvious:
Originally written, escape required
CreateFile ('app/controllers/application_controller.js ',
'Before (\ 'Protect from forgery \ ', function () {\ n protectFromForgery (\ ''+ secret +' \ '); \ n}); \ n ')
The implementation of here document is:
CreateFile ('app/controllers/application_controller.js ', mass. hereDoc (function (){/*
Before ('Protect from forgery ', function (){
ProtectFromForgery ("# {0 }")
})*/
}, Secret ));
You can see the final output at a Glance. This is a good news for the code maintainers!
From Ruby's Louvre