Thursday, July 05, 2012

Fiddle with EJS filters

Lets play with EJS filters a bit. Lets pay greetings to the first user in the array of names with a message as "Welcome <firstname> <lastname>"

Lets take the names in an array of objects as below:

var names = [{first: 'John', last: 'Scott'},
             {first: 'Rob', last: 'Miller'}];

Now lets create a filter to create the message, in this filter the first selected object will be passes, which comes from the previous filter "first"

ejs.filters.greeting = function(user){
  return 'Welcome ' + user.first + ' ' + user.last + '';
};

The call to the filter is made in the index.ejs file as below, here insted of first you can can also use last filter

<%=: users | first | greeting %>

Lets see the whole program.

server.js
var express = require('express');
var io = require('socket.io');
var http = require('http');
var ejs = require('ejs');
 
var app = express();
var server = http.createServer(app);
 
var names = [{first: 'John', last: 'Scott'},
             {first: 'Rob', last: 'Miller'}];
 
app.configure(function(){
  app.use(express.bodyParser());
  app.use(app.router);
  app.use(express.static(__dirname + '/'));
  app.set('view engine', 'ejs');
  app.set('view options', {
    layout: false
  });
});
 
ejs.filters.greeting = function(user){
  return 'Welcome ' + user.first + ' ' + user.last + '';
};
 
app.get('/', function(req, res) {
  res.render('index', {
    title : 'Welcome to Express EJS',
    users: names
  });
});
 
server.listen(8080);

index.ejs
<html>
 
<head>
<title><%= title %></title>
</head>
 
<body>
<p><%=: users | last | greeting %></p>
</body>
 
</html>

20 comments:

  1. It’s a very deterministic opportunity until perusal this one you power be musing about sentimental hubbub which goes care of you when you requirement any late night show and can’t monitoring your sentimental of not beget those hot bodies. Gurgaon Escorts

    ReplyDelete
  2. I'll try to make it as you've described it here. Nevertheless, I guess that i might need some help.

    ReplyDelete
  3. Thanks for sharing this unusual information for me. I need to clarify something in order to fully understand this information.

    ReplyDelete
  4. Every one of our models is peasant and charming; some of them offer national services as Travel Delhi Escorts Service

    ReplyDelete
  5. night life and much more. For men who like to be seen out on the town with wonderful models and to appreciate the best encounters that the urban community nightlife brings to the table, we have set up a strong notoriety of being the best Escorts Service in Gurgaon

    ReplyDelete
  6. These escort ladies or girls can aid convalesce mentally. People know that, these escort girls always give their best shot in order to fulfill their client’s requirement.
    Independent Bangalore Escorts
    Female Bangalore Escorts
    Andheri Escorts Service
    Powai Escorts Service
    vashi Escorts Service

    ReplyDelete
  7. Many gorgeous ladies come and join this industry, for their pocket money, but as the industry is prospering in a big way, people are willing to take this as the livelihood. To be one of the VIP Thane Escorts, you have to concentrate only on one thing, and that is the customers’ satisfaction.
    Bandra Escorts Service
    Juhu Escorts Service
    Thane Escorts Service
    Chembur Escorts Service
    Andheri Escorts
    Vashi Escorts

    ReplyDelete
  8. It is a fact that you must always hope for the enjoyment in the best possible form. Most of the hilarious things you like would be those that you will seek to have the finest things to witness ever.
    Thane Escorts
    Juhu Escorts
    Powai Escorts
    Mumbai Escorts Service
    Mumbai Escorts


    ReplyDelete
  9. If you are determined that you will be an escort, then you have to ensure the means of reaching you. If you are a newbie, then you can contact the agency.
    HiFi Independent Bangalore escorts
    Hunting the nearest escort model girl in Andheri Escorts Mumbai
    High-Quelity Female Escorts in Bangalore
    Female Escorts Bangalore

    ReplyDelete
  10. If you are determined that you will be an escort, then you have to ensure the means of reaching you. If you are a newbie, then you can contact the agency.
    HiFi Independent Bangalore escorts
    Hunting the nearest escort model girl in Andheri Escorts Mumbai
    High-Quelity Female Escorts in Bangalore
    Female Escorts Bangalore

    ReplyDelete
  11. Our High-class Escorts service in vashi is the most convenient and most friendly service on the world, and no other escorts agency goes to the same level that we go to make sure great stages of client service.
    AMAIZING BLOGS.-
    Vashi Escorts
    Mumbai Escorts

    ReplyDelete
  12. As independent Powai escorts have passion and fascination for love and amour. They can easily involve in the battle of fun.
    Powai Escorts



    The beautiful and amazing escort girls in Vashi Mumbai
    Vashi Escorts


    Nearest escort model girl in Escorts Mumbai
    Mumbai Escorts

    ReplyDelete
  13. Lots of shy young men are there, who never experienced good dating girls in Bangalore. They find a lot of difficulty in mingling with young delightful and stylish girls. For them, Mushkan is just perfect. expert and personal dating know how to build client contented in their companionship.
    Independent Bangalore Escorts

    ReplyDelete
  14. They can date you at a gorgeous rooftop restaurant in Bangalore or can join you at the most dreamy candle light dinner session. Take her to the apartment house or a five-star hotel in Bangalore, and take pleasure in a night full of fervor and eroticism.
    Female Escorts Bangalore

    ReplyDelete
  15. They will build a bond of open connection with you. They will coddle you with their female gesture. They provide you amazing time on bed with their passionate erotic acts. yet, they can Escort you anywhere you desire. Right from corporate events to educational social gathering – Mumbai Escort girls offer no complaining supplementary services to their customers.
    Mumbai Escorts Service

    ReplyDelete
  16. I am completely Independent and not allied with any Escort Agencies, so you will be dealing with me on a one on one basis at all times, without any complications. I am extremely sexy, very loving and a true elegant girl, something you will appreciate. Very conversational and huge intellect, you will truly appreciate, spending excellence time with me and attractive me out, for more than a date.
    Mumbai Escorts

    ReplyDelete
  17. Sexy body and good-looking personalities of Riya Dubey will make you think naughty and vulgar. These girls have each single thing in their character that all men in this world have ever appreciated for. They possess perfect body – total toned up and seductively curved.
    Powai Escorts Service
    Andheri Escorts Service
    vashi Escorts Service
    Bandra Escorts Service

    ReplyDelete
  18. Most gorgeous Girls come and join this Business, for their pocket money, but as the Company is prospering in a big way, people are willing to take this as the livelihood. To be one of the VIP Escorts, you have to concentrate only on one thing, and that is the customers’ Enjoyment.
    Thane Escorts Service
    Juhu Escorts Service
    Chembur Escorts Service
    Andheri Escorts

    ReplyDelete
  19. My High-Quelity Escorts service is the most convenient and Very friendly service on the world, and no other escorts agency goes to the same level that we go to make sure great stages of Customer service.
    Vashi Escorts
    Thane Escorts
    Juhu Escorts
    Powai Escorts

    ReplyDelete