简体   繁体   中英

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.

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

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

The basic ideas with view helpers is that they return strings or objects that implement __toString()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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