Roby Sottini
Roby Sottini

Reputation: 2265

How to change default template in Yii2?

I am using the Yii 2 Advanced Application Template, the AdminLTE Asset Bundle and the Gii code generator.

Here is my example:

enter image description here

I need to change the template so I can remove the "Create Lab Tipos Movimientos" button (and modify some things more).

I am removing every button after Gii create the CRUD but I would like to change the template so Gii can do it automatically.

Upvotes: 0

Views: 1404

Answers (2)

ManuelCash
ManuelCash

Reputation: 153

I have not done it myself, but I found this Guide by SamDark in Github that explains how to create your own template. This is the url: https://github.com/yiisoft/yii2-gii/blob/master/docs/guide/topics-creating-your-own-templates.md

Additionally, if you just want to eliminate the "Create Lab Tipos Movimiento" button you can try modifying the current template which if I am not wrong is located inside the folder vendor/yiisoft/yii2-gii/generators/crud/default/views and the file should be index.php. There you can try deleting or better yet commenting the part of the code that says:

<p>
    <?= "<?= " ?>Html::a(<?= $generator->generateString('Create ' . Inflector::camel2words(StringHelper::basename($generator->modelClass))) ?>, ['create'], ['class' => 'btn btn-success']) ?>
</p>

I suggest you to make a copy of the files you modify just in case anything goes wrong.

Hope this helps you. Have a great day.

EDIT:

Additionally following the answer of schmunk to a very similar question in stack overflow found here: How to use custom templates in gii (using Yii 2)

There is apparently a Gii extension in beta phase to help you in this situation called yii2-giiant found here: https://github.com/schmunk42/yii2-giiant (maybe there are similar extensions that are in a more advanced phase of development, google search should help with that)

Upvotes: 1

ScaisEdge
ScaisEdge

Reputation: 133370

Once you have create your own template for gii
You can change the default template for gii assigning the new template values in main.php (main-local.php)

assigning the proper parameter to gii module

   .....
   $config['modules']['gii'] = [
        //'class' => 'yii\gii\Module',  
        'class' => 'your_gii_module_path\Module',         
        'allowedIPs' => ['127.0.0.1', '::1', ],  
        'generators' => [ //here
            'crud' => [ // generator name
                //'class' => 'yii\gii\generators\crud\Generator', // generator class
                'class' => 'your_gii_module_path\generators\crud\Generator', // generator class
                'templates' => [ //setting for out templates
                    // template name => path to template
                    'your_crud_entry' => 'your_absolute_path_\your_gii_module_path\generators\crud\default', 
                ]
            ]
        ],
    ];
    .......

Upvotes: 2

Related Questions