pedro84
(A Osmialowski)
July 16, 2012, 2:18pm
1
Howdy,
is it possible to change templates of the code generated by Gii?
Example,
public function actionView($id)
{
$this->render('view',array(
'model'=>$this->loadModel($id),
));
}
I’d like to replace tabs with spaces
and spaces before/after => in ‘model’=>$this->loadModel($id)
Are Gii templates editable?
rootbear
(Alex Xm)
July 16, 2012, 2:25pm
2
of course you can, even yii framework itself…
am i kidding? yes.
i would suggest, create your own template so you don’t mess up the default one
copy file from
default@ yii\framework\gii\generators\controller\templates\default
to
mytemp@ yii\framework\gii\generators\controller\templates\mytemp
and modify them accordingly
in gii form when you create controller, specify which template you want to use, in this case: mytemp
this way, you have your own templates at the same time keep the original ones.
i always hesitate to mess around with original code.
pedro84
(A Osmialowski)
July 16, 2012, 3:34pm
3
Thanks @rootbear . I suppose I’m blind. Thanks.
teo_ne
(Matteo Scandolo)
November 11, 2013, 10:32am
4
Hi everybody,
thanks to @rootbear I have overridden Gii Templates creating another copy. Due to my layout I would like to be able to add some classes to auto-generated Form labels and field but I’m stucked.
Gii use this code to generate labels and fields:
<?php echo "<?php echo ".$this->generateActiveLabel($this->modelClass,$column,)."; ?>\n"; ?>
the code looks very familiar and I simply thought that it could work like Chtml::label()
$form->label($model,'type',array('class'=>'control-label col-lg-3'));
so I tried adding an array of htmlOptions like this
<?php echo "<?php echo ".$this->generateActiveLabel($this->modelClass,$column,array('class'=>'control-label col-lg-3'))."; ?>\n"; ?>
but unluckly it got no effects, simply seems to be ignored. I try to have a look but I can’t find any documentation on generateActiveLabel(). Does anyone have advice on this?
Thanks in advance
teo_ne
(Matteo Scandolo)
November 11, 2013, 11:56am
5
Ok, if someone need this,
I found out that "generateActiveLabel()" is declared in CrudCode.php under
/framework/gii/generators/crud/
I edited that function in this way
public function generateActiveLabel($modelClass,$column,$htmlOptions = null)
{
return "\$form->labelEx(\$model,'{$column->name}',array('class'=>'".$htmlOptions."'))";
}
So I can pass some css class to the tempalte generator. The same can be done to "generateActiveField()" function, that generate the input for the form.
Hope this can help someone.
ahmed201
(Rifajas)
November 12, 2013, 7:05am
6
teo_ne:
Ok, if someone need this,
I found out that "generateActiveLabel()" is declared in CrudCode.php under
/framework/gii/generators/crud/
I edited that function in this way
public function generateActiveLabel($modelClass,$column,$htmlOptions = null)
{
return "\$form->labelEx(\$model,'{$column->name}',array('class'=>'".$htmlOptions."'))";
}
So I can pass some css class to the tempalte generator. The same can be done to "generateActiveField()" function, that generate the input for the form.
Hope this can help someone.
really nice work you done toe_ne
I wrote even more better:
in CrudCode.php file
public function generateActiveLabel($modelClass,$column,$htmlOptions = null)
{
$hp = '';
if ( is_array($htmlOptions) && count($htmlOptions) )
{
$hp = 'array (';
foreach ($htmlOptions as $name=>$value){
$i++;
$hp .= "'{$name}'=>'{$value}'";
if ($i<count($htmlOptions))
$hp .= ', ';
}
$hp .= ')';
}
return "\$form->labelEx(\$model,'{$column->name}', {$hp})";
}
in _form.php file:
<?php echo "<?php echo ".$this->generateActiveLabel($this->modelClass,$column, array('class'=>'control-label'))."; ?>\n"; ?>