开发者

Zend Framework: when to use placeholders, partials, and just plain assigning to view properties?

I've been reading through the Zend_View documentation, and I'm not quite sure I understand exactly when it's best to use placeholders, partials, and just using view properties. Here's my basic understanding:

Placeholders: use mostly when aggregating content, i.e. sidebar sections.

Partials: use when you want the view script to be rendered without the view's variable scope. But when are the time开发者_高级运维s that this would be optimal?

View properties: any other variables you need to pass to the view script that don't fit into the above.


Placeholders

As you mentioned, these are good for aggregating content. The most common task is to collect data from views and display the entire collection in your layout.

For example, say you know that every page requires jQuery. Your layout would then have something like this using the inlineScript placeholder helper

<?php echo $this->inlineScript()->prependFile('path/to/jquery.min.js') ?>
</body>

You views can then add scripts to the placeholder, eg

<?php $this->inlineScript()->appendFile('path/to/script.js') ?>

Partials

A view partial is a way to encapsulate data rendering. Particularly useful if you want to use the same markup in more that one place.

For example, say you show the same table format where only the data changes (think pagination). A view partial is perfect for this.

View Properties

Your controllers should assign properties to the view based on the specific task at hand. These properties are usually your models / collections of models or forms (when using Zend_Form), whatever your particular view needs to display.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜