【问题标题】:How to get an iframe to render in pug from a json payload?如何从 json 有效负载中获取 iframe 以在 pug 中呈现?
【发布时间】:2018-04-04 22:22:40
【问题描述】:

问题是我的 iframe 被渲染为文本而不是 iframe。这是他的有效载荷

 var payload = {
  'iat': now,
  'nbf': now,
  'exp': now + 86400, //one day from now.
  'dashboard': 224277,
  'organization': 19190,
  'env': {"MYVAR": 42}
};

var token = jwt.encode(payload, ORGANIZATION_SECRET, 'HS256');
var src = 'iframe#dash.embedf(src=\'' + BASE_URL + '/' + token + '\')';
console.log(src);
res.render("pages/sponsorship", {frame: src});
});

这是哈巴狗模板,赞助。

#container
  .row
    #chart-container

include ../../partials/footer

  script
    | var src  = !{JSON.stringify(frame)}
    | document.getElementById('chart-container').innerHTML = (src);

浏览器中的输出是原始文本,而不是渲染 iframe。

pic of output in browser

【问题讨论】:

    标签: node.js express iframe pug


    【解决方案1】:

    您要求浏览器编译 pug 代码,但它没有这样做。相反,将 src 变量更改为使用标准 html:

    var payload = {
      'iat': now,
      'nbf': now,
      'exp': now + 86400, //one day from now.
      'dashboard': 224277,
      'organization': 19190,
      'env': {"MYVAR": 42}
    };
    
    var token = jwt.encode(payload, ORGANIZATION_SECRET, 'HS256');
    var src = '<iframe id="dash" class="embedf" src="' + BASE_URL + '/' + token + '"></iframe>';
    console.log(src);
    res.render("pages/sponsorship", {frame: src});
    });
    

    【讨论】:

    • 它有效。谢谢!我以为 pug 会将 pug 代码呈现为浏览器的 HTML?
    • 对哈巴狗来说,它只是一个包含字符串的变量。在页面加载到浏览器中之前,您设置元素内部 html 的脚本不会运行。
    猜你喜欢
    • 2020-10-08
    • 1970-01-01
    • 1970-01-01
    • 2021-01-21
    • 1970-01-01
    • 2017-05-18
    • 1970-01-01
    • 1970-01-01
    • 2019-03-27
    相关资源
    最近更新 更多