简体   繁体   English

客户端模板使用未知变量

[英]Client side templating with unknown variables

Our company has an intranet consisting of several e-mail templates filled with variables (like [[NAME]], [[PROJECT]] and so on). 我们公司的Intranet由几个填充有变量(例如[[NAME]],[[PROJECT]]等的电子邮件模板)组成。 I was thinking of implementing some sort of client side templating to make it easier to replace these variables with actual values. 我正在考虑实现某种客户端模板,以使其更容易用实际值替换这些变量。

The problem is that among all the client side template solutions I've located so far, all of them seem to assume that the JS code knows all the template variables that exist in the markup, and none of them seem to be able to fetch a list of variables defined in the markup. 问题是,到目前为止,在我找到的所有客户端模板解决方案中,所有这些似乎都假设JS代码知道标记中存在的所有模板变量,而且它们似乎都无法获取标记中定义的变量列表。

Does anyone know of any solutions/plugin which makes this possible? 有谁知道有什么解决方案/插件可以做到这一点?

Thanks in advance! 提前致谢!

Can't you just use some simple regex? 您不能只使用一些简单的正则表达式吗?

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

I set up a demo here , so you can see it in action. 在这里设置了一个演示 ,因此您可以看到它的实际应用。

If set of templates are already cached on client and available to customer on demand and each template will have it own set of information to be replaced in (either hardcoded or define on runtime). 如果模板集已经在客户端上缓存并且可以按需提供给客户,并且每个模板将具有其自己的信息集以供替换(硬编码或在运行时定义)。 Then we can go for some generic solution 然后我们可以寻求一些通用的解决方案

Pseudo code 伪代码

- 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/

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

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