node. JS Development Notes II • Page article

Source: Internet
Author: User


Objective:

Originally tangled inWeb Templates , chose theHandlebars. It was later found that the pages were weak logic and did not support complex logical expressions. Almost to give up the occasion, remembered theJavascriptinEvalfunction. AlthoughEvalThe function is powerful and powerful to generate objects or execute code "in thin Air", but it always feels like he's breaking the code's elegance. Plus"eval"and the"Evil" (Evil)It looks very much like that. Evalfunction is not a good impression, and most of the time it is treated as"Forbidden Hand". At this time there is no good way anyway. ThroughHandlebarsCustom FunctionsUse eval to perform the desired logic. Logical judgment in the pattern of stitching characters can theoretically handle most of the logic on a page just like an El expression. It seems that the effect is good oh.

Case:

Installing handlebars

NPM Install handlebars

Or in our previous Express project. Build two files.

Template:test_expression.html

<! DOCTYPE html>

JS code : Test_expression.js

var fs = require (' FS '); var HBS = require (' handlebars ');//The main idea is to use eval to perform the desired logic. Logical judgment in the pattern of stitching characters can theoretically handle most of the logic on a page just like an El expression. For example: {{#expression a ' = = ' B ' && ' C ' > ' 0}}hbs.registerhelper (' expression ', function () {var exps = []; try{// The latter parameter is used as the display content, which is the usual options. Not as a logical expression part var Arg_len = Arguments.length;var len = arg_len-1;for (var j = 0;j<len;j++) {Exps.push (arguments[j]);} var result = eval (Exps.join (")"), if (result) {return Arguments[len].fn (this);} else {return arguments[len].inverse (th is);} }catch (e) {throw new Error (' Handlerbars Helper "expression" can not be deal with wrong expression: ' +exps.join (') + ".");}}     ); var template = Hbs.compile (Fs.readfilesync ('.. /templates/test_expression.html '). ToString ()), Var http =require ("http"), Http.createserver (function (Request,   Response) {var conditions = {X:{a:4,b:2}};   var html = template (conditions);   Response.writehead ($, {"Content-type": "Text/html", "CharSet": "UTF-8"});   Response.Write (HTML); ResponSe.end ();}). Listen (3000);

Run CMD command line mode to enter the node.express\test directory to execute node test_expression.js


You can change the var conditions = {X:{a:4,b:2}} to validate the logical expression.

Summarize:

Thank you very much for seeing the children's shoes here. The above content is actually some basic set of page flow. The focus is on handlebar 's custom helper functionality and resolving page weak logic. Personal Support handlebar author of the page weak logic point of view. But feel at least to support the logical expression. Otherwise a page that writes a heap of if else or other logical expressions derived code is too tiring. Once wanted to give up this piece. Until you write the expression label, you find that there is another way ahead. predict the funeral, and listen to tell.

node. JS Development Notes II • Page article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.