Jade条件语句呈现为纯文本
我正试图有条件地在我的<head>
显示一些标签
代码如下:
<!doctype html> <html ng-app='nodeApp'> <head> if (title) <title>#{title}</title> <meta name="viewport" content="width=device-width, initial-scale=1"> <meta charset="UTF-8"> <meta name='author' content='#{author}'> <meta name='og:title' content='#{title}'> <meta name='description' content='#{description}'> <meta name='og:description' content='#{description}'> <meta name='og:image' content='#{image}'> <meta name="theme-color" content="#3F51B5"> <!-- CSS files are inserted here --> <link rel="stylesheet" href="/static/client.css" type="text/css"> </head>
当我进入我的页面时,文本“if(title)”在页面顶部以纯文本呈现。 这发生在所有的条件中。但是,标题标签是用正确的文本呈现的。
我也试过了
if title <title>#{title}</title>
和
if (title) { <title>#{title}</title> }
在我的后端,我有(整个class级)..
const jade = require('jade'); this.locals = { title: process.env.sitename }; this.header = jade.render(this.header, this.locals);
所以sitename被正确地返回,因为标题被填充,但if语句不做任何事情,只是呈现为纯文本。 思考?
我认为这个问题可能是你正在把普通的html和jade混合在一起。 由于您正在使用常规的html标签来编写if语句,因此已经是一个小孩了。 但是,那么你也使用缩进,它变成了双缩进,玉变得困惑。 在if语句之前不要缩进来试试这个:
<head> if (title) <title>#{title}</title>
或者更好。 始终如一地使用Jade。
你必须为这里的javascript语句预先设置一个连字符。
- var title = true; - if (title) { h1(title='#{title}') My Title - }