【问题标题】:PHP basename Twig equivalentPHP 基本名称 Twig 等效项
【发布时间】:2015-09-15 15:06:43
【问题描述】:

Twig 中是否有与 PHP basename() 等效的函数?

类似:

$file = basename($path, ".php");

【问题讨论】:

  • 没有这样的功能 - 但您可以轻松地为此使用自定义过滤器或功能。见stackoverflow.com/questions/8180585/use-php-function-in-twig
  • 你有一个你想使用它的用例吗?似乎是控制器应该管理的东西。
  • 当然,将您的逻辑移到模板之外。想知道如何在 Twig 中获取文件实例。
  • 好的,谢谢 Alexander Kludt

标签: symfony twig


【解决方案1】:

如果path 等于/path/to/file.php,那么

{{ path|split('/')|last|replace({'.php':''}) }}

将输出file

【讨论】:

    【解决方案2】:

    默认情况下,Twig 中没有默认的 basename 过滤器样式,但是如果您需要使用自己的扩展默认的 Twig 过滤器或函数,您可以按照说明书中的说明为您的 Symfony 版本创建一个扩展。 http://symfony.com/doc/current/cookbook/templating/twig_extension.html

    树枝扩展

    // src/AppBundle/Twig/TwigExtension.php
    namespace AppBundle\Twig;
    
    class TwigExtension extends \Twig_Extension
    {
    
        public function getName()
        {
           return 'twig_extension';
        }
    
        public function getFilters()
        {
           return [
               new \Twig_SimpleFilter('basename', [$this, 'basenameFilter'])
           ];
        }
    
        /**
         * @var string $value
         * @return string
         */
        public function basenameFilter($value, $suffix = '')
        {
           return basename($value, $suffix);
        }
    }
    

    配置文件

    # app/config/services.yml
    services:
        app.twig_extension:
            class: AppBundle\Twig\TwigExtension
            public: false
            tags:
                - { name: twig.extension }
    

    树枝模板

    {% set path = '/path/to/file.php' %}
    {# outputs 'file.php' #}
    {{ path|basename }}
    
    {# outputs 'file' #}
    {{ path|basename('.php') }}
    
    {# outputs 'etc' #}
    {{ '/etc/'|basename }}
    
    
    {# outputs '.' #}
    {{ '.'|basename }}
    
    {# outputs '' #}
    {{ '/'|basename }}
    

    【讨论】:

      【解决方案3】:

      使用 Twig,我们可以找到最后一个点 (.) 之后的字符串并将其从字符串中删除以获取文件名(但如果有多个点,它将不起作用)。

      {% set string = "file.php" %}
      {{ string|replace({ ('.' ~ string|split('.')|last): '' }) }}
      {# display "file" #}
      {% set string = "file.html.twig" %}
      {{ string|replace({ ('.' ~ string|split('.')|last): '' }) }}
      {# display "file.html" and not "file" #}
      

      解释:

      {{ string|replace({     # declare an array for string replacement
          (                   # open the group (required by Twig to avoid concatenation with ":")
              '.'
              ~               # concatenate 
              string
                  |split('.') # split string with the "." character, return an array
                  |last       # get the last item in the array (the file extension)
          )                   # close the group
          : ''                # replace with "" = remove
      }) }}
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-04
        • 2017-03-21
        • 1970-01-01
        • 2012-02-09
        • 2019-10-03
        相关资源
        最近更新 更多