【问题标题】:How can I print the output of my custom php function in twig?如何在 twig 中打印自定义 php 函数的输出?
【发布时间】:2017-11-16 13:06:29
【问题描述】:

在我的自定义模块中,我有以下代码:

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

如何在我的树枝模板中使用此功能?

注意:我知道我可以更轻松地获得当前路径。我只指定了这个函数,所以我可以了解在 twig 中使用自定义 php 函数的想法。

【问题讨论】:

标签: php drupal twig drupal-8


【解决方案1】:

您可以像这样在 Twig (Drupal) 中获取当前路径:

<a href="{{ url('<current>') }}">{{ 'I represent current page URL :)'|t }}</a>

编辑

按照 Drupal 文档中的定义,您可以按照此示例将自己的函数添加到 twig 模板,该示例展示了如何向 twig 模板添加函数或过滤器:

namespace Drupal\twig_extension_test\TwigExtension;

use Drupal\Core\Template\TwigExtension;

/**
 * A test Twig extension that adds a custom function and a custom filter.
 */
class TestExtension extends TwigExtension {

/**
 * Generates a list of all Twig functions that this extension defines.
 *
 * @return array
 *   A key/value array that defines custom Twig functions. The key denotes the
 *   function name used in the tag, e.g.:
 *   @code
 *   {{ testfunc() }}
 *   @endcode
 *
 *   The value is a standard PHP callback that defines what the function does.
 */
 public function getFunctions() {
     return array(
         'testfunc' => new \Twig_Function_Function(array('Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFunction')),
);
}

/**
 * Generates a list of all Twig filters that this extension defines.
 *
 * @return array
 *   A key/value array that defines custom Twig filters. The key denotes the
 *   filter name used in the tag, e.g.:
 *   @code
 *   {{ foo|testfilter }}
 *   @endcode
 *
 *   The value is a standard PHP callback that defines what the filter does.
 */
 public function getFilters() {
     return array(
          'testfilter' => new \Twig_Filter_Function(array('Drupal\twig_extension_test\TwigExtension\TestExtension', 'testFilter')),
);
 }

/**
 * Gets a unique identifier for this Twig extension.
 *
 * @return string
 *   A unique identifier for this Twig extension.
 */
 public function getName() {
     return 'twig_extension_test.test_extension';
 }

/**
 * Outputs either an uppercase or lowercase test phrase.
 *
 * The function generates either an uppercase or lowercase version of the
 * phrase "The quick brown fox jumps over the lazy dog 123.", depending on
 * whether or not the $upperCase parameter evaluates to TRUE. If $upperCase
 * evaluates to TRUE, the result will be uppercase, and if it evaluates to
 * FALSE, the result will be lowercase.
 *
 * @param bool $upperCase
 *   (optional) Whether the result is uppercase (true) or lowercase (false).
 *
 * @return string
 *   The generated string.
 *
 * @see \Drupal\system\Tests\Theme\TwigExtensionTest::testTwigExtensionFunction()
 */
 public static function testFunction($upperCase = FALSE) {
      $string = "The quick brown box jumps over the lazy dog 123.";
      if ($upperCase == TRUE) {
           return strtoupper($string);
      }
      else {
           return strtolower($string);
      }
  }

 /**
  * Replaces all instances of "animal" in a string with "plant".
  *
  * @param string $string
  *   The string to be filtered.
  *
  * @return string
  *   The filtered string.
  *
  * @see \Drupal\system\Tests\Theme\TwigExtensionTest::testTwigExtensionFilter()
  */
  public static function testFilter($string) {
      return str_replace(array('animal'), array('plant'), $string);
  }

  }

【讨论】:

  • 好吧,我的错,我应该先问,无论如何,如果你愿意,你可以将你的自定义函数添加到树枝扩展中,我编辑了我的答案
  • &lt;?php namespace Drupal\nextprev\plugin\block\customtwigoutput; use Drupal\nextprev\nextprevmodule\path; use \Symfony\Component\HttpFoundation\Request; class AppExtension extends \Twig_Extension;{ public function url() { $current_path = \Drupal::service('path.current')-&gt;getPath(); return $current_path; } public function getFilters() { return array( 'current_url' =&gt; new \Twig_Function_Function(array('Drupal\nextprev\plugin\block\customtwigoutput\AppExtension', 'url')), ); } }我在这里做错了什么?
  • @DavidKovač 您在 extends 关键字后面有一个分号 ;,但不应该出现在该处:extends \Twig_Extension;。删除它:class AppExtension extends \Twig_Extension { /* ... */ }
  • 我在发布上面的评论后修复了这个问题。恐怕是别的东西。
猜你喜欢
  • 1970-01-01
  • 2010-09-16
  • 1970-01-01
  • 1970-01-01
  • 2012-10-27
  • 2020-11-09
  • 1970-01-01
  • 2021-12-29
  • 1970-01-01
相关资源
最近更新 更多