素材牛VIP会员
express.js +html 模板 生成和load之后能否根据传入的locals参数实现动态视图
 23***om  分类:Node.js  人气:1094  回帖:1  发布于6年前 收藏

express.js render的模板里 配置如下

 app.configure(function () {   
app.set('view engine', 'html');   
app.set('views', path.join(__dirname, 'views'));   
app.register('.html', require('ejs'));   
app.use(express.bodyParser({
        uploadDir: config.upload_dir,
        keepExtensions: true,
        limit: 10000000, 
        defer: true  //enable event    }));

render的函数调用 res.render('topic/edit', {tags: tags, topic_type:'话题'});

想调用如下函数,显然需要load完成之后才能调用? 能否/应该如何在html模板中调用下列函数呢?

    function dynamicview()   
{
 if (!locals.topic_type ||locals.topic_type=='话题')
    {
  document.getElementById("needDiv").style.display="none";
 document.getElementById("maxnumber").style.display="compact";
document.getElementById('type_0').checked = true;
    }
    else if(locals.topic_type=='发送')
  {
..............
}

讨论这个帖子(1)垃圾回帖将一律封号处理……

Lv2 入门
请***丶 Linux系统工程师 6年前#1

你的模板是纯HTML?为什么不用模板引擎呢?在render阶段就把不必要显示的DOM节点过滤掉了,不需要再等到页面加载完毕之后再调用 js 来隐藏。

jadeejs之类的都可以在模板里面写简单的逻辑,当然就有条件判断了。拿jade举例吧:

if !topic_type || topic_type === '话题'
  div#maxnumber
  input(type="checkbox", checked)#type_0
else
  div#needDiv
 文明上网,理性发言!   😉 阿里云幸运券,戳我领取