首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用占位符Viewhelper

使用占位符Viewhelper
EN

Stack Overflow用户
提问于 2018-08-21 14:34:07
回答 1查看 49关注 0票数 0

我已经与部分工作,现在我想有一些类似的信息框,将填补额外的信息。

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

我所做的:我有一个额外的布局文件。layout2.phtml

代码语言:javascript
复制
<?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

代码语言:javascript
复制
  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**在这里完成:

代码语言:javascript
复制
public function onDispatch(MvcEvent $e)
    {
        $response = parent::onDispatch($e);
        $this->layout()->setTemplate('layout/layout2');
        return $response;
    }

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

编辑:计数记录

这是我的模型功能:

代码语言:javascript
复制
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");  
}

就因为这是我的问题。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-22 13:03:02

您调用并打印一个部分,如下所示:

代码语言:javascript
复制
<?= $this->partial('partial/file/alias') ?>

这将导致在您调用它的当前分部中呈现一个分部。view_manager配置中的名称是partial/file/alias

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

代码语言:javascript
复制
<?= $this->partial(
    'partial/file/alias',
    [
        'param1' => 'value1',
        'param2' => 'value2',
    ]
) ?>

数组的键成为称为分部的参数。因此,在部分中,您可以打印如下所示的变量:

代码语言:javascript
复制
<?= $param1 ?>

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

另一种更困难的方法是预渲染控制器中的部分,然后从Controller函数返回呈现的整体。

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

但当然,这是可能的;)

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

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

所以现在有4个值。这些被传递给demo.phtml。然而,最后两个值是次部分的值,所以我们必须将其称为.我们甚至循环,因为我们想要向他们展示不止一次!

代码语言:javascript
复制
<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中):

代码语言:javascript
复制
'view_manager'    => [
    'template_map'        => [
        'custom/partial' => __DIR__ . '/../view/partials/demo-child.phtml',
        //  -- ^^ name             -- ^^ location
    ],
    'template_path_stack' => [
        __DIR__ . '/../view',
    ],
],

文件demo-child.phtml

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51951053

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档