简体   繁体   English

使用占位符Viewhelper

[英]Using Placeholder Viewhelper

I have worked with partials and now I'd like to have something like an info box which shall be filled with additional information. 我曾经处理过局部函数,现在我想要一个类似信息框的内容,其中应包含其他信息。

I'm a bit confused how to give the data to the placeholder. 我有点困惑如何将数据提供给占位符。

What I did: I have an additional layoutfile. 我做了什么:我还有一个额外的layoutfile。 layout2.phtml 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')]); 
?>

The placeholderbox will be shown like I wanted. 占位符框将如我所愿显示。

But I won't get the value of $this->AnzahlAlerts . 但是我不会得到$this->AnzahlAlerts的值。 I thought it must be given to the viewmodell, so I tried as follows: 我认为必须将其提供给viewmodell,因此我尝试如下:

controller/showAction 控制器/的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)
               ]);

My **onDispatchAction** here for completion: 我的**onDispatchAction**在这里完成:

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

My Questions are, what is the error, and where is the postion to give the value AnzahlAlerts ? 我的问题是,什么是错误,给出AnzahlAlerts值的位置在AnzahlAlerts

EDIT: count records 编辑:计数记录

This is my modelfunction: 这是我的模型功能:

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

Just because it my be the problem. 只是因为这是我的问题。

You call and print a partial like so: 您可以像这样调用并打印部分内容:

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

This causes a partial to be rendered within the current partial where you called it. 这将导致局部在您调用它的当前局部内渲染。 The name in the view_manager config is partial/file/alias . view_manager配置中的名称为partial/file/alias

If you wanted to pass variables to the partial file, you would have to pass an array as the second parameter, like so: 如果要将变量传递到部分文件,则必须将数组作为第二个参数传递,如下所示:

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

The keys of the array become parameters in the called partial. 数组的键成为被调用的partial中的参数。 So in the partial you can print a variable like so: 因此,可以在partial中打印如下变量:

<?= $param1 ?>

When wanting to fill the 2nd partial with data, starting at the controller, you should pass the data you want to the first partial and from there fill the second. 当要用数据填充第二部分时,从控制器开始,应将所需的数据传递到第一部分,然后从那里填充第二部分。


Another option, the more difficult way, is to pre-render the partials in the Controller and then return the rendered whole from the Controller function. 另一种选择(更困难的方式)是在Controller中预渲染部分,然后从Controller函数返回已渲染的整体。

Personally, I'm opposed to this as you would then not be separating concerns (viewing vs handling). 就我个人而言,我反对这样做,因为那样您就不会分开关注点(查看与处理)。

But of course, it's possible ;) 但是,当然可以;)


Full example from Controller into 2nd partial 从控制器到第二部分的完整示例

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

So now there's 4 values. 所以现在有4个值。 These get passed to demo.phtml . 这些被传递给demo.phtml However, the last 2 values are for the sub-partial, so we must call that. 但是,最后2个值用于子部分,因此我们必须调用它。 We even loop that, because we want to show them more than once! 我们甚至循环播放,因为我们想多次显示它们!

<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>

Now, the above bit calls for the partial with name custom/partial . 现在,以上位要求使用名称custom/partial This must be registered. 必须注册。 Example config below (place in module.config.php from your module): 下面的示例配置(从模块中module.config.php ):

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

File demo-child.phtml 文件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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM