【问题标题】:How do I include a template in another template in a Dojo Widget?如何在 Dojo Widget 的另一个模板中包含一个模板?
【发布时间】:2017-02-10 17:40:32
【问题描述】:

我尝试使用以下模板模型来做到这一点:

<div>
    <div data-dojo-attach-point="includeHere"</div>
</div>

<div>
    ${includeHere}
</div>

然后在课堂上我认为应该是这样的:

define([
    "dojo/_base/declare",
    "dijit/_WidgetBase",
    "dijit/_TemplatedMixin",
    "dojo/text!./templates/myWidget.html",
    "dojo/text!./templates/templateToBeIncluded.html"
], function (
    declare,
    _WidgetBase,
    _TemplatedMixin,
    template,
    templateToBeIncluded
) {
        return declare([_WidgetBase, _TemplatedMixin], {
            templateString: template,

            buildRendering: function() {
                this.includeHere = templateToBeIncluded;
            },

        });
});

我查看了 _TemplatedMixin 源并尝试根据 _TemplatedMixin 的方式更改 buildRendering 函数:

buildRendering: function() {
    domConstruct.toDom(templateToBeIncluded, this.includeHere);
},

但也无法让它发挥作用。这样做的正确方法是什么?我不想在我的 .js 文件中包含 HTML。我只想在模板中使用 HTML。

【问题讨论】:

  • 为什么需要这个?您是否需要在另一个小部件中包含一个小部件?能帮上忙吗? stackoverflow.com/questions/9306670/…
  • 嗯...我还是从 Dojo 开始的。也许我没有得到做事的“道场方式”。但实际上我的想法是我不需要小部件内的小部件......我只需要一个子模板。但仔细想想,这几乎是一样的,不是吗?嗯...你认为我应该将子模板转换为小部件吗?

标签: javascript templates dojo


【解决方案1】:

我解决了。首先,当我在 buildRendering 方法中简单地引入 this.inherited(arguments); 时,它起作用了,但是 HTML 没有呈现。它在页面中显示为原始 HTML。所以我尝试将模板中的变量改为${!includeHere)。一旦我这样做了,HTML 就会正确呈现。

但是子模板中的替换变量仍然存在问题,没有渲染,所以我将 buildRendering 方法更改为:

buildRendering: function() {
    this.includeHere = string.substitute(templateToBeIncluded, this, (v) => v);
    this.inherited(arguments);
},

一切都很好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-15
    • 2021-06-23
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 2018-02-02
    • 1970-01-01
    • 2016-04-10
    相关资源
    最近更新 更多