• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
LilyLiya
博客园    首页    新随笔    联系   管理    订阅  订阅
EJS interpolation syntax : pass data to templates
在ejs文件中使用js的数学公式,生成随机数字。 <%= Math.floor(Math.random()*10) + 1 %> res.render('random.ejs', {bubble: num}) <%= bubble %>

refer to: https://www.udemy.com/course/the-web-developer-bootcamp/

https://ejs.co/#docs

Tags

  • <% 'Scriptlet' tag, for control-flow, no output
  • <%_ ‘Whitespace Slurping’ Scriptlet tag, strips all whitespace before it
  • <%= Outputs the value into the template (HTML escaped)
  • <%- Outputs the unescaped value into the template
  • <%# Comment tag, no execution, no output
  • <%% Outputs a literal '<%'
  • %> Plain ending tag
  • -%> Trim-mode ('newline slurp') tag, trims following newline
  • _%> ‘Whitespace Slurping’ ending tag, removes all whitespace after it

example

 embed js in html(.ejs file)

<body>
    <h1>《出现又离开》<%= 0202 + 1%>
    </h1>
    <p>
        试探未知和未来 <br>
        相信那胡言一派 <br>
        当天空暗下来<br>
        当周围又安静起来<br>
        当我突然梦里醒来<br>
        就等着太阳出来<br>
    </p>
</body>

generate random number

random.ejs

<body>
    <h1>Your random number is : <%= Math.floor(Math.random()*10) + 1 %>
    </h1>
</body>
index.js

app.get('/random', (req, res) => { res.render('random.ejs') })

run nodemon index.js. ------>

the normaler way to use math: passing data to templates

index.js

app.get('/random', (req, res) => { const num = Math.floor(Math.random() * 10) + 1 res.render('random.ejs', {bubble: num}) })
random.ejs

<body>
    <h1>Your random number is : <%= bubble %>.  //match the object name defined in index.js
    </h1>
</body>

 

posted on 2021-02-03 08:37  LilyLiya  阅读(86)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3