How to use node modules (like MomentJS) in EJS views?

前端 未结 10 1728
耶瑟儿~
耶瑟儿~ 2020-12-14 00:03

To use MomentJS in views/custom.ejs, what is the correct way (if any)?

  1. Server side

    routes/index etc we can easily use require(\'moment\');

10条回答
  •  南笙
    南笙 (楼主)
    2020-12-14 01:01

    I wrote a helpers to return moment for using on ejs view and layouts.

    ./helpers/utils/get-moment.js

    const moment = require('moment');
    
    module.exports = {
        friendlyName: 'formatMoney',
        description: 'format money number.',
        inputs: {},
        sync: true,
        exits: {},
        fn: function (inputs, exits) {
            return exits.success(moment);
        }
    };
    

    Then using:

    const moment = sails.helpers.utils.getMoment();
    

提交回复
热议问题