我试图在twig中使用自定义php函数,我做错了什么?
我尝试在twig中使用我的自定义php函数。 当我在page.html.twig中键入{current_url}}时,什么也没有发生。 我还尝试了{{kintcurrent_url}},它输出空值 这是我的密码我试图在twig中使用自定义php函数,我做错了什么?,php,drupal,twig,drupal-8,Php,Drupal,Twig,Drupal 8,我尝试在twig中使用我的自定义php函数。 当我在page.html.twig中键入{current_url}}时,什么也没有发生。 我还尝试了{{kintcurrent_url}},它输出空值 这是我的密码 <?php namespace Drupal\nextprev\plugin\block\customtwigoutput; use Drupal\Core\Template\TwigExtension; use Drupal\nextprev\nextprevmodule\
<?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 }
也许您应该尝试使用细枝功能: 在您的分机中: 在你看来
Web服务器日志中有错误吗?我没有。我正试图添加它,但我有点卡住了
public function getFunctions()
{
return array(
new \Twig_SimpleFunction('current_url', array($this, 'url')),
);
}
{{ current_url() }}