How to create global variables accessible in all views using Express / Node.JS?

前端 未结 8 1059
半阙折子戏
半阙折子戏 2020-11-28 01:53

Ok, so I have built a blog using Jekyll and you can define variables in a file _config.yml which are accessible in all of the templates/layouts. I am currently

8条回答
  •  独厮守ぢ
    2020-11-28 02:01

    With the differents answers, I implemented this code to use an external file JSON loaded in "app.locals"

    Parameters

    {
        "web": {
            "title" : "Le titre de ma Page",
            "cssFile" : "20200608_1018.css"
        }
    }
    

    Application

    var express     = require('express');
    var appli       = express();
    var serveur     = require('http').Server(appli);
    
    var myParams    = require('./include/my_params.json');
    var myFonctions = require('./include/my_fonctions.js');
    
    appli.locals = myParams;
    

    EJS Page

    
    
    
        
        <%= web.title %>
        
    
    
    
    
    

    Hoping it will help

提交回复
热议问题