如何使用 PHP 中的 phalcon 变量?

How to work with a phalcon variable inside PHP?

我在 controller 中创建了一个变量:

<?php

use Phalcon\Mvc\Controller;

class RestaurantreservationController extends Controller
{

    public function indexAction(){
        return $this->view->pick("reservation/listerReservation");
    }

    public function affecterReservTableAction($id) {
        $this->view->action_form = 'affecterReservTableExec';
        $this->view->titre = 'R&eacute;servation de table';
        $this->view->table_code = $id; // here is the variable I want to manipulate
        return $this->view->pick("reservation/reservTable");
    }

}
?>

在视图中 reservTable.phtml 我想使用变量 table_code :

<div class="input-control select">
        <select name="clt_id" id="clt_id">
            <option value="">--Choisir une table--</option>
            <?php
                $table_code = {{table_code}}; // it generates an error
                $tables = TableClient::lireParCritere([]);
                foreach ($tables as $table) {
                  $select = ($table_code == $table->table_code ? "selected" : "" );
                  ?>
                    <option <?php echo $select; ?> value="<?php echo $table->table_code; ?>"><?php echo $table->noms; ?></option>
                  <?php
                }
            ?>
        </select>
    </div>

如何使用它设置为select元素的选中元素?

问题是当您尝试将 {{table_code}} 分配给 $table_code 时,您混合了 phtml 和 Volt 语法。

伏特变量 {{ table_code }}$table_code 相同。

如果你用Phalcon那么最好用VOLT

当您创建 Volt 类型的模板时,您可以通过 {{ table_code }}

访问变量

如果你不想循环,你可以使用像

这样的东西
 {% for table in tables %}
   //do something
 {% endfor %}

Volt 也有很好的创建选择的功能

{{ select("table", table_code, 'using': ['table_code', 'table_noms']) }}

您在代码后面的三行中正确使用了它。在您尝试使用变量时,您处于 PHP 模式。这意味着您只需编写 PHP 代码,即 $table_code。形式 {{table_code}} 用于在 VOLT 模板内插值,不适用于 PHP 代码。

我实际上建议将所有 PHP 块转换为 VOLT 模板代码。