is there any assign method to the View?

Is there any assign method to the CControler?

I know now only one way to assign variable to the View and it’s:




$this->render('index', array('a'=>'some stuff...');



I ended creating my own methods in the Controler named "assign" and "getAssign"




	public function assign($key, $value){

		$this->params[$key] = $value;

	}

	

	public function getAssign(){

		return $this->params;

	}



And than i can do




$this->render('index', parent::getAssign());



But this is some very basic stuff… in ZEND for example you have the magic set I think…

Here

$this->someParam = 3;

trigger an error…

Help please…

Don’t want to invent the wheel

Which error do you get?

Have you declared $someParam in Controller class?


public $someParam;

for example.

Then you can use directly in your view $this->someParam, without passing this variable.

Hope, I understood your problem correctly.

No, the problem is I want to assign variables to the view I render…

Like in Smarty for example, or even ZF

Look here, this is what im searching in Yii:

http://framework.zend.com/manual/en/zend.view.controllers.html

You pass the variables through as part of the render method. If you want to alter the variables by logic you can build an array as you go through the action and pass that in. There’s no equivalent to the:

$this->view->randomVarName = 3;

that you have in Zend.

I checked CContoller specification, and seems that there are no assign method.

But why you need it, passing parameters with render method should be enough.

Yes, Zend framework have assign method in its controllers(and magic method for assign variables to view), but not sure if Zend Framework’s render method(it belongs to view) allow any parameters

This would never work with the functions you have above:




 $this->someParam = 3;

 trigger an error...



You’d need to do:




$this->assign('someParam', 3);



Or, add a magic __set() method such as:




 ...

 public function __set($x,$y)

 {

    // sanity checking here to be sure property/method doesn't already exist

    // or verification that $x is in some array of pre-defined allowed values

    // else fall back to parent::__set()

    $this->assign($x,$y);

 }

 ...



That said, I think it completely misses the boat in terms of MVC and what Yii can do.

You’d probably be much better off making an Assignments CModel that can hold the params you want to pass through, or making a specific behavior that handles assignments of particular properties that you want to pass through.

Dana Hi, I know that I should use


$this->assign('someParam', 3);

cause this is my methods I added :rolleyes:

The problem is that I had to add the methods, and they don’t exist…


I don’t think that using assign method for the view, goes against MVC or anything…

This is all about comfort… I don’t like the idea that in every controller I should start array(‘a’=>1,‘b’=>‘abc’,‘c’=>$someobject etc.

So I added the assign, cause I couldn’t find anything in CController

And for the magic set


 public function __set($x,$y)

I think when I looked at the methods list of CController, he have some sort of __set

So I ended up with assign, it’s remind me Smarty + no conflict with existing methods


And in addition for the MVC question, You work with some data, and different models, and than you need to collect all the data, and pass it to the View to display…

assign is very efficient way to do so :unsure:

Sorry, I misunderstood, thought you were trying to do the automatic assignment AFTER adding those methods and couldn’t figure out why the magic get wasn’t working. My bad!!

If you continue with your $this->assign() method, you don’t NEED to pass it to the view, the view can access it directly using $this->getAssign(‘somevalue’) since the view will check the methods accessible to it from the controller using $this.

So:

on Controller - you change public static function getAssign() to public function getAssign( $key )

View can access it directly using $this->getAssign( ‘keyId’ ) to access just the pieces it needs when it needs them.

Yes, but I don’t like the getAssign method… too long…

I actually like the Yii extract …

Don’t know why don’t thought about it before…

I also don’t need getAssign ( should name it get assignments, cause this is what it do if you look at the code…)

But I deleted it, the best way to go is:




/**

	 * the variables for the view render

	 * @var array

	 */

	public $params = array();


	/**

	 * the assign method

	 * @param string $key

	 * @param mixed $value

	 */

	public function assign($key, $value){

		$this->params[$key] = $value;

	}

	

	/**

	 * call template render, with the params array we created in the life cycle

	 * @param string $tpl

	 */

	public function show($tpl = 'index'){

		$this->render($tpl, $this->params);

	}



And than in the SiteController etc.

I work like this




$answer =  $command->queryAll();

$this->assign('dbData', $answer);

$this->assign('anotherOne', $something);

$this->show('index');



And I think it’s better practice, than




$this->render('index', array('dbData'=>$answer, 'anotherOne'=> $something));



Your controller could be very short, and this is the right way to go, but this is not always the case…

If your controller is long and compicated, when you got to the bottom, you need to start to search all your variables, and create that array manually…

Why work hard? ;D

I’ve never had many problems with the passing variables to the view. I tend to keep controller actions short though, working on the principle that it’s only there to tie models and views together and that the business logic should exist in the models.

The only thing I would add to your solution is that you could have overridden the render method rather than creating show, enforcing the use of the assign() method.




public function render( $tpl = 'index' )

{

    parent::render( $tpl, $this->params );

}



To me, this looks like just another API to do the same thing.

Just as a hint, maybe you already noticed: you can add your Code (params, geter/setter) to "Controller", in protected/components.

This class has been generated by the webapp command which built your application skeleton. It is meant as a layer for customization between the frameworks core CController and the controllers you use in your app. I guess it would be the proper place for your additional methods.

this is my solution:




//in  Controller:


   private $_params;


   public function assign($key,$val){

     $this->_params[$key]  = $val;

  }


 public function render($view,$data=null,$return=false)

{

    if(!empty($this->_params)){

        $data =  $data === null?  $this->_params : CMap::mergeArray($this->_params,$data)  ;

   }

   

    return parent::render($view, $data , $return);

}






in your action method you can use the smarty like syntax :

$this->assign($someKey,$someValue);…

$this->render(‘someView’); // $this->render(‘someView’,array(‘keyX’=>$valueX));

if you want it to support the renderPartial method ,you can override the renderPartial method similarly , or it 's best to override the renderPartial because the render method call the renderPartial internally . :P

Oh the other hand, you could just ignore Yii’s view stuff and use Smarty instead…