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

How to replace filter functionnality #76

Closed
jmcollin78 opened this issue Mar 21, 2015 · 16 comments
Closed

How to replace filter functionnality #76

jmcollin78 opened this issue Mar 21, 2015 · 16 comments

Comments

@jmcollin78
Copy link

@jmcollin78 jmcollin78 commented Mar 21, 2015

Hello,

Due to release upgrade my code don't work anymore due to this piece of code:
ejs.filters.toLocaleDateString = function(d) {...}

I wonder how to replace the filters fonctionnality that was in V1 ?
Have a guide line for doing that ?

Thank's in advance,

Jean-Marc

@mde

This comment has been minimized.

Copy link
Owner

@mde mde commented Mar 21, 2015

It looks like someone has added a custom filter that does nothing but call toLocaleString() on date objects. If this is something you do a lot in your code, you can create a helper method, and load that helper into the locals, so it's available in all your templates. EJS is really just plain executed JS, so you can do anything you'd do in plain vanilla JS.

@jmcollin78

This comment has been minimized.

Copy link
Author

@jmcollin78 jmcollin78 commented Mar 21, 2015

Hi mde !

Thank's for your answer. This helps a lot.

@jmcollin78 jmcollin78 closed this Mar 21, 2015
@TimothyGu

This comment has been minimized.

Copy link
Collaborator

@TimothyGu TimothyGu commented Mar 21, 2015

Using locals does bring some performance hit, as they are called at render time rather than compile time, like now-removed filters are.

@IgorDePaula

This comment has been minimized.

Copy link

@IgorDePaula IgorDePaula commented Mar 26, 2015

I received thie error

error

@mde

This comment has been minimized.

Copy link
Owner

@mde mde commented Mar 26, 2015

Filters were removed in v2; they weren't part of core EJS functionality, and they're easy to implement with your own helpers.

@IgorDePaula

This comment has been minimized.

Copy link

@IgorDePaula IgorDePaula commented Mar 26, 2015

How I implement a custom helper?

2015-03-26 16:14 GMT-03:00 Matthew Eernisse notifications@github.com:

Filters were removed in v2; they weren't part of core EJS functionality,
and they're easy to implement with your own helpers.


Reply to this email directly or view it on GitHub
#76 (comment).

@IgorDePaula

This comment has been minimized.

Copy link

@IgorDePaula IgorDePaula commented Mar 26, 2015

When I use app.locals the ejs engine throw error because he dont recognize
the tag <%- %>, if I use <%= the function body is printed on screen.

2015-03-26 16:20 GMT-03:00 principe.borodin@gmail.com <
principe.borodin@gmail.com>:

How I implement a custom helper?

2015-03-26 16:14 GMT-03:00 Matthew Eernisse notifications@github.com:

Filters were removed in v2; they weren't part of core EJS functionality,
and they're easy to implement with your own helpers.


Reply to this email directly or view it on GitHub
#76 (comment).

@mde

This comment has been minimized.

Copy link
Owner

@mde mde commented Mar 26, 2015

Write a JavaScript function, and pass it in with your local vars, or make an object with all your helper functions on it, or run your EJS in a context that get a reference to some helper object you've created. Ex:

var people = ['geddy', 'neil', 'alex'],
    helperFunc = function (inputString) { return inputString.toUpperCase(); },
    html = ejs.render('<%= helperFunc(people.join(", ")); %>', {people: people, helperFunc: helperFunc});
@mde

This comment has been minimized.

Copy link
Owner

@mde mde commented Mar 26, 2015

That's not a valid tag. Here are the docs: http://ejs.co/#docs

@IgorDePaula

This comment has been minimized.

Copy link

@IgorDePaula IgorDePaula commented Mar 26, 2015

The docs says that is a valida tag yeah
Tags

  • <% 'Scriptlet' tag, for control-flow, no output
  • <%= Outputs the value into the template (HTML escaped)
  • <%- Outputs the unescaped value into the template
  • <%# Comment tag, no execution, no output
  • <%% Outputs a literal '<%'
  • %> Plain ending tag
  • -%> Trim-mode ('newline slurp') tag, trims following newline

I'll try again..

thanks for your attention

2015-03-26 16:37 GMT-03:00 Matthew Eernisse notifications@github.com:

That's not a valid tag. Here are the docs: http://ejs.co/#docs


Reply to this email directly or view it on GitHub
#76 (comment).

@mde

This comment has been minimized.

Copy link
Owner

@mde mde commented Mar 26, 2015

Oop, sorry, distracted, totally misread your code. <%- is indeed a valid opening tag. It just doesn't escape the output.

@IgorDePaula

This comment has been minimized.

Copy link

@IgorDePaula IgorDePaula commented Mar 26, 2015

My index.ejs is :

Welcome to <%= title %>

<%= menuMounter()%>

In app.js is:

var ejs = require('ejs');

app.locals.menuMounter = function menuMounter() {
var menu = require('./config/menu.json');
var template = fs.readFileSync('./lib/menumounter/menu.ejs', 'utf-8');
return ejs.render(template, menu);
};

I received this error:

SyntaxError: C:\Users\Analista-02\projetos\teste-express\views\index.ejs:10
8|

<%= title %>


9|

Welcome to <%= title %>

10| <%= menuMounter()%>
11|
12|
13|

Unexpected token % while compiling ejs

2015-03-26 16:58 GMT-03:00 Matthew Eernisse notifications@github.com:

Oop, sorry, distracted, totally misread your code. <%- is indeed a valid
opening tag. It just doesn't escape the output.


Reply to this email directly or view it on GitHub
#76 (comment).

@IgorDePaula

This comment has been minimized.

Copy link

@IgorDePaula IgorDePaula commented Mar 26, 2015

Sorry, I start understand now. Sorry again.

2015-03-26 17:08 GMT-03:00 principe.borodin@gmail.com <
principe.borodin@gmail.com>:

My index.ejs is :

Welcome to <%= title %>

<%= menuMounter()%>

In app.js is:

var ejs = require('ejs');

app.locals.menuMounter = function menuMounter() {
var menu = require('./config/menu.json');
var template = fs.readFileSync('./lib/menumounter/menu.ejs', 'utf-8');
return ejs.render(template, menu);
};

I received this error:

SyntaxError: C:\Users\Analista-02\projetos\teste-express\views\index.ejs:10
8|

<%= title %>


9|

Welcome to <%= title %>

10| <%= menuMounter()%>
11|
12|
13|

Unexpected token % while compiling ejs

2015-03-26 16:58 GMT-03:00 Matthew Eernisse notifications@github.com:

Oop, sorry, distracted, totally misread your code. <%- is indeed a valid
opening tag. It just doesn't escape the output.


Reply to this email directly or view it on GitHub
#76 (comment).

@IgorDePaula

This comment has been minimized.

Copy link

@IgorDePaula IgorDePaula commented Mar 26, 2015

I do the function work, but I can't use it recursively. Any suggestions?

2015-03-26 17:28 GMT-03:00 principe.borodin@gmail.com <
principe.borodin@gmail.com>:

Sorry, I start understand now. Sorry again.

2015-03-26 17:08 GMT-03:00 principe.borodin@gmail.com <
principe.borodin@gmail.com>:

My index.ejs is :

Welcome to <%= title %>

<%= menuMounter()%>

In app.js is:

var ejs = require('ejs');

app.locals.menuMounter = function menuMounter() {
var menu = require('./config/menu.json');
var template = fs.readFileSync('./lib/menumounter/menu.ejs', 'utf-8');
return ejs.render(template, menu);
};

I received this error:

SyntaxError: C:\Users\Analista-02\projetos\teste-express\views\index.ejs:10
8|

<%= title %>


9|

Welcome to <%= title %>

10| <%= menuMounter()%>
11|
12|
13|

Unexpected token % while compiling ejs

2015-03-26 16:58 GMT-03:00 Matthew Eernisse notifications@github.com:

Oop, sorry, distracted, totally misread your code. <%- is indeed a
valid opening tag. It just doesn't escape the output.


Reply to this email directly or view it on GitHub
#76 (comment).

@mde

This comment has been minimized.

Copy link
Owner

@mde mde commented Mar 26, 2015

We should probably open a new ticket for this. This ticket has nothing to do with this issue, and has been closed.

@IgorDePaula

This comment has been minimized.

Copy link

@IgorDePaula IgorDePaula commented Mar 26, 2015

Ok,I resolved already. Thnaks.

2015-03-26 17:44 GMT-03:00 Matthew Eernisse notifications@github.com:

We should probably open a new ticket for this. This ticket has nothing to
do with this issue, and has been closed.


Reply to this email directly or view it on GitHub
#76 (comment).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.