Js-template-art "Four" is registered by helper method, calling external method

Source: Internet
Author: User
Tags dateformat

First, the grammar

Calling external functions in template code requires a helper method to register

Template.helper (name, callback)
Ii. use of "instances"

Original: http://blog.csdn.net/u011413061/article/details/50498470

Prerequisite: Replace use

HTML code:

<!--Data Container -<DivID= "Content"></Div><!--/Data Container -<ScriptID= "Test"type= "text/html">    /*to format a time object*/{{ time|DateFormat:'yyyy mm month DD Day HH:MM:SS'}}</Script>

JavaScript code

/** * Format dates, * @param date to format * @param format formatted pattern String * Supported pattern letters are: * y: Year, * M: Month of the Year (1-12), * d: Day of the Month (1-31), * H: Hours (0-23), * m: Min (0-59), * s: S (0-59), * s: msec (0-999), * Q: Quarter (1-4) * @return String * @author Yanis.wang * @see http://yaniswang.com/frontend/2013/02/16/dateformat-performance/*/Template.helper (' DateFormat ',function(date, format) {Date=NewDate (date);//new Date Object    /*Date Dictionary*/    varMap = {        "M": Date.getmonth () + 1,//Month"D": date.getdate (),//Day"H": date.gethours (),//hours"M": date.getminutes (),//points"S": Date.getseconds (),//seconds"Q": Math.floor ((Date.getmonth () + 3)/3),//Quarterly"S": Date.getmilliseconds ()//milliseconds    }; /*Regular Replacement*/format= Format.replace (/([YMDHMSQS]) +/g,function(all, t) {varv =Map[t]; if(v!==undefined) {            if(All.length > 1) {v= ' 0 ' +v; V= V.substr (v.length-2); }            returnv; }        Else if(t = = = ' Y '){            return(Date.getfullyear () + "). substr (4-all.length); }        returnAll ;    }); /*return to itself*/    returnformat;});/*Data*/vardata ={time: (NewDate). ToString (),};/*Rendering*/varhtml = template (' Test ', data);/*binding*/document.getElementById (' content '). InnerHTML = html;

Js-template-art "Four" is registered by helper method, calling external method

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.