PUG(以前称为JADE)变量在锚点HREF内无法正常工作(插值) [英] Pug (formerly Jade) Variables Not Working (Interpolating) Correctly Inside Anchor Href

查看:21
本文介绍了PUG(以前称为JADE)变量在锚点HREF内无法正常工作(插值)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node和Express,并且使用PUG(以前称为JADE)模板引擎来呈现我的html。在我开始尝试将变量注入我的锚链接的href之前,一切都运行得很好。奇怪的是,如果我将Express应用程序view engine更改为jade,事情就会按预期开始工作。

根据other articles我读到的信息,该问题似乎是插值问题,但是我似乎找不到说明如何正确修复此问题的资源或文档。

例如

我从roomsjson数组中拉入数据,然后使用for循环遍历每个数组元素并输出每个房间的数据。使用jade可以进行以下工作。

table.table.table-striped
  thead
    tr
      th Name
      th Id
  tbody
    each room in rooms
      tr
        td(style="width: 50px;")
          a(href!="/admin/rooms/delete/#{room.id}") Delete
        td #{allTitleCase(room.name)}
        td #{room.id}

使用pug以上不能正常工作。具体地说,a(href='/admin/rooms/delete/#{room.id}') Delete链接不能正常工作。它不是将房间ID注入链接href,而是直接输出#{room.id}作为href链接的末尾。

有什么想法可以在pug中修复此问题吗?

请注意,我已使用pug尝试了以下所有操作,但这些选项都不起作用。

  • a(href="/admin/rooms/delete/#{room.id}") Delete
  • a(href!="/admin/rooms/delete/#{room.id}") Delete

推荐答案

更新:PUG2.0引入了对插值处理方式的突破性更改。

基于the changelog,下列任何都应该起作用:

// to solve OP's problem, the following can be used:
a(href="/admin/rooms/delete/" + room.id) Delete

// Here are a few additional scenarios which use the new API 
// for anyone stumbling across this answer in the future
- var href = "/admin/rooms/delete/" + room.id;
a(href=href)
a(href=`${href}`) // Node.js/io.js ≥ 1.0.0
a(href=href + '?foo=bar')

这篇关于PUG(以前称为JADE)变量在锚点HREF内无法正常工作(插值)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆