Yii Framework Forum: Consider adding this to CController - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Consider adding this to CController

#1 User is offline   dckurushin 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 392
  • Joined: 16-March 11
  • Location:Israel

Posted 23 August 2011 - 04:06 PM

/**
	 * 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');

0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,879
  • Joined: 17-January 09
  • Location:Russia

Posted 24 August 2011 - 10:50 AM

What's the benefit of having such inconsisting assignments all over the place instead of just one assignment?
Yii 2.0 Development Cookbook, Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

#3 User is offline   dckurushin 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 392
  • Joined: 16-March 11
  • Location:Israel

Posted 25 August 2011 - 12:15 PM

View Postsamdark, on 24 August 2011 - 10:50 AM, said:

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
0

#4 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,454
  • Joined: 10-October 10
  • Location:Denmark

Posted 25 August 2011 - 12:24 PM

What's so wrong with
$this->render('view', array('model' => $model, 'someVar' => $someVar));

"Less noise - more signal"
0

#5 User is offline   Dana 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 243
  • Joined: 18-February 10
  • Location:Florida, USA

Posted 25 August 2011 - 12:44 PM

/agree Jacmoe -- I think it's much simpler and elegant the way it is currently.
0

#6 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,454
  • Joined: 10-October 10
  • Location:Denmark

Posted 25 August 2011 - 12:51 PM

Forgot to remember 'compact' :
$this->render('view', compact('model', 'someVar'));
;)
"Less noise - more signal"
0

#7 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 615
  • Joined: 08-May 09

Posted 25 August 2011 - 12:57 PM

View Postjacmoe, on 25 August 2011 - 12:24 PM, said:

What's so wrong with
$this->render('view', array('model' => $model, 'someVar' => $someVar));



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

1

#8 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,454
  • Joined: 10-October 10
  • Location:Denmark

Posted 25 August 2011 - 01:04 PM

I beat you to it by six minutes! :lol:
"Less noise - more signal"
0

#9 User is offline   mbi 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 615
  • Joined: 08-May 09

Posted 25 August 2011 - 01:09 PM

View Postjacmoe, on 25 August 2011 - 01:04 PM, said:

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


weird :huh:
0

#10 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,454
  • Joined: 10-October 10
  • Location:Denmark

Posted 25 August 2011 - 01:26 PM

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. :)
"Less noise - more signal"
0

#11 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,879
  • Joined: 17-January 09
  • Location:Russia

Posted 25 August 2011 - 02:03 PM

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.
Yii 2.0 Development Cookbook, Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.

Support me so I work on Yii fulltime: https://www.patreon.com/samdark
0

#12 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,454
  • Joined: 10-October 10
  • Location:Denmark

Posted 25 August 2011 - 02:11 PM

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

View Postsamdark, on 25 August 2011 - 02:03 PM, said:

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.

I also like that it's passed explicitly in one place - instead of scattered.
"Less noise - more signal"
0

#13 User is offline   dckurushin 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 392
  • Joined: 16-March 11
  • Location:Israel

Posted 25 August 2011 - 05:38 PM

Nice function "compact", don't knew it even exist :blink:
0

#14 User is offline   Maurizio Domba Cerin 

  • Yii - Yesss It Is !!!
  • Yii
  • Group: Yii Dev Team
  • Posts: 4,535
  • Joined: 12-October 09
  • Location:Croatia

Posted 26 August 2011 - 01:56 AM

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 :)
Find more about me.... btw. Do you know your WAN IP?
0

#15 User is offline   dckurushin 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 392
  • Joined: 16-March 11
  • Location:Israel

Posted 26 August 2011 - 05:35 AM

View Postmdomba, on 26 August 2011 - 01:56 AM, said:

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
0

#16 User is offline   jacmoe 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 3,454
  • Joined: 10-October 10
  • Location:Denmark

Posted 26 August 2011 - 09:00 AM

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. :)
"Less noise - more signal"
0

#17 User is online   tri 

  • Elite Member
  • Yii
  • Group: Moderators
  • Posts: 1,743
  • Joined: 20-November 08
  • Location:Stockholm, Sweden

Posted 26 August 2011 - 09:10 AM

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
Don't forget to read The Definitive Guide to Yii (1.1 en) (1.1 sv) (2.0 en) | The class reference (1.1) (2.0) has the details
0

#18 User is offline   ekerazha 

  • Advanced Member
  • PipPipPip
  • Yii
  • Group: Members
  • Posts: 541
  • Joined: 10-October 08
  • Location:European Union

Posted 26 August 2011 - 12:29 PM

Comparatively, compact(mt_rand()) is about 130% faster than hash('sha256', mt_rand())
Yii user #37
0

#19 User is offline   kernel 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 91
  • Joined: 22-November 10

Posted 27 August 2011 - 07:09 AM

View Postjacmoe, on 25 August 2011 - 12:51 PM, said:

Forgot to remember 'compact' :
$this->render('view', compact('model', 'someVar'));
;)


That's good :mellow:
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users