代码之家  ›  专栏  ›  技术社区  ›  Steven Aguilar

如何在JSON中插值

  •  0
  • Steven Aguilar  · 技术社区  · 5 年前

    我正在尝试从字符串中删除Unicode字符。

        {% include 'components/accordion.twig' with {
          this: {
            id: program.slug,
            active: (loop.index == 1) ? true : false
          }
        } only %}
      {% endfor %}
    

    基本上我想要达到的是

    {% include 'components/accordion.twig' with {
              this: {
                id: "{{ programs.slug | convert_encoding('UTF-8', 'ISO-8859-1') }}"
                active: (loop.index == 1) ? true : false
              }
            } only %}
    {% endfor %}
    

    问题是 programs.slug 退货 "aria-c-senior-citizens-rent-increase-exemption-%e2%80%8bscrie" 我正试着移除 %e2%80%8b "aria-c-senior-citizens-rent-increase-exemption-scrie" convert_encoding('UTF-8', 'ISO-8859-1') 我该怎么做呢?

    1 回复  |  直到 5 年前
        1
  •  0
  •   u_mulder    5 年前

    只是:

    {% include 'components/accordion.twig' with {
      this: {
        id: program.slug | convert_encoding('UTF-8', 'ISO-8859-1'),
        active: (loop.index == 1) ? true : false
      }
    } only %}
    

    因为 {{ }} 相当于 echo .

        2
  •  0
  •   Steven Aguilar    5 年前

    在木材中创建函数 Make functions available in Twig

     /**
         * Adds functionality to Twig.
         *
         * @param \Twig\Environment $twig The Twig environment.
         * @return \Twig\Environment
         */
    function add_to_twig( $twig ) {
        // Adding functions as filters.
        $twig->addFilter( new Timber\Twig_Filter( 'url_decode', 'url_decode' ) );
    
        return $twig;
    }
    
    function url_decode( $text ) {
        return  urldecode($text);
    }
    

    programs.slug 所以我创建了一个函数,并在twig模板中传递它,如下所示 urldecode

     id: function('url_decode', program.slug),