/**
* 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');
samdark
(Alexander Makarov)
August 24, 2011, 3:50pm
2
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
And almost in all frameworks there is this assign…
people adapted to it(привычка)…
like ZF
jacmoe
(Jacob Moena)
August 25, 2011, 5:24pm
4
What’s so wrong with
$this->render('view', array('model' => $model, 'someVar' => $someVar));
danaluther
(Dana Luther)
August 25, 2011, 5:44pm
5
/agree Jacmoe – I think it’s much simpler and elegant the way it is currently.
jacmoe
(Jacob Moena)
August 25, 2011, 5:51pm
6
Forgot to remember ‘compact’ :
$this->render('view', compact('model', 'someVar'));
mbi
(mbi)
August 25, 2011, 5:57pm
7
$this->render('view', compact('model', 'someVar'));
jacmoe
(Jacob Moena)
August 25, 2011, 6:04pm
8
I beat you to it by six minutes!
jacmoe
(Jacob Moena)
August 25, 2011, 6:26pm
10
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.
samdark
(Alexander Makarov)
August 25, 2011, 7:03pm
11
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.
jacmoe
(Jacob Moena)
August 25, 2011, 7:11pm
12
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.
I also like that it’s passed explicitly in one place - instead of scattered.
dckurushin
(Diavolonok)
August 25, 2011, 10:38pm
13
Nice function “compact”, don’t knew it even exist
mdomba
(Maurizio Domba Cerin)
August 26, 2011, 6:56am
14
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
dckurushin
(Diavolonok)
August 26, 2011, 10:35am
15
mdomba:
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
One call of simple active record instance is like doing 10000 times compact
jacmoe
(Jacob Moena)
August 26, 2011, 2:00pm
16
It’s great that the Yii team is performance oriented.
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.
tri
(tri - Tommy Riboe)
August 26, 2011, 2:10pm
17
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
ekerazha
(Ekerazha)
August 26, 2011, 5:29pm
18
Comparatively, compact(mt_rand()) is about 130% faster than hash(‘sha256’, mt_rand())