代码之家  ›  专栏  ›  技术社区  ›  chakrit Dutchie432

John Resig的“JavaScript微模板”有什么进展吗?

  •  19
  • chakrit Dutchie432  · 技术社区  · 16 年前

    我在网上看过这篇文章 JavaScript Micro-Templating

    但他在帖子中表示,他将在《JavaScript忍者秘籍》中保留一个更完善的版本,并提到他希望看到它的发展。

    所以我想知道,John Resig的微模板引擎是否有更稳定/更高级的版本?如果是的话,我怎样才能得到它?那本JavaScript书在我的国家不可用。

    7 回复  |  直到 15 年前
        1
  •  5
  •   Crescent Fresh    16 年前

    是否有更稳定/先进的 此微模板的版本

    看见 Rick Stahl's blog

        2
  •  3
  •   Michael    14 年前

    也看到 jQote2 . 我将引用网站本身的话:

    jQote(发音类似于《星际迷航》中的Chakotey)基本上是对johnresigs令人敬畏的JavaScript微模板实用程序的重写。我把他的代码移植到jQuery,彻底检查了解析/转换部分,并扩展了它的功能,以尽量减少每个人的编码工作。

        3
  •  2
  •   vadimk    14 年前

    看看jQuery插件

    https://github.com/vkiryukhin/vkTemplate

    这是建立在John Resig的基础上的 微模板引擎 一个“单引号”问题被修复,引擎根据插件的架构稍微简化。

    演示和文档 http://www.eslinstructor.net/vktemplate/

        4
  •  1
  •   James Black    16 年前

    如果你去 http://www.manning.com/resig/ 您可以预先订购PDF,以便查看即将出版的书籍。

        5
  •  1
  •   benjamunky    14 年前

    js中的模板函数基于John Resig的微模板引擎。 http://documentcloud.github.com/underscore/#template

        6
  •  1
  •   user3165346    12 年前

    只有335个字节,速度极快,并且修复了许多源代码中的错误。

    https://github.com/leolems/javascript/tree/master/templates

        7
  •  1
  •   mzalazar    11 年前

    这是John Resig(稍加修改)的脚本,来自Rick Strahl的web( http://weblog.west-wind.com/posts/2008/Oct/13/Client-Templating-with-jQuery

    var _tmplCache = {}
    this.tmpl= function(str, data) {
    /// <summary>
    /// Client side template parser that uses &lt;#= #&gt; and &lt;# code #&gt; expressions.
    /// and # # code blocks for template expansion.
    /// NOTE: chokes on single quotes in the document in some situations
    ///       use &amp;rsquo; for literals in text and avoid any single quote
    ///       attribute delimiters.
    /// </summary>    
    /// <param name="str" type="string">The text of the template to expand</param>    
    /// <param name="data" type="var">
    /// Any data that is to be merged. Pass an object and
    /// that object's properties are visible as variables.
    /// </param>    
    /// <returns type="string" />  
    var err = "";
    try {
        var func = _tmplCache[str];
        if (!func) {
            var strFunc =
            "var p=[],print=function(){p.push.apply(p,arguments);};" +
                        "with(obj){p.push('" +
            //                        str
            //                  .replace(/[\r\t\n]/g, " ")
            //                  .split("<#").join("\t")
            //                  .replace(/((^|#>)[^\t]*)'/g, "$1\r")
            //                  .replace(/\t=(.*?)#>/g, "',$1,'")
            //                  .split("\t").join("');")
            //                  .split("#>").join("p.push('")
            //                  .split("\r").join("\\'") + "');}return p.join('');";
    
            str.replace(/[\r\t\n]/g, " ")
               .replace(/'(?=[^#]*#>)/g, "\t")
               .split("'").join("\\'")
               .split("\t").join("'")
               .replace(/<#=(.+?)#>/g, "',$1,'")
               .split("<#").join("');")
               .split("#>").join("p.push('")
               + "');}return p.join('');";
    
            //alert(strFunc);
            func = new Function("obj", strFunc);
            _tmplCache[str] = func;
        }
        return func(data);
    } catch (e) { err = e.message; }
    return "< # ERROR: " + err.htmlEncode() + " # >";
    }
    

    tmpl($('myHtmlTempl').html(),数据);

    我对它进行了测试,它使用“单引号”(这是我的主要问题,直到我找到这个)。

        8
  •  0
  •   kimbo    6 年前

    我所做的一个小改进/定制是允许在模板的id中使用连字符。对于原始代码,这将不起作用:

    <script type="text/html" id="my-awesome-template">
      <!-- template contents -->
    </script>
    

    为了解决这个问题,我修改了第9行中的正则表达式

    var fn = !/\W/.test(str) ?
    

    对下列事项:

    var fn = !/[^a-zA-Z0-9_-]/.test(str) ?
    
    推荐文章