Accessing controller data from a partial viewscript

When using an inline partial viewscript in my form, I sometimes need access to data. I can accomplish this by using a setter and getter with a public function in my form. We’ll use the action “guinness” as the controller action in this example.

application\modules\project\forms\guinness\base.php

protected $_id;

public function setId($id)
{
    $this->_id = $id;
    return $this;
}

public function getId()
{
    return $this->_id;
}

public function getPintCount()
{
    $model = new PubModel();
    return $model->countPints(this->getId());
}

application\modules\project\views\scripts\guinness\page.phtml

<?=$this->render('_partial.phtml');?>

application\modules\project\controllers\GuinnessController.php

<?php
$form = new SomeForm();
$form->setId(123);
?>

application\modules\projects\views\scripts\guinness\_partial.phtml

<?=$this->element->getPintCount();?>

 

This way I can get the content view my connected partials which in essence are a copy of the viewscript.

Seems a long way round, but it keeps everything nicely separated.

 

 

 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>