【问题标题】:Handlebars - inline decode context and fill the templateHandlebars - 内联解码上下文并填充模板
【发布时间】:2019-04-05 19:59:17
【问题描述】:

我有一个包含 JSON 但已编码的 json。 容器 2 中的数据字段是一个字符串。我想使用 JSON 中的这些数据和其他字段来填充模板。

var data = {
      "containers": [
        {
          "containerId": 1,
          "containerLabel": "1",
          "dataType": "URL",
          "data": "http://www.google1.com",
          "dataEncoding": null,
          "startDTS": "2019-04-03T10:41:04.570Z",
          "endDTS": "2025-01-01T18:29:59.999Z",
          "containerAnalyticsData": {
            "variationId": "563601179",
            "actionBlockId": "29408_563601179ActionBlock_0",
            "campaignId": 29408,
            "containerId": "1",
            "controlGroupId": "23517",
            "treatmentId": "8f3b53a9-1a7e-4fbe-b28f-450fa88ab474"
          }
        },
        {
          "containerId": 2,
          "containerLabel": "2",
          "dataType": "application/json",
                  "data": "{\"cardType\":\"123Stock\",\"cardTypeID\":5,\"cardID\":\"/content/help/en/ccx/v1/stock/width/2/stock-search\",\"cardName\":\"123 Stock\",\"displayTemplate\":\"123Stock\",\"width\":2,\"backgroundImage\":\"https://helpx.123.com/content/dam/help/en/ccx/stock/stock-june2017-2w-730x280.jpg\",\"backgroundFillColor\":\"\",\"invertPresentation\":false,\"overlayTintColor\":\"\",\"overlayTintPercentage\":0.0,\"priority\":1,\"cardLabel\":\"GET TEN FREE IMAGES FROM 123 STOCK\",\"displayText\":\"Get 10 free 123 Stock images.\",\"displayTextAlignment\":\"center\",\"bodyCopy\":\"\",\"bodyCopyAlignment\":\"left\",\"ctaLabel\":\"Go\",\"ctaAlignment\":\"right\",\"secondaryCTALabel\":\"\",\"secondaryCTAAlignment\":\"right\",\"actionURL\":\"https://stock.123.com/search?k=\",\"urlLinkType\":\"external\",\"defaultURL\":\"https://stock.123.com\",\"urlAppendAnalyticsParams\":true,\"urlApply123Authentication\":true,\"footnote\":\"\",\"searchLabel\":\"\"}",
          "dataEncoding": null,
          "startDTS": "2019-04-03T10:41:04.493Z",
          "endDTS": "2025-01-02T07:59:59.999Z",
          "containerAnalyticsData": {
            "variationId": "563597567",
            "actionBlockId": "28018_563597567ActionBlock_0",
            "campaignId": 28018,
            "containerId": "2",
            "controlGroupId": "",
            "treatmentId": "PR-91a1350b-1f86-46f4-8193-0e06fbc9412d"
          }
        }]
    };

有没有办法在线解码这些数据并填充模板?

这是具有填充模板逻辑的小提琴链接:http://jsfiddle.net/agoyal/38goqau5/6/

编辑:
结果对象应具有 JSON 格式的内部数据元素,而不是字符串。

我快到了。我想构建自己的对象,但是有一个小问题我现在必须使用 data.data 而不仅仅是数据。如何通过修改当前上下文返回整个对象?
看到这个小提琴:jsfiddle.net/agoyal/0fwm768n/29

【问题讨论】:

  • 您可以为data 字段提供register a helper 函数(例如解析),该字段可以获取其值并将其解析为JSON,然后将该JSON 转换为适当的HTML,然后作为输出返回.那你就用{{{parse data}}}吧。
  • 您还可以在将值传递给 Handlebars 进行进一步处理之前,转换所有对象中 data 字段的值(使其成为正确的 JSON)。

标签: javascript handlebars.js


【解决方案1】:

您可以做一个帮助器,将您的字符串解析为 JSON。

看看这个fiddle

<div>
   {{data.cardID}}
</div>


Handlebars.registerHelper('eachJson', function(context, options) {
    let returnValue = ""

  $.each(context, function() {
    if (this.dataType === "application/json") {
      const json = JSON.parse(this.data)

      returnValue += options.fn({
        //Return the whole JSON
        data: json
      });      
    } else {
        returnValue += options.fn({
                data: this.data
      })
    }
  })

    return returnValue;

})

编辑:

【讨论】:

  • 这是一个很好的尝试,但它会丢失原始上下文,即带有父元素键“数据”的 JSON。
  • 但是您不能将 JSON 对象输出为 HTML。以太你在帮助器的返回中构建你的新对象,或者你可以返回整个 JSON 并在 HTML 中选择你想要的数据。检查我的编辑...
  • 我快到了。我想构建自己的对象,但有一个小问题我现在必须使用 data.data 而不仅仅是数据。如何通过修改当前上下文返回整个对象?看到这个小提琴:jsfiddle.net/agoyal/0fwm768n/29
  • 您可以随时将新数据的属性更改为 value,如果感觉更好,您将获得 value.data。
  • 那么,没有内联方式来改变当前上下文,但唯一的解决方案是创建一个新对象?
猜你喜欢
  • 2013-10-23
  • 2016-12-12
  • 2014-02-25
  • 2019-03-02
  • 1970-01-01
  • 2010-10-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-24
相关资源
最近更新 更多