简体   繁体   English

javascript文件的变色龙模板?

[英]Chameleon templates for javascript files?

I am developing a simple pyramid application where I am using JQuery to do AJAX requests. 我正在开发一个简单的金字塔应用程序,我正在使用JQuery来执行AJAX请求。 I have until now had my javascript code within my chameleon templates. 我到目前为止在变色龙模板中有我的javascript代码。 Now I want to extract my javascript into another location (eg as static resources). 现在我想将我的javascript提取到另一个位置(例如作为静态资源)。

My problem is that I find my javascript code relies on dynamically generated content like so: 我的问题是我发现我的javascript代码依赖于动态生成的内容,如下所示:

$.post("${request.route_url('my_view')}",{'data': 'some data'}, function(html){
    $("#destination").html(html);
});

The dynamic element being: 动态元素是:

"${request.route_url('my_view')}"

Which is calling the route_url method of the request object within the template. 这是在模板中调用请求对象的route_url方法。

Is there a recognised pattern for separating such javascript files into their own templates and providing routes and views for them or do I simply keep my javascript in my page template? 是否有一种公认的模式将这些javascript文件分离到他们自己的模板中并为他们提供路线和视图,或者我只是将我的javascript保存在我的页面模板中?

Yes; 是; you generally put context-specific information like expanded routes into the templates and access this information from your (static) JavaScript libraries. 您通常会将特定于上下文的信息(如扩展路径)放入模板中,并从(静态)JavaScript库中访问此信息。

Including the context info can be done in various ways, depending on taste: 包括上下文信息可以通过各种方式完成,具体取决于品味:

  1. You could use a data attribute on a tag in your generated HTML: 您可以在生成的HTML中的标记上使用数据属性:

     <body data-viewurl="http://www.example.com/route/to/view"> ... </body> 

    which you then, in your static JS code load with the jQuery .data() function : 然后,在您的静态JS代码中加载jQuery .data()函数

     var viewurl = $('body').data('viewurl'); 
  2. Use a made-up LINK tag relationship to include links in the document head: 使用组合的LINK标记关系在文档头中包含链接:

     <head> <link rel="ajax-datasource" id="viewurl" href="http://www.example.com/route/to/view" /> ... </head> 

    which can be retrieved using $('link#viewurl').attr('href') , or $('link[rel=ajax-datasource]').attr('href') . 可以使用$('link#viewurl').attr('href')$('link[rel=ajax-datasource]').attr('href')检索。 This only really works for URL information. 这仅适用于URL信息。

  3. Generate JS variables straight in your template, to be referenced from your static code: 直接在模板中生成JS变量,以便从静态代码中引用:

     <head> ... <script type="text/javascript"> window.contextVariables = { viewurl = "http://www.example.com/route/to/view", ... }; </script> </head> 

    and these variables are referable directly with contextVariables.viewurl . 这些变量可直接与contextVariables.viewurl

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript / Chameleon模板中的转义引号 - Escape quotes in Javascript/Chameleon template 在模板或 HTML 中注入 JavaScript 文件和 webpack - Inject JavaScript files inside Templates or HTML with webpack 如何管理Django模板中Javascript文件的包含 - How to manage the inclusion of Javascript files in Django templates 如何在chameleon / zpt模板(金字塔)中正确包含javascript代码? - How to correctly include javascript code in chameleon/zpt template (pyramid)? Javascript模板 - Javascript templates 如何在Play 2.3.1模板中启用缩小的JavaScript文件? - How to enable minified JavaScript files in Play 2.3.1 templates? XSL:跨XSL模板和文件传递Javascript变量 - XSL: Passing Javascript Variables across XSL Templates & Files Google Closure Templates 为每种语言生成多个 JavaScript 文件,而不是具有单独资源文件的单个 JavaScript 代码库 - Google Closure Templates generates multiple JavaScript files for each language instead of single JavaScript code base with separate resource files 使用javascript将对象绑定到模板 - binding object to templates with javascript Angular模板中的Javascript广告 - Javascript ads in Angular templates
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM