【问题标题】:Twig template - looping inputsTwig 模板 - 循环输入
【发布时间】:2016-05-07 13:15:45
【问题描述】:

我是使用 PHP 的 Twig 模板引擎,但是在呈现某些表单时遇到问题,特别是自动显示“选择”输入的当前选定项。

我正在从数据库中提取某些信息集,它们是:宠物、品种、位置,它们返回各自的值,例如“猫,狗”。

我不确定如何向用户显示这些输入,因为输入选择值会与文本不同,我确实想过使用 swtich 但不确定它是否是最佳选择。

这是我当前的代码:

$select = SELECT pet, breed, location FROM...;
$pet = array('cat', 'dog');

页面然后使用这些变量呈现视图。

<select class="form-control">
{% for p in pet %}
    {% if p == select.pet %}
        <option value="{{ select.pet }}" selected>{{ select.pet|capitalize }}</option>
    {% else %}
        <option value="{{ p }}">{{ p|capitalize }}</option>
    {% endif %}
{% endfor %}

正如你所看到的,我目前需要依靠“pet”变量来循环输入,这就是我想要改变的,你也可以看到选择的文本只是值但大写意味着它是有限的。

如何创建不需要数组 $pet 的 twig 循环,并允许我指定每个值 => 文本,如下所示:

<option value="{{ select.pet }}" selected>Foo dog</option>
<option value="{{ select.pet }}">Bar cat</option>

【问题讨论】:

  • 您应该有 2 个查询,一个用您数据库中的所有动物填充一个数组,然后您在 twig 中循环以填充您的选择,另一个用于获取所选动物的所有详细信息。 Imo 你应该使用动物的id,而不是它的类型作为选择中的值。
  • 我将如何填充动物数组,这需要单独的字段吗?数据库中的 pet 字段目前是一个包含这些宠物名称的 ENUM,您是否可以提供一个示例来说明您将如何以 twig 语法进行建议?

标签: php loops twig template-engine


【解决方案1】:

以下代码只是一个示例,说明如何实现您想要的目标。我没测试过

表格

---------------
| animal_type |
---------------
id [PK]
title


---------------
|   animals   |
---------------
id [PK]
name
animal_type_id [FK, animal_type.id]
breed
birthdate
...

php

<?php
    $animal_types = $animals = [];
    $dbh = new PDO('mysql:host=' . $host . ';dbname=' . $db, $user, $pass);
    //Raise exception on errors
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    //Target all available types:
    $stmt_types = $dbh->prepare('SELECT id, title FROM animal_type ORDER BY title');

    if ($stmt_types->execute()) {
        $animal_types = $stmt_types->fetchAll();
    }

    if (isset($_POST['cbo_animal_type']) && is_numeric($_POST['cbo_animal_type'])) {
        $stmt_animals = $dbh->prepare('SELECT * FROM animals WHERE animal_type_id = :animal_type_id ORDER BY name');
        if ($stmt_animals->execute()) {
            $animal_types = $stmt_animals->fetchAll([ 'animal_type_id' => $_POST['cbo_animal_type']);
        }       
    }

    echo $twig->render('path/to/view.html', [
        'animal_types'          => $animal_types,
        'animals'               => $animals,
        'selected_animal_type'  => isset($_POST['cbo_animal_type']) && is_numeric($_POST['cbo_animal_type']) ? $_POST['cbo_animal_type'] : -1,
    ]);

树枝

<form method="POST">
    <select name="cbo_animal_type">
    {% for animal_type in animal_types %}
        <option value="{{ animal_type.id }}"{% if selected_animal_type is defined and selected_animal_type = animal_type.id %} selected{% endif %}>{{ animal_type.title }}</option>
    {% endfor %}    
    </select>
</form>

{% if animals is defined and not animals is empty %}
<ul>
    <li>
        <div>
            <h1>{{ animal.name }}</h1>
            <h2>{{ animal.breed }}</h2>
            <p>
                {{ animal.birthdate }}<br />
            </p>
        </div>
    </li>
</ul>
{% endif %}

【讨论】:

    猜你喜欢
    • 2015-01-15
    • 2023-03-20
    • 2016-09-28
    • 1970-01-01
    • 2014-03-07
    • 1970-01-01
    • 1970-01-01
    • 2012-04-15
    • 2014-07-14
    相关资源
    最近更新 更多