﻿(function () {//Шаблонизатор от - John Resig
    var cache = {};

    this.tmpl = function (str, data) {

        var cacheName = str;

        // Figure out if we're getting a template, or if we need to
        // load the template - and be sure to cache the result.

        // Generate a reusable function that will serve as a template
        // generator (and which will be cached).

        // Introduce the data as local variables using with(){}

        var fn = !/\W/.test(str) ? cache[cacheName] = cache[cacheName] || tmpl(str) :
                              new Function("obj",
                                                "var codeContainer=[],print=function(){codeContainer.push.apply(codeContainer,arguments);};" +

        // Convert the template into pure JavaScript
                                                "with(obj){codeContainer.push('" +
                                                str
                                                  .replace(/[\r\t\n]/g, " ")
                                                  .split("<%").join("\t")
                                                  .replace(/((^|%>)[^\t]*)'/g, "$1\r")
                                                  .replace(/\t=(.*?)%>/g, "',$1,'")
                                                  .split("\t").join("');")
                                                  .split("%>").join("codeContainer.push('")
                                                  .split("\r").join("\\'")
                                              + "');}return codeContainer.join('');"
                             );

        // Provide some basic currying to the user
        return data ? fn(data) : fn;
    };





//расширение резиговского скрипта от Вадимки
String.prototype.formatBy = function (obj, options) {



    if (!obj) {
        if (window['console'])
            console.log(this);
    }

    function format(template, obj, options) {

        var b = '<%=';
        var e = '%>';

        if (options && options.encode) {
            e = '.toString().htmlEncode()' + e;
        }

        template = template.replace(/{/gi, b);
        template = template.replace(/}/gi, e);

        return tmpl(template, obj);
    }


    return (obj) ? format(this, obj, options) : this;

}

String.prototype.htmlEncode = function () {
    return this.replace(/</g, '&#8249;').replace(/>/g, '&#8250;').replace(/"/g, '&#34;').replace(/'/g, '&#8242;');
}

})();






