【问题标题】:Imported Twig macro displays nothing i.e. not working导入的 Twig 宏不显示任何内容,即不工作
【发布时间】:2017-05-21 03:57:51
【问题描述】:

我正在尝试实现某种宏自动加载。

这个想法是定义一堆宏并在所有下一个模板文件中使用它们。

这是我的尝试:

<?php
    define('ROOT_FRONT', '/path/to/files/');
    define('LAYOUT_DIR', ROOT_FRONT . 'layout/');

    include(ROOT_FRONT . 'lib/Twig/Autoloader.php');
    Twig_Autoloader::register();
    
    $twig_loader = new Twig_Loader_Filesystem(array(LAYOUT_DIR, ROOT_FRONT));
    
    $twig = new Twig_Environment($twig_loader, array(
        'charset' => 'ISO-8859-15',
        'debug' => !!preg_match('@\.int$@', $_SERVER['SERVER_NAME']),
        'cache' => $_SERVER['DOCUMENT_ROOT'] . '/cache/twig/'
    ));
    
    $macro_code = '';
    foreach(array_filter(
        array_diff(
            scandir(LAYOUT_DIR . 'macros/'),
            array('..','.')
        ),
        function($file)
        {
            return strtolower(pathinfo($file, PATHINFO_EXTENSION)) == 'twig'
                && is_file(LAYOUT_DIR . 'macros/' . $file);
        }
    ) as $file)
    {
        $info = pathinfo($file);
        $macro_code .= '{% import \'macros/' . $info['basename'] . '\' as macros_' . $info['filename'] . ' %}';
    }
    
    $twig
        ->createTemplate($macro_code)
        ->render(array());
    
    $twig->display('index.twig', array());

如果我有一个文件,比如macro/clearfix.twig,它将在$macro_code 内部生成这个模板代码:

{% import 'macros/clearfix' as macros_clearfix %}

macro/clearfix.twig 里面的代码是这样的:

{% macro clearfix(index, columns) %}

    {% if index is divisible by(columns) %}
        <div class="clearfix visible-md-block visible-lg-block"></div>
    {% endif %}
    
    {% if index is even %}
        <div class="clearfix visible-sm-block"></div>
    {% endif %}
    
{% endmacro %}

然后,在index.twig 中,我有这个:

{{ macros_clearfix.clearfix(index=2, columns=6) }}

但是什么都没有显示。

但是,以下代码可以工作:

{% set index = 2 %}
{% set columns = 6 %}

{% if index is divisible by(columns) %}
    <div class="clearfix visible-md-block visible-lg-block"></div>
{% endif %}

{% if index is even %}
    <div class="clearfix visible-sm-block"></div>
{% endif %}

我可能做错了什么?

我是否误解了某些内容或应用不正确?

【问题讨论】:

  • 你奇怪地传递了参数,你应该像这样传递它们:{{ macros_clearfix.clearfix(2, 6) }}
  • 我知道,但两者的意思完全相同。由于(老实说)宏的名称是垃圾,我就这样传递它们。这样我就可以知道什么意思,不用看宏。
  • 你为什么要使用这个复杂的宏系统,而你可以添加twig函数来做到这一点?宏并不意味着在项目中全局使用。 Twig 功能旨在进行内容生成并在全球范围内注册
  • @goto 我正在写一些基于此的东西。事实上,我正在为此写一个答案并展示一些代码。

标签: macros twig


【解决方案1】:

TL;DR:

Twig 要求您将宏加载到将要使用它们的文件中。
只需创建自定义函数来做你想做的事。


Twig(至少 v1.30)不实现宏继承。
这要求您加载要在每个文件上使用的每个单个宏。

唯一的方法是使用完全用 PHP 编写的函数。

这是我已经解决的:

  • index.php:

    <?php
        define('ROOT_FRONT', '/path/to/files/');
        define('LAYOUT_DIR', ROOT_FRONT . 'layout/');
    
        include(ROOT_FRONT . 'lib/Twig/Autoloader.php');
        Twig_Autoloader::register();
    
        $twig_loader = new Twig_Loader_Filesystem(array(LAYOUT_DIR, ROOT_FRONT));
    
        $twig = new Twig_Environment($twig_loader, array(
            'charset' => 'ISO-8859-15',
            'debug' => !!preg_match('@\.int$@', $_SERVER['SERVER_NAME']),
            'cache' => $_SERVER['DOCUMENT_ROOT'] . '/cache/twig/'
        ));
    
        // ~ magic happens here ~
        foreach(include(LAYOUT_DIR . 'fn.php') as $k => $fn)
        {
            $twig->addFunction(new Twig_SimpleFunction("fn_$k", $fn));
        }
    
        $twig->display('index.twig', array());
    
  • fn.php:

    <?php
        return array(
            'clearfix' => function($index, $columns){
                $html = '';
    
                if(!($index % $columns))
                {
                    $html .= '<div class="clearfix visible-md-block visible-lg-block"></div>';
                }
    
                if(!($index & 1))
                {
                    $html .= '<div class="clearfix visible-sm-block"></div>';
                }
    
                return $html;
            }
        );
    
  • index.twig:

    {{ fn_clearfix(index=2, columns=6) }}
    

这样,您的所有代码都会被整齐地编入索引,新函数会自动创建,并且很容易根据自己的喜好进行扩展。

这可能是最糟糕的想法,但它确实有效。

【讨论】:

    【解决方案2】:

    从 Twig 2.0 开始,文件中导入的宏在子模板中不再可用(例如通过 include 调用)。您需要在使用宏的每个文件中显式导入宏。

    来自https://twig.symfony.com/doc/1.x/deprecated.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-30
      • 1970-01-01
      • 2012-09-29
      • 2020-11-10
      • 2012-04-12
      • 2019-11-11
      • 2017-09-25
      相关资源
      最近更新 更多