试图在空间中找到一些对象

时间:2014-10-14 23:04:10

标签: symfony twig distance

我正在尝试使用Twig在一个字段中找到一些对象给出一些条件(是的,我知道它很简单,但是我有太多麻烦才能得到结果)。没关系,但我在给对象之间留一些距离时遇到了麻烦。

这是我的代码:

{% for key, positions in teams %}
    {% for key1, position in positions %}
        {% for key2, player in  position %}
            {% set x = 100 %}
            {% set counter = player.positionId|length + 1%}
            {% set d = x/counter %}
            {% if player.positionId == 1 %}
                {% set top = 0.4 %}
                {% set xpos = 42 %} //That value is correct because it's a goalkeeper
            {% elseif player.positionId == 2%}
                {% set top  = 9.5 %}
                {% set xpos =  %}
            {% elseif player.positionId == 3%}
                {% set top = 20.5 %}
                {% set xpos = %}
            {% elseif player.positionId == 4%}
                {% set top = 32.5 %}
                {% set xpos = %}
            {% endif %}

总之,我有一些玩家位于不同的y坐标上,但是如果它们具有相同的位置,我需要每个玩家在它们之间有一些距离,即xpos在x轴上的位置

如果您不了解Twig,您也可以帮助我使用其他语言。提前致谢 啊,好吧,我想知道如何分开球队(因为它现在渲染所有球员)

1 个答案:

答案 0 :(得分:0)

有多种方法可以将值传递给twig,一种方法是通过路由来实现,

# app/config/routing.yml
blog_show:
    path:      /blog/{YOUR_VARIABLE_OR_VALUE_HERE}
    defaults:  { _controller: AcmeBlogBundle:Blog:show }

请参阅此参考:http://symfony.com/doc/current/book/routing.html

您还可以设置全局变量:http://symfony.com/doc/current/cookbook/templating/global_variables.html

或在控制器中使用存储库或在您的on上创建一个数组,然后通过参数将其发送到视图 即)

$repository2 = $this->getDoctrine()->getRepository('YOUR_BUNDLE_NAME_Bundle:YOUR_REPOSITORY');
        $YOUR_ARRAY_NAME = $repository2->REPOSITORY_METHOD_OR_GENERIC_METHOD($IDENTIFIER);

$params = array('form' => $form->createView(), 'YOUR_ARRAY_NAME' => $YOUR_ARRAY_NAME');
            return $this->render('SystemCheckoutBundle:Checkout:checkout.html.twig', $params);

然后可以通过以下方式访问这些参数:

{% for YOUR_ARRAY_NAME in YOUR_ARRAY_NAME %}
               Client ID: {{ YOUR_ARRAY_NAME.contactId }}<br>
               First Name: {{ YOUR_ARRAY_NAME.firstName }}<br>
               Last Name:{{ YOUR_ARRAY_NAME.lastName }}<br>
               Email: {{ YOUR_ARRAY_NAME.email }}<br>
{% endfor %}