hi there
i dont have a problem with this :
‘value’=>’$data->item_id’
and no problem with this too:
‘value’=>’$data->item->category_id’
and this is my custom class:
class Helper {
public static function getString($inputList, $spaceCount = 3){
return implode(str_pad(' ', $spaceCount), $inputList);
}
}
when i use ‘value’=>’$data->item_id’ like this again no problem:
‘value’=> "Helper::getString(array(
\"<a href=’".Yii::app()->createUrl(’/item/view’).’&id=$data->item_id’."’>\".
\"<img src='".Yii::app()->baseUrl."/image/categories/".'$data->item_id'. "'>\".
\"</a>\" ));",
but when use ‘value’=>’$data->item->category_id’ a problem occur :
‘value’=> "Helper::getString(array(
\"<a href='".Yii::app()->createUrl('/item/view').'&id=$data->item_id'."'>\".
\"<img src='".Yii::app()->baseUrl."/image/categories/".'$data->item->category_id'. "'>\".
\"</a>\" ));",
and this is error msg:
Object of class Item could not be converted to string
thanks for thought on this
Keith
(Kburton)
June 9, 2014, 8:55pm
2
I highly recommend that you use anonymous functions rather than trying to manage so much quote escaping. Also, the CHtml::link() method can help you with your links.
'columns'=>array(
array(
'name'=>'blah',
'value'=>function($data){
$imageTag = CHtml::image(Yii::app()->baseUrl . '/images/categories/'
. $data->item->category_id);
$linkTag = CHtml::link($imageTag, array('/item/view', 'id'=>$data->item_id));
return Helper::getString(array($linkTag));
},
),
),
Thanks
But Because of my php version i cant use anonymous functions
Keith
(Kburton)
June 10, 2014, 8:56am
4
Well, just create a function in your view file to serve the same purpose.
Somewhere near the top of the view file:
function getBlahColumn($data)
{
$imageTag = CHtml::image(Yii::app()->baseUrl . '/images/categories/' . $data->item->category_id);
$linkTag = CHtml::link($imageTag, array('/item/view', 'id'=>$data->item_id));
return Helper::getString(array($linkTag));
}
Then your column config becomes:
'value'=>'getBlahColumn($data)',
Feel free to wrap the function definition in a separate class or model as appropriate.
With this approach, you wrap far less of your code in a string, making it much easier to understand.
thank you
but actually i dont want to have a func definition in every view file, if i have these definition so i only process input array in each view file nd dont need that helper class
how can i use my helper class with out anything between …
thanks for you help
Keith
(Kburton)
June 11, 2014, 7:48am
6
I don’t understand. How is a function definition worse than a string expression which is evaluated? Especially one which has become so complicated that you’re unable to debug it.
thanks for ur replay
my only problem is that i want to concat $data->item->category_id with a string
joblo
(Joe)
June 11, 2014, 7:51pm
8
Instead of using anonymus functions to avoid much quote escaping you can use callable methods of your model, the current controller or a static method of a helper class.
See this wiki