ejs icon indicating copy to clipboard operation
ejs copied to clipboard

Add 'literal' tag to make content in the tag pair be taken as literal and not be parsed

Open ifishing opened this issue 12 years ago • 6 comments

When you ejs template contains literal strings <% or %>, it will be recognized as delimiters by ejs engine,where it is not the template author's intention and it probably cause en parse error. We need add support to make some content (especailly string equals to delimiter ) be treated as literal and not parsed by ejs. I add such support as a literal tag.Its usage is as follows:

  <%literal%>
    <span>this span contains delimeters <% %> , it should be wrapped by literal tag</span>
  <%/literal%>
  <script type="text/javascript">
    var template="<%=title%>";
    ...
 </script>

ifishing avatar Apr 23 '13 16:04 ifishing

Don't inline a polyfill for JSON, either make do without JSON.stringify, or require it. We probably have other things that break worse in older browsers anyway, just get people to polyfill it properly.

ForbesLindesay avatar Apr 24 '13 02:04 ForbesLindesay

Although JSON is not a global object of node.js. But node.js is built on V8, which provides the global object JSON. And The definition of the JSON object is part of the ECMAScript 5 specification.So I think threre is no problem if the ejs template runs at Node.js server side. For old browsers , I have check whether the JSON object exists.If it doesn't exists , I provide a alternative method to do json stringify. See the code:

exports.stringifyJSON=function(obj)
{
    if(typeof(JSON)=="object"&&typeof(JSON.stringify)=="function")
        return JSON.stringify(obj);
    else
        return stringify(obj);
}

For the js coding style (curly brace) issue , I will modify the code and pull a request again.

ifishing avatar Apr 24 '13 05:04 ifishing

Yes, older browsrs will need to polyfill JSON. The problem is that they will most likely need JSON for things other than EJS. This change would force even modern browsers to download the shim for JSON. That's a lot of code to support browsers that have a tiny market share.

If you want to support older browssers, just use something like modenizr in your own projects. Don't do it at a library level.

ForbesLindesay avatar Apr 24 '13 12:04 ForbesLindesay

I will remove the stringify method for supporting old browsers in util.js.

ifishing avatar Apr 24 '13 13:04 ifishing

This would be a great addition. Will this be included someday?

trevordixon avatar Aug 16 '13 16:08 trevordixon

Agreed. This would be a seemingly great feature.

inolen avatar Sep 23 '13 18:09 inolen