简体   繁体   English

如何使用javascript中的模板生成RDF / XML?

[英]How to generate RDF/XML using a template in javascript?

I have some working Javascript code that generates an RDF/XML document using variables picked up from HTML fields: 我有一些有效的Javascript代码,它们使用从HTML字段中选取的变量来生成RDF / XML文档:

...
  peopleDoap += "   <foaf:name>" + person_name + "</foaf:name>\n";
  if (person_url != "") {
    peopleDoap += "   <foaf:homepage rdf:resource=\"" + person_url + "/\"/>\n";
  }
  if (person_pic != "") {
    peopleDoap += "   <foaf:depiction rdf:resource=\"" + person_pic + "/\"/>\n";
  }
...

It's hard, looking at this code, to get any sense of what the output will look like (especially as this code is scattered amongst sub functions etc). 很难看清这段代码,以了解输出的外观(尤其是这段代码分散在子函数等中)。

I'm wondering if there is an easy way that would enable me to have something like this: 我想知道是否有一种简单的方法可以使我拥有这样的东西:

...
<foaf:name>%person_name%</foaf_name>
  <foaf:homepage rdf:resource="%person_url%"/>
  <foaf:depiction rdf:resource="%person_pic%"/>
...

And then some substitution code. 然后是一些替换代码。 One slight complication is if fields are left blank, I will want to not generate the whole element. 一个小的复杂之处是,如果将字段留空,我将不希望生成整个元素。 Ie, if person_url='', the above should generate as: 即,如果person_url ='',则上面的代码应生成为:

...
<foaf:name>%person_name%</foaf_name>
  <foaf:depiction rdf:resource="%person_pic%"/>
...

I guess I could do this fairly naively by defining the template as a huge string, then performing a bunch of replaces on it, but is there anything more elegant? 我想我可以通过将模板定义为一个巨大的字符串,然后对它执行一堆替换来相当天真地做到这一点,但是还有什么更优雅的方法吗? Mild preference for native Javascript rather than libraries, but happy to be convinced... 对本机Javascript而非库的偏爱,但很高兴被说服...

(Btw, yes, since this is RDF/XML, maybe there is a smarter way using some kind of RDF library. If you want to address that of the question instead, that's ok with me.) (顺便说一句,是的,因为这是RDF / XML,所以使用某种RDF库可能是一种更聪明的方法。如果您想解决问题,那我也可以。)

Code is here . 代码在这里

Also, this is a widget running on a Jetty server. 另外,这是在Jetty服务器上运行的小部件。 I don't think server-side code is an option. 我不认为服务器端代码是一种选择。

I recommend using: 我建议使用:

  1. jQuery Templates jQuery模板
  2. Mustache 胡子
  3. John Resig's Micro-Templating 约翰·雷格(John Resig)的微型模板

jQuery Templates are very powerful and nicely integrated with jQuery. jQuery模板非常强大,可以很好地与jQuery集成。 That means that you can do things like this: 这意味着您可以执行以下操作:

$.tmpl("Hello ${n}", {n: "World"}).appendTo('h1');

for the most simple stuff, or define templates in your HTML inside special script tags with custom MIME types, compile them, populate them with JSON data from AJAX calls, etc. 以获取最简单的内容,或者在HTML中的模板中使用自定义MIME类型在特殊的脚本标签中进行定义,对其进行编译,使用来自AJAX调用的JSON数据进行填充等。

To add a bit of follow-up, I did implement John Resig's Micro-Templating (actually the refined version I posted earlier). 为了补充一些后续信息,我实现了John Resig的Micro-Templating(实际上是我之前发布的改进版本)。 However, I then backpedalled a bit. 但是,我随后退缩了一点。 I found implementing control structures in the template is less readable than outside: 我发现在模板中实现控制结构比在外部更不易读:

...
 '<description xml:lang="en">@description</description>';
if (homepage) t += 
  '<homepage rdf:resource="@homepage"/>';
...

rather than: 而不是:

...
 '<description xml:lang="en"><#= description #></description>' +
'<# if (homepage) { #>' + 
  '<homepage rdf:resource="<#= homepage =>"/>' +
'<# } #>';
...

I also ditched the microtemplating code for a simple substitution of variables, using @var rather than <# var #>. 我还放弃了微模板代码,使用@var而不是<#var#>来简单地替换变量。

Readability of templates like this is really critical, so I've done everything I could think of. 这样的模板的可读性非常关键,因此我做了我能想到的一切。 In particular, keeping the javascript outside of the template lets syntax highlighting work, which is valuable to me. 特别是,将javascript保留在模板之外可以使语法突出显示起作用,这对我来说很有价值。

That John Resig post also suggested burying the template in your HTML, in a but I preferred to keep it in my javascript, which is a separate .js. John Resig的帖子还建议将模板埋入HTML中,但我更喜欢将其保留在我的JavaScript中,这是一个单独的.js。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM