开发者

Zend Aggregation of data in view

开发者 https://www.devze.com 2023-03-16 12:10 出处:网络
I\'m new to zend and i\'m struggling with some items. I tried using Helpers, but then realized it doesn\'t do what I wanted it to do...

I'm new to zend and i'm struggling with some items. I tried using Helpers, but then realized it doesn't do what I wanted it to do... either i'm usi开发者_开发问答ng $this->placeHolder... wrong or it doesn't do what I want it to do

I want to do the following:

I want to create a custom class that basically has 2 methods addScriptContent, and getScriptContent...

addScriptContent adds what ever is passed into it to a string that is global for the page. getScriptContent will just output whatever data that has been added using addScriptContent

i.e.

this->addScriptContent('var x="foo";')
 ....some html
this->addScriptContent('var y="feee";')
  ....some html
echo this->getScriptContent()  //writes out var x="foo"; var y="fee";


The placeholder helper does exactly what you are after:

<?php $this->placeholder('scriptContent')->set('var x="foo";') ?>
....some html
<?php $this->placeholder('scriptContent')->append('var y="feee";') ?>
....some html
<?php echo $this->placeholder('scriptContent')?>

the key methods on the helper are set, append and prepend; which overwrite, add to, and add to the start of the content respectively.

If you really want to write your own helper for this, this wouldn't be too difficult. Take a look at the headTitle or headScript helpers for an example, but would be something along the lines of:

class My_View_Helper_ScriptContent extends Zend_View_Helper_Placeholder_Container_Standalone
{
    public function scriptContent($script)
    {
        $this->append($script);
    }
}

usage would then be:

<?php $this->scriptContent('var x="foo";') ?
....some html
<?php $this->scriptContent('var y="feeee";') ?>
....some html
<?php echo $this->scriptContent() ?>
0

精彩评论

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