如何防止树枝自动转义日文字符?

How to prevent twig from auto escaping Japanese characters?

我正在尝试显示产品的到达日期。我正在使用 moment.jsYYYY/MM/DD 日期字符串格式化为日语本地字符串。我的 javascript 代码包含在 twig 文件中:

<script>
        moment.locale('ja');
        let updateArrivalDate = function() {
            $('.arrival-date').each(function() {
                let $this = $(this);
                $selectDate = $this.next().next().find('select').eq(0);
                $selectTime = $this.next().next().find('select').eq(1);
                var text = $selectDate.val();
                var date = moment(text, 'YYYY/MM/DD');
                if (date.isValid()) {
                    $this.text(date.format('{{ 'YYYY年M月D日 (dd)'|raw }}'))
                }
            });
        };
        $(document).ready(function() {
          updateArrivalDate();
            $('select').on('change', function() {
                updateArrivalDate();
            });
        });
    </script>

如您所见,我使用 raw 过滤器来防止 twig 转义日文字符。尽管如此,twig 无论如何都会转义特殊字符,并且文本是乱码:

当然,如果我将上面的代码片段移动到外部文件中就可以解决。但说真的,有没有办法阻止 twig 转义日文字符?为什么 raw 过滤器 没有 工作?

我想你可以像这样更改代码:

$this.text(date.format('{{ "YYYY年M月D日 (dd)"|json_encode()|raw }}'))

从 cdn(或从 npm/yarm 安装包)导入 moment ja 语言环境,如 https://momentjs.com/docs/#/i18n/

所示

如果你使用 cdn 托管 js 然后使用以下代码:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/ja.js" integrity="sha256-CFWtR1hGN/5Vc+kcJkqeMFND0g6gFFZdnSqUtdL7WOQ=" crossorigin="anonymous"></script>

然后代替:

 $this.text(date.format('{{ 'YYYY年M月D日 (dd)'|raw }}'))

使用:

 var date = moment(text, 'YYYY/MM/DD');
 $this.html(date.locale('ja').format('LL (dd)'))

并删除:

        moment.locale('ja');

因此您的脚本将是:

<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js" integrity="sha256-4iQZ6BVL4qNKlQ27TExEhBN1HFPvAvAMbFavKKosSWQ=" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/ja.js" integrity="sha256-CFWtR1hGN/5Vc+kcJkqeMFND0g6gFFZdnSqUtdL7WOQ=" crossorigin="anonymous"></script>
<script>
        let updateArrivalDate = function() {
            $('.arrival-date').each(function() {
                let $this = $(this);
                $selectDate = $this.next().next().find('select').eq(0);
                $selectTime = $this.next().next().find('select').eq(1);
                let text = $selectDate.val();
                let date = moment(text, 'YYYY/MM/DD');
                if (date.isValid()) {
                    $this.text(date.locale('ja').format('LL (dd)'))
                }
            });
        };
        $(document).ready(function() {
          updateArrivalDate();
            $('select').on('change', function() {
                updateArrivalDate();
            });
        });
    </script>

参考以下仅包含有关语言的格式的示例:

$(document).ready(function(){
 $("#date").html(moment().locale('ja').format('LL (dd)'))
});
<script src="https://momentjs.com/downloads/moment.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/locale/ja.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="date"></div>

所以你让 moment 格式化日期并避免 js/twig 的任何复杂性。保持简单,让 js 完成它的工作,而不是将它们混合在一起。