【问题标题】:Handlebars sanitize string - SyntaxError: unterminated string literalHandlebars sanitize string - SyntaxError: unterminated string literal
【发布时间】:2020-06-05 01:36:15
【问题描述】:

我目前正在使用 Publii CMS 开发一个使用 Handlebars 的应用程序。我正在positions.hbs 文件中创建一个 Vue 应用程序。我正在使用 Handlebars 从 CMS 检索一个长字符串以创建 JSON 对象。

{{#getPostsByTags 30 "jobs" " "}}
  jobsData.push(
      {
        "title": "{{ title }}",
        "url": "{{ url }}",
        "mainTag": "{{ mainTag.name }}",
        "description": "{{ mainTag.description }}"
      }
    )
{{/getPostsByTags}}

{{ mainTag.description }} 抓取存储在 CMS 中的文本并在控制台中引发错误。

SyntaxError: 未终止的字符串文字

我可以从第 45 行看到可能有新的行或字符会引发错误。在该行上,我可以看到字符串如下。

在将 Handlebars 中的不需要的字符传递给 description: 值时,如何清理或删除它?我尝试过的一件事是使用 JSON.stringify 没有成功。

 "description": "JSON.stringify({{ mainTag.description }})"

[了解更多]

我该如何清理该字符串?还是我必须从 CMS 解决这个问题?我只存储包含<p> 标签的文本。我该如何解决这个问题?然后我将数据传递给 Vue 应用程序,这里是完整代码。

<script>
    let jobsData = [];

    {{#getPostsByTags 30 "jobs" " "}}
      jobsData.push(
          {
            "title": "{{ title }}",
            "url": "{{ url }}",
            "mainTag": "{{ mainTag.name }}",
            "description": "{{ mainTag.description }}"
          }
        )
    {{/getPostsByTags}}

    window.onload = function () {
        var app = new Vue({
          delimiters: ['${', '}'],
          el: '#app',
          data: {
            jobs: jobsData
          },
          computed: {
            sectionTitlesWithJobs: function () {
              let tags = [];
              this.jobs.forEach(job=> {
                tags.push(job.mainTag)
              })

              tags = tags.filter((a, b) => tags.indexOf(a) === b)
              return tags.sort();
            }
          }
        });
    }
</script>

这是有问题的字符串的样子:

"&lt;p&gt;The Product team conducts user research, creates user experience, and works closely with the internal and vendor technology teams to prototype, develop, and maintain evolving digital tools. The team also leads outreach, training and customer service for the &lt;/a href&#x3D;&quot;https://www1.nyc.gov/site/opportunity/portfolio/products.page&quot;&gt;products in its portfolio&lt;/a&gt;. The public-facing products include &lt;a href&#x3D;&quot;https://access.nyc.gov/&quot;&gt;ACCESS NYC&lt;/a&gt;,
        &lt;a href &quot;https://growingupnyc.cityofnewyork.us/&quot;&gt;Growing Up NYC&lt;/a&gt;, and &lt;a href&#x3D;&quot;https://growingupnyc.cityofnewyork.us/generationnyc/&quot;&gt;Generation NYC&lt;/a&gt; and their related APIs.&lt;/p&gt;&lt;p&gt;The Product team works closely integrated with other NYC Opportunity teams, including the development, design, and data teams. It also engages with other program and technology staff across City government. The team helps NYC Opportunity and our agency partners translate anti-poverty program and policy goals into digital strategies. The team also helps define how the City of New York uses modern, agile and user-centric approach to technology products, including offering product management best practices. Working as part of the team is an opportunity to continue to build a model for in-house product development and serve as national leader in digital government innovation.&lt;/p&gt;"

【问题讨论】:

  • 这取决于字符串。您已在屏幕截图中剪切了有问题的部分。让我们看看行的结尾。 Console.log 返回的字符串。
  • @JoshWulf 我用字符串更新了帖子。

标签: javascript vue.js handlebars.js


【解决方案1】:

这将删除换行符:

mainTag.description.split('\n').join('')

【讨论】:

  • ```解析 index.hbs 文件时发生错误 (1002)。第 21 行的解析错误:...mainTag.description.split('\n').join('') ------------------------------------^期待'ID',得到'INVALID'````
  • 已更新。试试看。这是删除换行符所需执行的操作。
  • {{ description }} 不起作用,因为这是一个车把分隔符并且无法识别 description 是什么。我也试过${sinitizedDescription},但没用。
  • 在这种情况下,请在靠近系统其他边界的地方进行消毒。一个边界是视图层,另一个是数据层。您可以在加载时转换数据吗?
猜你喜欢
  • 1970-01-01
  • 2018-04-26
  • 1970-01-01
  • 2019-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-12
  • 2022-06-22
相关资源
最近更新 更多