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.

4
  • Are you asking how to create and populate an array in JavaScript? Ie return ['A+', 'O+', 'B+', 'AB+', ...] Commented Aug 13, 2018 at 6: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 Commented Aug 13, 2018 at 6:45
  • So you want a .js file that you can include in every of your other js files, as with PHPs include() ? Commented Aug 13, 2018 at 6: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. Commented Aug 13, 2018 at 6:50

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

5 Comments

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!
I have update my answer please check..You need like this?
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!
Now second time i have update my answer Please check
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 '<?php required('functions.php'); ?>' and the functions.php returns those 25 functions for each 50 pages!
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);  

1 Comment

what if i have function such as the below, how do it work? function successMessage($message) { echo ' <div class="alert pastel alert-success" align="center"> <p>'.$message.'</p> </div> '; }
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();
}

Comments

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.