使用占位符Viewhelper

时间:2018-08-21 14:34:08

标签: zend-framework zend-framework3 zend-view

我曾经处理过部分部件,现在我想要一个类似信息框的东西,里面应该有更多的信息。

我有点困惑如何将数据提供给占位符。

我做了什么: 我还有一个额外的layoutfile。 layout2.phtml

<?php $this->placeholder('content')->captureStart(); ?>

<div class="row">
    <div class="col-md-8">
    <?= $this->content; ?>
    </div>
    <div class="col-md-4">
        <div class="panel panel-default">
          <div class="panel-heading">
            <h3 class="panel-title">additional part info</h3>
          </div>
          <div class="panel-body">
          <strong>Approval Status</strong><p>
            <strong>Alerts</strong> <p>
            there are 
           <?= $this->AnzahlAlerts?> Alerts at the moment<p>
            <strong>MoM</strong><p>
            <p>see MoM here</p>            

          </div>
        </div>
    </div>
</div>

<?php 
  $this->placeholder('content')->captureEnd(); 
  echo $this->partial('layout/layout', 
          ['content'=>$this->placeholder('content')]); 
?>

占位符框将如我所愿显示。

但是我不会得到$this->AnzahlAlerts的值。我认为必须将其提供给viewmodell,所以我尝试如下:

控制器/ showAction

  return new ViewModel([
                 'unitid' => $unit,
                 'part' => $part,
                 'dcls' => $this->table->fetchPartDcl($part,$dclid),
                 'pads' => $this->padtable->fetchPadPart($part, $unit),
                  'heritage' => $this->table->getHeritage($part,  $projectid), //$this->unitpartTable->fetchHeritage($part)
                  'AnzahlAlerts' => $this->padtable->countAlert($part)
               ]);

我的**onDispatchAction**在这里完成:

public function onDispatch(MvcEvent $e)
    {
        $response = parent::onDispatch($e);
        $this->layout()->setTemplate('layout/layout2');
        return $response;
    }

我的问题是,什么是错误,给出值AnzahlAlerts的位置在哪里?

编辑:计数记录

这是我的模型功能:

public function countAlert($partnumber)
    {
        $statment = "SELECT  count(*) as AnzahlAlerts
        from t_part_alert

        WHERE t_part_alert.Part_Number = '" . $partnumber. "';";
      //  AND  t_unit_part.UnitID =" . $unitid;

        return  $this->tableGateway->adapter->query($statment, "execute");  
}

只是因为这是我的问题。

1 个答案:

答案 0 :(得分:1)

您像这样呼叫并打印部分内容:

<?= $this->partial('partial/file/alias') ?>

这将导致在您调用它的当前局部中渲染局部。 view_manager配置中的名称为partial/file/alias

如果要将变量传递到部分文件,则必须将数组作为第二个参数传递,如下所示:

<?= $this->partial(
    'partial/file/alias',
    [
        'param1' => 'value1',
        'param2' => 'value2',
    ]
) ?>

数组的键成为被调用部分中的参数。因此,在部分中,您可以像这样打印变量:

<?= $param1 ?>

要用数据填充第二部分时,应从控制器开始,将所需的数据传递到第一部分,然后从那里填充第二部分。


另一种方法(更困难的方法)是在Controller中预先渲染部分,然后从Controller函数返回渲染的整体。

就个人而言,我反对这样做,因为那样您就不会分开关注点(查看与处理)。

但是,当然有可能;)


从控制器到第二部分的完整示例

class AwesomeController
{
    public function demoAction()
    {
        // do stuff to create the values
        return [
            'key1' => 'value1',
            'key2' => 'value2',
            'fooKey1' => 'barValue1',
            'fooKey2' => 'barValue2',
        ];
    }
}

所以现在有4个值。这些将传递给demo.phtml。但是,最后2个值用于子部分,因此我们必须调用它。我们甚至循环播放,因为我们想多次显示它们!

<div class="row">
    <div class="col-8 offset-2">
        <table class="table table-striped table-bordered">
            <tr>
                <th><?= $this->translate('Key 1') ?></th>
                <td><?= $this->escapeHtml($key1) // <-- Look, the key from Controller is now a variable! This is ZF magic :) ?></td>
            </tr>
            <tr>
                <th><?= $this->translate('Key 2') ?></th>
                <td><?= $this->escapeHtml($key2) ?></td>
            </tr>

            <?php for ($i = 0; $i < 3; $i++) : ?>
                <?= $this->partial(
                    'custom/partial', 
                    [
                        'value1' => $fooKey1, // <-- Look, the key from Controller is now a variable! This is ZF magic :) 
                        'value2' => $fooKey2,
                    ]
                ) ?>
            <?php endfor ?>
        </table>
    </div>
</div>

现在,以上位要求使用名称为custom/partial的Partial。必须注册。下面的示例配置(从模块中放入module.config.php中):

'view_manager'    => [
    'template_map'        => [
        'custom/partial' => __DIR__ . '/../view/partials/demo-child.phtml',
        //  -- ^^ name             -- ^^ location
    ],
    'template_path_stack' => [
        __DIR__ . '/../view',
    ],
],

文件demo-child.phtml

<tr>
    <th><?= $this->translate('Value 1') ?></th>
    <td><?= $this->escapeHtml($value1) // <-- Look, from above partial - ZF magic :) ?></td>
</tr>
<tr>
    <th><?= $this->translate('Value 2') ?></th>
    <td><?= $this->escapeHtml($value2) ?></td>
</tr>
相关问题