简体   繁体   中英

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). 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.

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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