从 MongoDB 中检索 HTML 以用于模板 [英] Retrieving HTML from MongoDB for use in Template

查看:21
本文介绍了从 MongoDB 中检索 HTML 以用于模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Meteor.js 和 MongoDB 的新手,所以这个问题可能有一个明显的解决方案,但到目前为止我的搜索没有结果.

我的第一个 Meteor 项目是一个非常简单的博客.在 MongoDB 中,我有以下内容:

 Blog.insert({作者:在这里命名",title: "这里的标题",headerHTML: "这是我的<b>非常</b>第一篇博文.",bodyHTML:是什么驱使我们<em>解决</em>这些类型的问题?",日期:新日期()});

然后在 blog.js 中我有:

 if (Meteor.isClient) {Meteor.subscribe("博客");Template.posts.entry = function () {返回博客.find({});};}

最后在我的 HTML 中,我有以下内容

<代码> ...<div class="row">{{>帖子}}

...<模板名称=帖子"><div class="span12">{{#每个条目}}{{作者}}{{日期}}{{标题}}{{headerHTML}}{{bodyHTML}}{{/每个}}

当我让应用程序运行 {{headerHTML}} 和 {{bodyHTML}} 指定的部分时,返回文字字符串.所以你会看到文本中的标签.我想要的是将字符串视为 HTML 并按原样显示.所以有些文字会被加粗,我可以有链接等......有人可以抛出我的任何智慧吗?

我尝试将把手放在各种 HTML 标签中(例如 <p>{{bodyHML}}</p>),但没有成功.

解决方案

使用三个括号 {{{ }}} 告诉 Metere 不要转义你的 html 字符串.

 {{{headerHTML}}}{{{bodyHTML}}}

I'm new to Meteor.js and MongoDB so this question might have an obvious solution that I am missing but so far my searches have turned up nothing.

My first Meteor project is a very bare-bones blog. In the MongoDB I have the following:

    Blog.insert({
      author: "Name Here",
      title: "Title Here",
      headerHTML: "This is my <b>very</b> first blog post.",
      bodyHTML: "What drives us to <em>solve</em> these types of problems?",
      date: new Date()
    });

Then in blog.js I have:

    if (Meteor.isClient) {
        Meteor.subscribe("blog");
        Template.posts.entry = function () {
          return Blog.find({});
        };
    }

And finally in my HTML I have the following

    ...
    <div class="row">
        {{> posts}}
    </div>
    ...
    <template name="posts">
      <div class="span12">
      {{#each entry}}
        {{author}}
        {{date}}
        {{title}}
        {{headerHTML}}
        {{bodyHTML}}
      {{/each}}
      </div>
    </template>

When I have the app running the sections specified by {{headerHTML}} and {{bodyHTML}} return the literal string. So you see the tags in the text. What I want is for the string to be treated as HTML and displayed as such. So some text will be bolded, I could have links, etc... Any wisdom someone can throw my way?

I've tried putting the handlebars in various HTML tags (like <p>{{bodyHML}}</p>) with no luck.

解决方案

Use three brackets {{{ }}} to tell meteor not to escape your html strings.

 {{{headerHTML}}}
 {{{bodyHTML}}}

这篇关于从 MongoDB 中检索 HTML 以用于模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆