More smart $htmlOptions

I don’t know the plan about how html elements will be rendered on Yii 2.0 but if the lovely $htmlOptions property will be around I expect to be more “smart”.

I have found ( specially when designing extensions ) that the 1 level limitation of this property is annoying.

Most common case is the ‘class’ when I have to merge default options with the once use has provided. Something like:


$default = array( "class"=>array("default"));//merged with

$specific = array("class"=>array("spec"));//and end up to

…class="default spec"… Could be real nice and handy.

Considering HTML5 data used more and more,it would be nice if I could do something like:


$htmlOptions = array("data"=>array("id"=>1, "name"=>"tydeas"));

and as a result …data-id=1 data-name="tydeas" in my element.

In your example wouldn’t be easier and less typing to just write data-id and data-name ?

For the other example… what if someone need a different thing… that the "spec" overwrites the "default" ?

Yes if you know that there are 2 params for the data it could be easier. But what if you have a json that you need to decode and pass as data params? Wouldn’t it be more handy just to set data to the array? Imho having something:




$htmlOptions['data']=CJSON::decode($json);

rather


foreach(CJSON::decode($json) as $key => $value)

{ $htmlOptions['data-'.$key] = $value; }

Is much more less typing and more clear. Other example could be $htmlOptions[‘data’] = $model->attributes;

Related to the overwritting of the class, I thought about it too. And as thing get smarter they get more complex as well.

Taking the creation of an extension as an example, the author of the extension is the one how decides if the option will be able to ovewritten or not. So from this aspect if you set the class property to as string it will overwriten in merging but if you set into an array it won’t be overwritten but merged.

Can you explain me the use of json for configuring PHP… I use it only on the JS side…

Please don’t take this examples too strict. Json encoded data may occure on server side as well imho. If you feel the example was bad stict to the model attributes that really possible.

I would just like to see a real life example for the functioning you are requesting… as for json… I don’t see the point of using json array in PHP… PHP has it’s array…