客户端使用未知变量进行模板化

时间:2010-04-28 14:11:09

标签: javascript jquery templates client-side

我们公司的内联网由几个填充了变量的电子邮件模板组成(如[[NAME]],[[PROJECT]]等)。我正在考虑实现某种客户端模板,以便更容易用实际值替换这些变量。

问题是,在我到目前为止所有的客户端模板解决方案中,所有这些解决方案似乎都假设JS代码知道标记中存在的所有模板变量,并且它们似乎都不能获取标记中定义的变量列表。

有没有人知道任何使这成为可能的解决方案/插件?

提前致谢!

2 个答案:

答案 0 :(得分:1)

难道你不能只使用一些简单的正则表达式吗?

 var variables = mycontent.match(/\[\[(.*?)\]\]/g);

我设置了demo here,因此您可以看到它的实际效果。

答案 1 :(得分:0)

如果模板集已经缓存在客户端上并且可以按需供客户使用,并且每个模板都有自己的一组信息要替换(硬编码或运行时定义)。然后我们可以去寻找一些通用的解决方案

伪代码

- Decide which Template now need to render
- Send a Ajax command of array of variables in to be replace in the template 
  [{var_name:"%project_name%",var_value:"Project" },{var_name:"%superviser%",var_value:"Its me :)" }]


- Write a generic code that loop through json array and replace the var_name with the template source 
  Check this - http://api.prototypejs.org/language/template/