使用占位符 Viewhelper

Using Placeholder Viewhelper

我曾经使用过部分组件,现在我想要一个信息框之类的东西,其中应该填充额外的信息。

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

我做了什么: 我有一个额外的布局文件。 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,所以我尝试如下:

controller/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");  
}

只是因为这是我的问题。

您像这样调用并打印部分内容:

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

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

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

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

数组的键成为被调用部分的参数。所以 在局部 你可以像这样打印一个变量:

<?= $param1 ?>

当想要用数据填充第二个部分时,从控制器开始,您应该将您想要的数据传递给第一个部分,然后从那里填充第二个部分。


另一种更困难的方法是在 Controller 中预渲染部分,然后 return 从 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 的部分。这必须注册。下面的示例配置(放在模块的 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>