2

actually i have used functions like as follow

function selectBloodGroupNames()
{
    return array("A+", "O+", "B+", "AB+", "A-", "O-", "B-", "AB-", "A1+Ve", 
    "Unknown");
}

the above function return list of blood group which can be formatted as per our idea like to display in a drop down or else in a table!!!

i have saved that function in a separate file called functions.php

how to use the same like in node.js in express module and in ejs or basic html template engine.

Aravindan
  • 93
  • 9
  • Are you asking how to create and populate an array in JavaScript? Ie `return ['A+', 'O+', 'B+', 'AB+', ...]` – Phil Aug 13 '18 at 06:41
  • no exactly like that, actually i am planning to create function that has to called globally in every pages of project in node js – Aravindan Aug 13 '18 at 06:45
  • So you want a .js file that you can include in every of your other js files, as with PHPs include() ? – Zim84 Aug 13 '18 at 06:47
  • yup, @Zim84 and not only in js, i am using ejs template engine in node js for my project, so i need that idea to be done on the ejs template engine tooo. – Aravindan Aug 13 '18 at 06:50

4 Answers4

2

Full Example with access array in ejs file

data.js

 const sampleArray = {
     function1(){
     return ["A+", "O+", "B+", "AB+", "A-", "O-", "B-", "AB-", "A1+Ve", 
        "Unknown"];
      },
   function2() {
     return ["A+", "O+", "B+", "AB+", "A-", "O-", "B-", "AB-", "A1+Ve", 
        "Unknown"];
     }
    }
module.exports = sampleArray;

main.js //render ejs file.

const  sampleArray = require('./data');
app.get('/renderEjs', function(req, res) {
  res.set('Content-Type', 'application/javascript');
  res.render('ejsHtmlFileName', { myArray :sampleArray.function1(),myArrayTwo:sampleArray.function2()});
});

ejsFile

<div><p><%= myArray %><%= myArrayTwo %></p></div>

Reference Link for render data in ejs file - LINK

IftekharDani
  • 3,619
  • 1
  • 16
  • 21
  • Your ans was exaclty matching my point! But i am using more than one function in the **data.js** and i will use those functions n number of functions in the **viewes.ejs** file, so i cannot define every function during the time of rendering in the **main.js** file! – Aravindan Aug 13 '18 at 07:36
  • 1
    I have update my answer please check..You need like this? – IftekharDani Aug 13 '18 at 07:45
  • The ans was good! your have answered what i expected, but it is like hard coding will not be automated, if there was 10 to 15 functions have to add in the **view.ejs** file! i need to define the 15 function in the **main.js** file during rendering and also i have to do it for many page during rending, This will increase the lines of code! in the **main.js** file! – Aravindan Aug 13 '18 at 08:05
  • 1
    Now second time i have update my answer Please check – IftekharDani Aug 13 '18 at 08:43
  • You are not getting my point @IftekharDani. u are defining for every function! { myArray :sampleArray.function1(),myArrayTwo:sampleArray.function2()} what i am asking is if there are 20 to 25 function how can we define it! thats what my que? for a single page if we define 25 times means! think about other pages! if there are 50 pages, i need to pass 25 functions for each of the 50 pages individually! so a simple code as like php call a line like '' and the functions.php returns those 25 functions for each 50 pages! – Aravindan Aug 13 '18 at 08:49
1

for using in node.js you can create a separate file common.js

 module.exports=function selectBloodGroupNames()
 {
    return ["A+", "O+", "B+", "AB+", "A-", "O-", "B-", "AB-", "A1+Ve", 
    "Unknown"];
 }

then in the file you want to use it you can include it and use it like below:-

   const Common=require('pathto/common.js');
   let bloodGroupList= Common.selectBloodGroupNames();
   //now you can use it where ever you want 
   console.log(bloodGroupList);  
Yogesh.Kathayat
  • 974
  • 7
  • 21
  • what if i have function such as the below, how do it work? `function successMessage($message) { echo '

    '.$message.'

    '; }`
    – Aravindan Aug 13 '18 at 06:55
0

you should install babel to able of use ES6 for javascript

npm install --save-dev babel-core then you create separate file config.js. In config.js :

export default Config {
  selectBloodGroupNames() {
     return ["A+", "O+", "B+", "AB+", "A-", "O-", "B-", "AB-", "A1+Ve", 
    "Unknown"];
  }
}

then in file you want to use function

import Config from ./pathto/config.js;

function functionName () {
  var arr = Config.selectBloodGroupNames();
}
Narendra Jadhav
  • 10,052
  • 15
  • 33
  • 44
chel long
  • 1
  • 1
0

At last i found an solution myself with the answer referencing from @IftekharDani and done some changes myself

functions.js the file which contains multiple functions!!!

module.exports = {
    menuTop: function(){
        var data = '<li>'+
        '<a href="/login">Log in</a>'+
        '</li>'+
        '<li>'+
        '<a href="/register">Register</a>'+
        '</li>';
        return data;
    },

    workCheck: function(user){
        if(user === "OLD")
        {
            var data1 = '<h1>OLD user</h1>';
        }
        else
        {
            var data1 = '<h1>Unknown or new user</h1>';
        }
        return data1;
    }
};

app.js main rendering file where i pass the entire file with the variable to the ejs file

var func = require('./functions');

app.get('/', function (req, res) {
    res.render('home', { funs : func});
});

home.ejs file that contains the function's content.

function 1

<ul class="nav navbar-nav navbar-right">
    <%- funs.menuTop(); %>
</ul>

function 2

<div class="row">
    <%- funs.workCheck("NEW"); %> 
    /* Here i pass parameter to the function that i call from the external file to which make more dynamic. */
</div>

and finally i get the expected result! i will give 50% credits to @IftekharDani for giving me an idea. Thank you!

Aravindan
  • 93
  • 9