从 Twig 模板调用 PHP 函数 [英] Call PHP function from Twig template

查看:67
本文介绍了从 Twig 模板调用 PHP 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的控制器中有一个返回实体数组的函数,所以在我的树枝模板中,我这样做是为了迭代元素:

I have a function in my controller that returns array of entities so in my twig template I do this to iterate over elements:

{% for groupName, entity in items %}
    <ul>
        <ul>
            {% for element in entity %}
                <li>{{ element.getLabel }}</li>
                <li><input type="text" name="detail[{{ element.getId }}]" id="pd_{{ element.getId }}" /><input type="text" name="price[{{ element.getId }}]" id="pd_price_{{ element.getId }}" /><input type="text" name="stock[{{ element.getId }}]" id="pd_stock_{{ element.getId }}" /></li>
            {% endfor %}
        </ul>
    </ul>
{% endfor %}

在我的控制器中,我也有这个 PHP 函数:

In my controller I have also this PHP function:

private function DetailCombination($arr, $level, &$result, $curr = array()) {
    for ($i = 0; $i < count($arr); $i++) {
        $new = array_merge($curr, array($arr[$i]));
        if ($level == 1) {
            sort($new);
            if (!in_array($new, $result)) {
                $result[] = $new;
            }
        } else {
            combinations($arr, $level - 1, $result, $new);
        }
    }
}

我可以这样称呼它:

for ($i = 0; $i < count($arr); $i++) {
    $this->DetailCombination($arr, $i + 1, $result);
}

// TEST
foreach ($result as $arr) {
    echo join(" ", $arr) . '<br>';
}

是否可以从 Twig 模板访问 PHP 函数以获取实体中元素的所有可能组合?怎么样?

It's possible access to the PHP function from Twig template in order to get all the possible combinations of elements in entity? How?

** 更新 **

这是返回由 Twig Template 处理的实体的函数:

This is the function that returns entities processed after by Twig Template:

private function getVariations($category_id) {
    $items = array();

    $em = $this->getDoctrine()->getManager();
    $entityCategory = $em->getRepository('CategoryBundle:Category')->find($category_id);

    foreach ($entityCategory->getProductDetails() as $entity) {
        if ($entity->getToProduct() == 1) {
            foreach ($entity->getDetailGroup() as $group) {
                if (!array_key_exists($group->getName(), $items)) {
                    $items [$group->getName()] = array();
                }

                $items [$group->getName()] [] = $entity;
            }
        } 
    }

    return $items;
} 

推荐答案

无法直接访问 Twig 中的任何 PHP 函数.

Its not possible to access any PHP function inside Twig directly.

您可以做的是编写一个 Twig 扩展.一个常见的结构是,编写一个具有一些实用功能的服务,编写一个 Twig 扩展作为从 Twig 访问服务的桥梁.Twig 扩展将使用该服务并您的控制器也可以使用该服务.

What you can do is write a Twig extension. A common structure is, writing a service with some utility functions, write a Twig extension as bridge to access the service from twig. The Twig extension will use the service and your controller can use the service too.

看一看:http://symfony.com/doc/current/cookbook/templating/twig_extension.html

干杯.

这篇关于从 Twig 模板调用 PHP 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆