[英]How to write html from an oEmbed object directly into a jade template?
我有一个对象数组,每个对象包含一个原始HTML值。 原始html是一个oEmbed对象,在单个字符串中包含javascript,css和html。
我想将每个原始html字符串迭代到css flex框中,但似乎无法弄清楚如何做。
<! -- attempt 1 -->
div.container
h2 posts
ul.flex-container
each post in posts
li.flex-item
p!= #{post.html}
<! -- attempt 2 -->
div.container
h2 posts
ul.flex-container
each post in posts
li.flex-item
include content.html #{post.html}
<! -- attempt 3 -->
div.container
h2 posts
ul.flex-container
each post in posts
li.flex-item #{post.html}
尝试#1源自此帖子 。 尝试时,在p!=
行上出现Unexpected token ILLEGAL
错误。
我以为我读过一些东西,说html是玉器的内置过滤器 。 虽然找不到文档中的任何地方。 尝试#2试图实现它,但是我想我需要保存一个.html文件。 目前,html仅存储在变量中。
当我将#{post.title}
替换为#{post.html}
,尝试#3会在页面上呈现某些内容,因此该错误不在each post in posts
功能中的each post in posts
。
玉可以处理直接的html写吗? 我会尝试在函数中使用document.body.innerHTML
更好,然后看看是否可以通过这种方式将其注入flex框中吗?
任何帮助是极大的赞赏!
在阅读了《 Jade Attributes》和《 Jade逻辑教程》中的一些文档之后,希望这个答案对您有帮助:
Node.js
只需调用node server.js
即可查看输出。
文件:sever.js
var jade = require('jade');
var data = [
{"extId":"eg1" , "html":"<div>Everything you want 1<script>alert('hello1');</script></div>"},
{"extId":"eg2" , "html":"<div>Everything you want 2<script>alert('hello2');</script></div>"},
{"extId":"eg3" , "html":"<div>Everything you want 3<script>alert('hello3');</script></div>"},
];
var html = jade.renderFile('testing.jade', {posts : data , pageTitle : 'TestingJade'});
console.log('html : ' , html);
文件:testing.jade
doctype html
html(lang="en")
head
title= pageTitle
body
h1 Jade - node template engine
ul
each post ,index in posts
- var curId = post.extId
li(id= curId)= post.html
阅读我提供的文档,它将帮助您理解。
each post ,index in posts
使用index
, each post ,index in posts
似乎很重要!
之后,我们定义一个变量来处理“ ID”,就像我们可以使用“ attributes ”定义将其设置为标签。
最后,我们使用=
设置内容以转义post.html
<!DOCTYPE html> <html lang="en"> <head> <title>TestingJade</title> </head> <body> <h1>Jade - node template engine</h1> <ul> <li id="eg1"><div>Everything you want 1<script>alert('hello1');</script></div></li> <li id="eg2"><div>Everything you want 2<script>alert('hello2');</script></div></li> <li id="eg3"><div>Everything you want 3<script>alert('hello3');</script></div></li> </ul> </body> </html>
请注意,如果您不想转义post.html
的内容, post.html
使用!=
li(id= curId)!= post.html/*
^
instead of |
v
li(id= curId)= post.html*/
输出应为:
<!DOCTYPE html> <html lang="en"> <head> <title>TestingJade</title> </head> <body> <h1>Jade - node template engine</h1> <ul> <li id="eg1"> <div>Everything you want 1 <script> alert('hello1'); </script> </div> </li> <li id="eg2"> <div>Everything you want 2 <script> alert('hello2'); </script> </div> </li> <li id="eg3"> <div>Everything you want 3 <script> alert('hello3'); </script> </div> </li> </ul> </body> </html>
因此将其转换为您的代码:
<! -- attempt 4 -->
div.container
h2 posts
ul.flex-container
each post, index in posts
li.flex-item= post.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.