在将数组传递给模板之前计算选择哪些项目是否合适?

时间:2017-11-04 11:54:08

标签: design-patterns twig

我了解到在模板中应该没有太多逻辑。

当我需要渲染下拉列表时,我决定这样做。

而不是因为有选择的逻辑: 从这里回答:Twig Setting Select Option on a dropdown

<select class="form-control" id="supervisor">
    {% for oneSupervisor in supervisor %}
        {% set selected = '' %}
        {% if (oneSupervisor.fname ~ ' ' ~ oneSupervisor.lname) == user.supervisor %}
           {% set selected = 'selected' %}
        {% endif %}
        <option value="{{oneSupervisor.fname}} {{oneSupervisor.lname}}" {{ selected }}>{{oneSupervisor.fname}} {{oneSupervisor.lname}}</option>
    {% endfor %}
</select>

会这样做:

<select class="form-control" id="supervisor">
    {% for oneSupervisor in supervisor %}
        <option value="{{oneSupervisor.fname}} {{oneSupervisor.lname}}" {{ oneSupervisor.selected }}>{{oneSupervisor.fname}} {{oneSupervisor.lname}}</option>
    {% endfor %}
</select>

因此,您可以看到我删除了逻辑计算是否被选中。

在将主管传递给模板之前计算oneSupervisor.selected。

这是好还是坏?我的同事认为它太复杂了。我不认为它更复杂,只是主管项目被一个键扩展。

没有测试代码,所以可能是错误,但这不是重点。

1 个答案:

答案 0 :(得分:0)

这是主要的意见。无论如何我在这个问题上的2美分

如果您有很多组合框,您需要定义所选的组合框,您建议的内容可能会有用。由于您没有向我们提供有关如何在我想象的值上设置属性selected的详细信息,您在控制器中使用loop执行此操作。因为你会有很多组合框,你会有很多冗余的代码,这很糟糕。

我不确定你是否正在使用对象,但是我们确定你确定每个对象都有一个唯一的id来区分哪个对象然后它会更好用trait

解决这个问题

Selectable.php

<?php
    trait Selectable {
        public function isSelected($object) {
            return $this->getId() === $object->getId();
        }
    }

通过这样做,您可以为每个可以选择的对象提供特征,例如

user.php的

<?php   
    class User {
        use Selectable;

        protected $id;
        protected $supervisor = null;
        protected $city = null;

        public function setId($id) {
            $this->id = $id;
            return $this;
        }

        public function getId() {
            return $this->id;
        }

        public function setSupervisor(User $supervisor) {
            $this->supervisor = $supervisor;
            return $this;
        }

        public function getSupervisor() {
            return $this->supervisor;
        }

        public function setCity(City $city) {
            $this->city = $city;
            return $this;
        }

        public function getCity() {
            return $this->city;
        }           
    }

City.php

<?php
    class City {
        use Selectable;

        protected $id;

        public function setId($id) {
            $this->id = $id;
            return $this;
        }

        public function getId() {
            return $this->id;
        }
    }

现在您无需担心编写逻辑以检查是否已选择某些内容,您可以在twig

中自由使用它

枝杈

<select name="supervisor">
{% for supervisor in supervisors %}
    <option value="{{ supervisor.id }}" {% if user.supervisor.isSelected(user.getSupervisor()) %} selected{% endif %}>{{ supervisor.getName() }} {{ supervisor.getFirstName() }}</option>
{% endfor %}
</select>


<select name="cities">
{% for city in cities %}
    <option value="{{ city.id }}" {% if city.isSelected(user.getCity()) %} selected{% endif %}>{{ city.getZipcoded() }} {{ city.getName() }}</option>
{% endfor %}
</select>

如果您使用doctrinepropel,您甚至可以通过在每个模型中注入Selectable trait来修改模型生成器,从而避免识别哪个对象可选择的问题