Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Whitespace Slurping with expression/output tag'<%=, <%-'. #289

Closed
bhaskarmelkani opened this issue Jul 31, 2017 · 6 comments
Closed

Whitespace Slurping with expression/output tag'<%=, <%-'. #289

bhaskarmelkani opened this issue Jul 31, 2017 · 6 comments

Comments

@bhaskarmelkani
Copy link

bhaskarmelkani commented Jul 31, 2017

Feature: Add support for having expression/output tags which support removing white space before them.

Description: Need expressions tag which can also remove the white space before the tag.
Its a combination of <%= and <%_.

Tags Introduced:

  • <%=_ Outputs the value into the template (escaped), strips all whitespace before it
  • <%-_ Outputs the unescaped value into the template, strips all whitespace before it

Use case:
I want to write markdown inside the EJS template.
Since markdown heavily relies on indentation(and whitespaces), I want my EJS code to be indented in itself but not adding extra white space to markdown.

Example

For generating a markdown like following.

Heading

  • No: 1
  • No: 2

Basic implementation.

<% if(condition) {%>
<%= '**Heading**' %>
    <% [1,2].forEach((i) => {%>
<%=  '* No: '+i %>
    <% }) %>
<% } %>

To write indented code.

Implementation with existing tag('Whitespace Slurping' ).

<% if(condition ) {%>
    <%_ %><%= '**Heading**' %>
    <% [1,2].forEach((i) => {%>
        <%_ %><%=  '* No: '+i %>
    <% }) %>
<% } %>

Implementation with new tag.

<% if(condition ) {%>
    <%=_ '**Heading**' %>
    <% [1,2].forEach((i) => {%>
        <%=_  '* No: '+i %>
    <% }) %>
<% } %>

Proposal:
#PR 290

@User4martin
Copy link

Nice idea.

But it may break existing templates:
_ is a valid variable name.

So there may be existing templates, that contain <%=_ %> and the would currently insert the value of _.

Of course (almost) any change to the tokens could break existing code.
So they either need to be optional, or added with a change of the major version number (which allows breaking changes, if they are wanted)

<%_= may be save, because afaik no javascript expression can start with an = sign.
But <%_- is not save, because -4 is a valid expression.

Also there is the question @mde if it is generally more desirable to keep it to single modifier chars.

To avoid breaking changes, there are chars that can (afaik) not be at the start of any javascript expression: ^ & # * =
So those could be used. (Either as new single char, or as 2nd char)

@RyanZim
Copy link
Collaborator

RyanZim commented Jul 31, 2017

This brings up the question that has never really had a definite decision:

Should EJS support whitespace-sensitive stuff like markdown, yaml, etc?

The fact is, EJS was designed primarily for HTML, which is not whitespace-sensitive. Sometimes I feel even the whitespace tags we do have add too much complexity. Do we add this? Then do we add the next whitespace-only request we get? Where do we stop? How many opening/closing tags are too many in the interests of keeping EJS simple?

CC: @mde @User4martin @TimothyGu

@User4martin
Copy link

The alternative are

  • once plugins block/mixin (again) #274 are supported, this can be done as a plugin.
  • If your tag needs to insert at the start of the line <%= '\n** Heading' %>
  • look at rmWhitespace option. Though that may do too much for you.

@bhaskarmelkani
Copy link
Author

bhaskarmelkani commented Aug 4, 2017

@User4martin
I got your point and yeah it will be a breaking change. So to avoid it.
Since caret ^ is used in regex for matching from beginning and also in vim convention its a way to go to the start of line so I can use it in my tag for the same purpose.
So, if I change the tags to be <%^= and <%^-.
It will solve the problem and will not introduce a breaking change. :)

What do you say ?

@RyanZim
Copy link
Collaborator

RyanZim commented Aug 4, 2017

That does look weird, though. @mde Is this a feature we want?

@mde
Copy link
Owner

mde commented Mar 29, 2018

Definitely seems more reasonable to provide hooks to this for a plugin. We already have too many different permutations of whitespace stripping or formatting directly in EJS. Rather than building more and more facilities for handling formatting, we should be making it easy for people to offload that formatting to their chosen tools. I'm going to close this issue now, although I would love some input on an API for making plugins easy to use.

@mde mde closed this as completed Mar 29, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants