c# - How to Html escape template fields -


there's few posts including this regarding html escaping not working me. if have simple template such this:

<html><body>$field$</body></html> 

i need field escaped, not whole template. i've created custom render uses system.web.httputility class perform escaping of strings:

class htmlrenderer : iattributerenderer {     public string tostring(object obj, string formatstring, system.globalization.cultureinfo culture)     {         return httputility.htmlencode(             new stringrenderer().tostring(obj, formatstring, culture));     } } 

and sample code render template data:

public static string render() {     var group = new templategroup('$', '$');     group.registerrenderer(typeof(string), new htmlrenderer());     var template = new template(group, "<html><body>$field$</body></html>");     template.add("field", "chalk & cheese");     return template.render(); } 

returns following:

&lt;html&gt;&lt;body&gt;chalk &amp; cheese&lt;/body&gt;&lt;/html&gt; 

which escapes everything.

how can escape fields added template?

i new stringtemplate think have idea on how make work, think there, whats missing format option in stringtemplate. think need this:

<html><body>$field;format="htmltag"$</body></html> 

after tagging string template "htmltag" can register renderer, 1 have done above , check tag follows:

public class htmlrenderer:iattributerenderer {     public string tostring(object obj,string formatstring,cultureinfo culture)     {         if(formatstring=="htmlencode")         { return httputility.htmlencode(obj.tostring()); }         return obj.tostring();     } } 

more information can found here: http://www.antlr.org/wiki/display/st/object+rendering

note: untested , c# not :) hope have pointed right direction.


Comments