主要成分:管理模板 [英] Backbone: managing templates

查看:108
本文介绍了主要成分:管理模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用下划线模板引擎为骨干的应用程序。截至目前我已经在&LT超过15模板; HEAD> 。它越来越难以维持。到目前为止,大部分的解决方案,我看到管理模板最终需要他们是js文件。这也很头疼,我preFER他们是HTML文件进行编辑的目的。

I'm using underscore template engine for an backbone application. As of now I have over 15 templates in the <head>. Its getting hard to maintain. So far, most of the solutions I seen to manage templates ended up needing them to be js files. That's also a headache, I prefer them to be html files for editing purposes.

我看了看requirejs和不知道如果我需要一个,因为它围绕还挺更加模块化的方法行健,我不能,我使用的那一刻说了(虽然我会很快)。

I took a look at requirejs and not sure if I need that since it kinda revolves around a more modular approach that I can't say I'm using at the moment (although I will soon).

会有什么管理模板和加载/根据需要缓存呢?最好的办法

What will be the best way to manage templates and load/cache them as needed?

推荐答案

个人,我们需要在我的公司一个强大的解决方案,所以我们用了:

Personally we needed a robust solution at my company, so we went with:


  • Require.js - 用于加载模块

  • 把手 - 更强大的模板比下划线可以提供

  • HBS - 一个极好的需要插件从亚历克斯·塞克斯顿处理带来编译模板通过要求

通过这个设置,我可以保持我的所有模板在自己的文件,然后使用它们我的文件是这样的:

With this setup I can keep all of my templates in their own file, and then to use them I have files like this:

define(['template!path/to/someTemplate'], function(someTemplate) {
    var MyNewView = BaseView.extend({template: someTemplate});
    $('body').append(new MyNewView().render().el);
}

(正如您可能已经猜到,我们有所谓的基本视角基地骨干视图,它使用视图的模板属性来渲染视图)。

(and as you might guess we have a base Backbone view called BaseView which uses the view's template property to render the view).

现在,所有这一切是说,如果你不需要如此强大的安装程序然后要求可能不适合你。在这种情况下,我会做到以下几点:

Now, all that being said, if you don't need such a robust setup then Require may not be for you. In that case I would do the following:


  1. 把所有的模板中的一个或多个HTML文件;他们包装在脚本标记,像这样:

  1. Put all of your templates in to one or more HTML files; wrap them in script tags, like so:

<script id="dummyTemplate" type='text/template'>
<span>I'm a template!</span>
</script>


  • 写在你的服务器端的一些code包括在主HTML文件发送到客户端的HTML文件

  • Write some code on your server-side to include those HTML files in the main HTML file you send to the client

    写一个函数,它接受一个模板ID,获取元素的文本,编译到一个模板,并返回该模板(如果你想也许缓存编译模板......当然,用下划线模板我不认为你甚至不需要编译,所以你可以跳过所有)。

    Write a function which takes a template ID, gets the text of that element, compiles it in to a template, and returns that template (maybe cache the compiled templates if you want ... of course, with Underscore templates I don't think you even need compiling, so you can skip all that).

    使用您的函数来访问你的模板: $(#东西)HTML(templateFunc('dummyTemplate')模板())

    Use your function to access your templates: $("#something").html(templateFunc('dummyTemplate').template())

    这将允许您将模板存储在HTML文件(语法着色),但仍方便地访问他们的JS。您也可以将您的模板尽可能多的文件之间,只要你想,只要​​你可以编写包括逻辑以使其

    This will allow you to store your templates in html files (for syntax coloring), but still access them conveniently in JS. You can also divide your templates between as many files as you want, as long as you can write include logic to bring them in.

    如果您选择要求虽然肯定检查了HBS插件。如果你没有看过把手模板的是,你可能想;他们远比那些下划线更强大的(但像任何好的模板系统,不允许太​​多的逻辑)。

    If you do opt for Require though, definitely check out the HBS plugin. And if you haven't looked at Handlebars templates yet, you might want to; they're far more powerful than Underscore ones (but like any good templating system, don't allow for too much logic).

    这篇关于主要成分:管理模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆