Example of javascript Text Template usage and javascript example

Source: Internet
Author: User

Example of javascript Text Template usage and javascript example

This document describes how to use a javascript Text Template. Share it with you for your reference. The details are as follows:

This is a small function I wrote with reference to the Prism. js algorithm. There is nothing to say. As a programmer, I should understand the example in seconds.

String template engine class:

/*class*/StringTemplate = function (/* Optional {patt: RegExp, clPatt: RegExp}*/pattern) {  if (!!pattern) {    this.patt = pattern.patt;    this.clPatt = pattern.clPatt;  } else {    this.patt = /%\{\s*[\w\-]+\s*\}/g;    this.clPatt = /(^%\{\s*)|(\s*\}$)/g;  }  this.format = function(val, map) {    var ls = [];    var res;    var prevEnd = 0;    while ((res = this.patt.exec(val)) != null) {      var va = res[0];      var start = val.substr(prevEnd, res.index - prevEnd);      prevEnd = res.index + va.length;      ls.push(start);      var vac = va.replace(this.clPatt, "");      ls.push(map[vac]);    }    ls.push(val.substr(prevEnd, val.length));    return ls.join("");  }}

Usage:

Var str = new StringTemplate (). format ("Hello % {userName}, welcome to log on again % {systemName}", {userName: "James", systemName: "jb51 "}); // str = "Hello James, welcome to jb51 again ";

I hope this article will help you design javascript programs.

Related 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.