简体   繁体   English

如何从自动 escaping 日文字符中防止 twig?

[英]How to prevent twig from auto escaping Japanese characters?

I am trying to display product's arrival date.我正在尝试显示产品的到货日期。 I'm using moment.js to format YYYY/MM/DD date string to Japanese local string.我正在使用moment.jsYYYY/MM/DD日期字符串格式化为日语本地字符串。 My javascript code is included in a twig file:我的 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>

As you can see, I used raw filter to prevent twig from escaping Japanese characters.如您所见,我使用raw过滤器来防止 twig 来自 escaping 日文字符。 Nevertheless, twig escapes special characters anyway and the text is garbled:尽管如此, twig 无论如何都会转义特殊字符并且文本是乱码:

由于树枝自动转义,日文字符乱码

Of course, it would be solved if I moved the above snippet to an external file.当然,如果我将上面的代码片段移到外部文件中,它就会得到解决。 But seriously, is there not a way to stop twig escaping Japanese characters?但是说真的,有没有办法阻止 twig escaping 日文字符? Why is raw filter not working?为什么raw过滤器不起作用

I think you can change the code like that:我认为您可以像这样更改代码:

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

Import moment ja locale from the cdn (or from npm/yarm installed package) as seen in https://momentjs.com/docs/#/i18n/https://momentjs.com/docs/#/i18n/中所示,从 cdn(或从 npm/yarm 安装包)导入 moment ja语言环境

If you use a cdn hosted js then use the following code:如果您使用 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>

Then instead of:然后代替:

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

Use:利用:

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

And remove the:并删除:

        moment.locale('ja');

line线

So your script will be:所以你的脚本将是:

<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>

Consult the example bellow containing only the formating regarding the language:请参阅下面的示例,其中仅包含有关语言的格式:

 $(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>

So you let moment to format the date and avoid any complexity with js/twig.所以你让时间来格式化日期并避免使用 js/twig 的任何复杂性。 Just keep it simple and let the js to do its job instead of mixing them all together.保持简单,让 js 完成它的工作,而不是将它们混合在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM