简体   繁体   English

使用Zend Framework View Helper返回数据

[英]Returning Data with a Zend Framework View Helper

I am creating a view helper to deal with returning some data screen scraped from elsewhere and my question is, what is the best way to gather the collected data and return it for use in the view? 我正在创建一个视图帮助程序来处理返回从其他位置刮取的某些数据屏幕的问题,我的问题是,收集收集的数据并将其返回以供视图使用的最佳方法是什么?

Assume the view helper goes out and successfully grabs a few divs full of data. 假设视图助手退出并成功获取了一些充满数据的div。

Should I return an array of data and iterate over that in the view? 我应该返回一个数据数组并在视图中对其进行迭代吗?

$i = 0
foreach($this->myHelper as $noob) {
    echo '<div><h3>'.$noob[$i][0].'</h3><a href="'.$noob[$i][1].'">'.$noob[$i][2].'</a></div>';
    $i++;
}

or perhaps build the output in the view helper and return the whole lot as a string? 还是在视图帮助器中构建输出并以字符串形式返回全部?

echo $this->myHelper;
/* giving me something like: 
    <div><h3>Foo1</h3><a href="bar1.htm">bar1</a></div>
    <div><h3>Foo2</h3><a href="bar2.htm">bar2</a></div>
*/

or return an object rather than an array or some other way I am missing (or screwing up in the above examples)? 还是返回一个对象,而不是一个数组或我缺少的某种其他方式(或者在上面的示例中搞砸了)? Thanks in advance. 提前致谢。

I'd add a view partial argument and pass the data to the nominated partial script, returning the result using the partial or partialLoop helpers (depending on your data), for example 我将添加一个视图局部参数,并将数据传递给指定的局部脚本,例如,使用partialpartialLoop帮助器(取决于您的数据)返回结果

<?php echo $this->myHelper('path/to/partial.phtml') ?>

and in your helper 和你的帮手

return $this->partial($partialScript, $data);

Edit If you wanted to simplify things, you could make the partial argument optional and if it was missing, build your HTML string internally. 编辑如果您想简化事情,可以将part参数设为可选,如果缺少,则在内部构建HTML字符串。

The basic ideas with view helpers is that they return strings or objects that implement __toString() 视图助手的基本思想是它们返回实现__toString()字符串或对象

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

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