javascript function execution inside jade template

感情迁移 提交于 2019-11-28 08:23:30

You need to define function in the scope of jade, not in JS you generate:

block content
   -  function capitalize(s) { return s.charAt(0).toUpperCase() + s.slice(1); };
  table
    - var list = ['one', 'two']
    - var itemName = 'test test'
    - each item in list
      tr
        td
          a(href="/collection") #{capitalize(itemName)}

but it's probably better to have it outside of template and pass reference to helpers object

I realize this is really old, but when you declare a function in jade, you need to do

script.

not

script

the period makes the difference and will allow jade to pick up that it's in fact a piece of code, rather than HTML.

By this #{capitalize(itemName)} you are trying to call function that is passed to the template from the controller (back-end).

For instance (/routes/index.js):

res.render('index', { title: 'Express test', fs : { echo : lang} });

While in index.jade

 a(href='/register') #{fs.echo('xxx')}

where

lang

is a function defined earlier that takes some parameter.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!