简体   繁体   English

Sails.js index.ejs控制器逻辑

[英]Sails.js index.ejs controller logic

I'm a little confused on how to add logic to the home page of my Sails.js app. 我对如何在Sails.js应用程序的首页中添加逻辑有些困惑。 Right now, its just a static page, but I would like to include data on the homepage (index.ejs). 现在,它只是一个静态页面,但是我想在主页(index.ejs)中包含数据。 I have a MainController and I included a index: function that is pulling in the data. 我有一个MainController,并且包括一个index:函数,用于提取数据。 I can't figure out how to configure my route to allow for this. 我不知道如何配置我的路线以允许这样做。

From What you wrote I am guessing you are using Express js and ejs template. 从您写的内容来看,我猜您正在使用Express js和ejs模板。

If you are using the Express render method in your mainController to render your index.ejs you can send the data with response like: 如果在mainController中使用Express渲染方法来渲染index.ejs,则可以使用以下响应发送数据:

    res.render('index', { data: 'my data' }, function(err, html){
        // handle callback and error
    });

so here you have sent some data 所以这里您已经发送了一些数据

then in your index.ejs file you can get data using ejs tags like: 然后在index.ejs文件中,您可以使用ejs标签获取数据,例如:

<div><%= data %></div>

Tell me more about your mainController method if this is not useful 如果这没有用,请告诉我更多有关mainController方法的信息

The static-folder is for static-content only (as the name is telling us). 静态文件夹仅用于静态内容(顾名思义)。 If you want your website to show content dynamically you need to create a controller like that: 如果要让网站动态显示内容,则需要创建一个像这样的控制器:

module.exports = {
    index: function(req, res){
        res.view(); // sending the view in /views/{controller_name}/index.ejs
        return;
    }
}

Cheers! 干杯!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM