mde / ejs

Embedded JavaScript templates -- http://ejs.co
Apache License 2.0
7.75k stars 842 forks source link

why not 'open' and 'close' option? #115

Closed wohugb closed 6 years ago

mde commented 9 years ago

Could you be a little more specific about what this is?

mscdex commented 8 years ago

I think they want to be able to use (completely) custom open and close tags such as '{{' and '}}' respectively, instead of being forced to use angle brackets.

mde commented 8 years ago

I'm confused by these requests -- if people want to use Mustache templates, why not use Mustache templates? What is the use case for this?

geekingin commented 8 years ago

Maybe because i tend to use javascript in templates, while' <%' and '%>' are less elegant : )

mde commented 8 years ago

First of all, optimizing for elegance is what brings you needless layers of abstraction like HAML and CoffeeScript. Elegance is a thing, but it shouldn't be the main thing.

The issue here is that the semantics of these tag systems are totally different. In Mustache, the simple pairs of double brackets represent interpolation (they actually output the value), and various characters inside the left-hand side create different flow-control tags. In EJS, plain open/close tags are only for executing code, not for outputting/interpolating anything. And there aren't any control-flow tags because it uses an actual programming language, instead of inventing a new one.

EJS Mustache
<% %> {{ }}
no output outputs value
EJS Mustache
<%= %> {{# }}
outputs value control flow

These two systems are totally different, and don't map to each other. The previous (v1) of EJS implemented these mustache delimiters by special-casing it in a very naive and barely functional way, with a single hard-coded mapping that assumed all you were doing was interpolation/output. This is not how EJS works.

One possibility might be to allow you to customize not just the delimiter (by default, "%"), but also the tag marker (by default "<"). This would allow people to use whatever combination of these they deem the most "elegant," and wouldn't force a bunch of ugly special casing code into EJS just to allow it to masquerade as a completely different templating system.

Interpolation/output tags would still include the equals sign. They would potentially look like this: {{= }}.

jskrzypek commented 8 years ago

@mde what you describe would be a fantastic addition.

mde commented 8 years ago

Wouldn't let people use Mustache out of the box (which mystifies me; just use Mustache if you like it), but I'd be happy to merge a PR that implements this. Wouldn't be super hard to do.

MartinMouritzen commented 8 years ago

@mde This would be awesome indeed.

calumk commented 7 years ago

Was any progress made on this? My personal reason for wanting a change is just that <%= looks too much like html in my opinion.

Id prefer a [[= or {{= option to my present use of custom delimiters <{=

RyanZim commented 7 years ago

@calumk PR welcome; this isn't going to be something the core team will devote time to implement anytime soon.

RyanZim commented 7 years ago

Proposed PR: https://github.com/mde/ejs/pull/304

gabesumner commented 6 years ago

One other reason to want this is the auto-formatting features of IDE's. Because EJS tags look like an unclosed HTML tag it messes up the indentation of subsequent lines. I'd also be happy to simply have the ability to close the tag. I.E. <% %/> or <% %>... </% %>

Gyomo commented 6 years ago

The use case for me is that my IDE (WebStorm) occasionally complains bitterly when it sees a less than inside of another tag attribute. The fix is of course to put the template in a separate .ejs file and then my IDE mostly recognizes ejs formatting. But it still struggles in some situations.

For example the dynamically generated part of the id here:

There are many, many ways to work around this too, but it would be very, very nice to have something other than the very special less than symbol. (I care nothing for mustache - I love ejs, because it's javascript and I can do anything javascript can do). This is also likely a problem with the idea as other tags (like a div instead of textarea don't care that it's not an 'lvalue') But I don't care that the problem may be with the IDE, I just want to tag marker and make all the various problems go away. :D

crussell42 commented 4 years ago

The use case that is not so edge is where you want to use an IDE that is NOT ejs aware to create ejs. For instance boostrapstudio which only knows how to generate html. Bootstrapstudio only creates html which cant include < or > between html tags. So in bootstrapstudio you create something like this

<span aria-hidden="true">×</span></button><span><strong>Alert</strong>
<%= message %></span>

The html exported now has been escaped between tags to &lt%= message %&gt. So using that base html as an ejs file fails until you convert back to <%= message %>. So if I could change the open and close MonkeyOpen= message MonkeyClose I can do bulk conversion from original html to ejs with less chance of screw up. In other words who is to say that <% will not clash with the namespace of some app. Would be nice to be able to change them since we dont know how or what transformations happen on the way to ejs processing.

mde commented 4 years ago

Incidentally, this was indeed added and merged in PR #304