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.

 

 

 

By admin

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.