如何使用Twig在变量上添加动态属性?

时间:2019-01-07 18:17:44

标签: symfony variables dynamic attributes twig

在我的项目(symfony / flex 1.1)中,我有一种方法“ displayMenuAndSubMenu([...])”(作为服务),用于在多维数组上构建菜单和子菜单。

NB:某些菜单可能没有子菜单

我的问题是,当我想在html.twig模板中呈现菜单板时,无法重现增量变量$ i和$ a。确实,使用我的实际树枝代码,我得到了一个简单的字符串输出:-(

我在某处读到一个定制的树枝扩展名将使我能够解决此难题。

但是我发现实现一个简单的计数器非常复杂。

所以我的问题是:

  • 在html.twig模板中是否有更简单的方法来做到这一点?
  • 我是否必须通过php.twig模板(对我而言是令人反感的)?
  • 我注定要定义树枝扩展吗? (如果是这样,您有例子吗?)

谢谢

ContentService :: displayMenuAndSubmenu

    public function displayMenuAndSubMenu(CategoryService $categoryService, CategoryTypeService $categoryTypeService)
        {
            $categoryId = $categoryService->getCategoryId('Primary');
            $categoryTypeId = $categoryTypeService->getCategoryTypeId('Menu');

            // Getting primary menus
            $primaryMenu = $this->em->getRepository('App\Entity\Content')->findByCategoryTypeAndCategory($categoryTypeId, $categoryId);

            // Constructing MainMenu with menus and submenus
            $i = 0;
            $menu = [];
            $submenu = [];
            $mainMenu = [];
            foreach ($primaryMenu as $keyMenu => $menuItem) {
                $menu[$i] = [ 'menu_' . $i => [
                    'id' => $menuItem->getId(),
                    'name' => $menuItem->getName(),
                    'body' => $menuItem->getBody(),
                    'slug' => $menuItem->getSlug(),
                    ] 
                ];

                // Getting secondary menu
                $secondaryMenu = $this->em->getRepository('App\Entity\Content')->findByParent($menuItem->getId());
                $a = 0;
                foreach ($secondaryMenu as $keySubmenu => $subMenuItem) {
                    $submenu[$i][$a] = [ 'submenu_' . $i . '_' . $a => [ 
                                                    'id' => $subMenuItem->getId(),
                                                    'name' => $subMenuItem->getName(),
                                                    'body' => $subMenuItem->getBody(),
                                                    'slug' => $subMenuItem->getSlug(),
                                                 ]
                                             ];

                    $menu[$i] += $submenu[$i][$a];
                    $a++;
                }

                $mainMenu += $menu; 
                $i++;

            }

            return $mainMenu;
        }

_menu.html.twig

    {% set i = 0 %}
    {% for menu in menu_items %}

    {% set a = 0 %}

        <div class="center">
        <section class="primary-menu">

             {% if 'menu_'~i~'.slug' is not empty %}
             <header class="enter">{{ 'menu_'~i~'.name' }}</header>

                {% for submenu in 'menu_'~i~'.submenu' %} 
                <article>
                 <a href= {{ 'submenu_'~i~'_'~a~'.slug' }}" alt="{{ 'submenu_'~i~'_'~a~'.body' }}"class="para1">{{ 'submenu_'~i~'_'~a~'.name' }}</a>
                </article>
                {% endfor %}

            {% else %}
             <header class="enter"><a href="{{ 'menu_'~i~'.slug' }}" alt="{{ 'menu_'~i~'.body' }}">{{ 'menu_'~i~'.name' }}</a></header>
            {% endif %} 
        </section>
        {% set i = i + 1 %}
        {% endfor %}

ContentService类输出上的转储($ mainMenu)

    array:5 [▼
      0 => array:4 [▼
        "menu_0" => array:4 [▼
          "id" => "a420742f-124a-11e9-a1fd-805e4fe8b43b"
          "name" => "Menu 0"
          "body" => "Primary menu0"
          "slug" => null
        ]
        "submenu_0_0" => array:4 [▼
          "id" => "a4208ec3-124a-11e9-a1fd-805e4fe8b43b"
          "name" => "Submenu 0"
          "body" => "Secondary menu0"
          "slug" => "menu-0-submenu-0"
        ]
        "submenu_0_1" => array:4 [▼
          "id" => "a420a70d-124a-11e9-a1fd-805e4fe8b43b"
          "name" => "Submenu 1"
          "body" => "Secondary menu1"
          "slug" => "menu-0-submenu-1"
        ]
        "submenu_0_2" => array:4 [▼
          "id" => "a420ba8f-124a-11e9-a1fd-805e4fe8b43b"
          "name" => "Submenu 2"
          "body" => "Secondary menu2"
          "slug" => "menu-0-submenu-2"
        ]
      ]
      1 => array:1 [▼
        "menu_1" => array:4 [▼
          "id" => "a4205c9f-124a-11e9-a1fd-805e4fe8b43b"
          "name" => "Home"
          "body" => "Home menu"
          "slug" => "home"
        ]
      ]
      2 => array:4 [▶]
      3 => array:4 [▶]
      4 => array:4 [▶]
    ]

1 个答案:

答案 0 :(得分:0)

为了解决上一个问题,我决定创建一个像这样的树枝过滤器:

<?php

namespace App\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class AppExtension extends AbstractExtension
{
    public function getFilters()
    {
        return array(
            new TwigFilter('formatMenu', [
                $this,
                'makeMenu'
            ]),
            new TwigFilter('formatSubmenu', [
                $this,
                'makeSubmenu'
            ]),
        );
    }

    public function makeMenu($primaryCounter)
    {
        $result =  "menu_" . $primaryCounter;
        // $result = '$'.$result;

        return $result;
    }

    public function makeSubmenu($primaryCounter, $secondaryCounter)
    {
        $result = "submenu_" . $primaryCounter . "_" . $secondaryCounter;
        // $result = '$'.$result;

        return $result;
    }

}

在树枝视图中是这样的:

{% set i = 0 %}
{% for menu in menu_items %}

   <div class="center">
        <section class="primary-menu">

        {% if menu|formatMenu(i).slug is empty %}

         <header class="enter">{{ menu|formatMenu(i).name }}</header>

        {% set a =  0 %}
           {% for submenu in menu|formatMenu(i).submenu %}

                <article>
                 <a href= {{ submenu|formatSubmenu(i, a).slug }}" alt="{{ submenu|formatSubmenu(i, a).body }}" class="para1">{{ submenu|formatSubmenu(i, a).name }}</a>
                </article>

            {% set a = a + 1 %}
            {% endfor %}

        </header>
        {% else %}

        <header class="enter">
            <a href="{{ menu|formatMenu(i).slug }}" alt="{{ menu|formatMenu(i).body }}">{{ menu|formatMenu(i).name }}</a>
        </header>

        {% endif %}

        </section>

    </div> 
{% set i = i + 1 %}
{% endfor %}

不幸的是,现在我出现以下错误:

在呈现模板的过程中引发了异常(“注意:数组到字符串的转换”)。

我承认我有点迷茫,因为当我像这样转储变量时:

    public function makeMenu($primaryCounter)
    {

        dump($primaryCounter);die;

        $result =  "menu_" . $primaryCounter;

        return $result;
    }

我得到数组而不是变量:

array:4 [▼
  "menu_0" => array:4 [▶]
  "submenu_0_0" => array:4 [▶]
  "submenu_0_1" => array:4 [▶]
  "submenu_0_2" => array:4 [▶]
]

那么,那是什么文档?