views:

38

answers:

2

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!

A: 

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/
Ifi
+1  A: 

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.

fudgey
That looks great! I'll give it a whirl.
Eirik Johansen
Worked like a charm! For those who are interesed, I've modified the example to do exactly what I needed: http://jsfiddle.net/KMVRJ/5/
Eirik Johansen