Consider adding this to CController




/**

	 * 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 render($tpl = 'index'){

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

	}



example of usage in controller:




$this->assign('greetings', 'Yo man!');

$this->assign('model', $model);

$this->render('login');



What’s the benefit of having such inconsisting assignments all over the place instead of just one assignment?

The main benefit is that it is more confortable…

I could of course just follow the yii way and do


$data = array();


...

...


$data['a'] = ...




..

..

..

$data['b'] = ...


render('index', $data);

But the assign way looks more elegant and user friendly in my opinion…

Or maybe this is just my Smarty days spoiled me :lol:

And almost in all frameworks there is this assign…

people adapted to it(привычка)…

like ZF

What’s so wrong with


$this->render('view', array('model' => $model, 'someVar' => $someVar));

/agree Jacmoe – I think it’s much simpler and elegant the way it is currently.

Forgot to remember ‘compact’ :


$this->render('view', compact('model', 'someVar'));

;)


$this->render('view', compact('model', 'someVar'));

I beat you to it by six minutes! :lol:

weird :huh:

Well, to be honest: I had to look it up, because I haven’t used it since I used CakePHP… ;)

I will definitely be using it now, since I am a really lazy programmer.

Too bad I forgot about it.

The bottom line is that it simply doesn’t get less verbose than that. :)

After using Smarty long-long time ago I wanted this as well. Later Yii-way saved me a lot of time when reviewing existing code. You can tell what was passed to the view very fast by looking at just a single place.

Maybe you could use ‘compact’ for the CRUD generator instead of regular assoc array syntax?

That would make it even neater, and prevent feature suggestions such as this. :D

I also like that it’s passed explicitly in one place - instead of scattered.

Nice function “compact”, don’t knew it even exist :blink:

I personally don’t like the compact…

because it’s just another function call… could be it’s very fast… but again it’s a call to a function… it needs to process the parameters and build an array to be returned… so in the end - a bit slower code :)

if speed that important, why to use Yii at all?

When you use a framework, you get what you need, and a lot of code that you will never use ;D

One call of simple active record instance is like doing 10000 times compact :D

It’s great that the Yii team is performance oriented. :D

However, I trust the PHP team more than I trust myself, so I don’t think ‘compact’ will be slower than any of my own functions.

That function has had huge amount of eyeballs upon it. :)

I think the current format of the render() call should be preferred since it will give a hint that key/value doesn’t have to be the same. Perhaps the possible compact() call could be mentioned in a comment?

/Tommy

Comparatively, compact(mt_rand()) is about 130% faster than hash(‘sha256’, mt_rand())

That’s good :mellow: