繁体   English   中英

我正在尝试在树枝中使用自定义php函数,我在做什么错呢?

[英]I am trying to use custom php function in twig, what am I doing wrong?

我试图在树枝中使用我的自定义php函数。 当我在page.html.twig中输入{{current_url}}时,什么也没有发生。 我也尝试过{{kint(current_url)}},它输出NULL这是我的代码

<?php
namespace Drupal\nextprev\plugin\block\customtwigoutput;


use Drupal\Core\Template\TwigExtension;
use  Drupal\nextprev\nextprevmodule\path;
use \Symfony\Component\HttpFoundation\Request;


class AppExtension extends \Twig_Extension {

public function url() {
   $current_path = \Drupal::service('path.current')->getPath();
   return $current_path;
}


  public function getFilters() {
     return array(
       'current_url' => new \Twig_Function_Function(array('Drupal\nextprev\plugin\block\customtwigoutput\AppExtension', 'url')),
     );
   }
}

我究竟做错了什么?

您是否在其中添加了services.yml? 这是一个样本;

twig.url:
    class: AppBundle\Twig\Extension\AppExtension
    tags:
        - { twig.extension }

也许您应该尝试使用树枝函数:

在您的扩展名中

public function getFunctions()
{
    return array(
        new \Twig_SimpleFunction('current_url', array($this, 'url')),
    );
}

在您看来

 {{ current_url() }}

暂无
暂无

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

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