Bootstrap Modal render function

Hi all, i read Yii2 Bootstrap Modal documentation and i see the render method, but i can’t find the right way to use.

I need to render a view into my modal.

For now i find a working different solution:


Modal::begin([

    'id'=>'modal_PrivacyPolicy',

    'size'=>'modal-lg',

]);


echo "<div id='modalContent'>";

echo  \Yii::$app->view->render('@app/views/privacy/_privacy_policy');

echo "</div>";


Modal::end()

but i think there’s a better way using the modal render method.

Any suggestion?

Thank’s

this is my code, you could refer to it.

button to show modal




<?= Html::button('Price List', [                        

     'value' => Yii::$app->urlManager->createUrl('/some-controller/some-action'),

     'class' => 'btn btn-primary',

     'id' => 'BtnModalId',

     'data-toggle'=> 'modal',

     'data-target'=> '#your-modal',

]) ?>



define modal outside active form




<?php ActiveForm::end(); ?>

    <?php                 

        Modal::begin([

                'header' => 'your-header',

                'id' => 'your-modal',

                'size' => 'modal-md',

            ]);

        echo "<div id='modalContent'></div>";

        Modal::end();

    ?>



your javascript




$('#BtnModalId').click(function(e){    

    e.preventDefault();

    $('#your-modal').modal('show')

        .find('#modalContent')

        .load($(this).attr('value'));

   return false;

});



hope it helps.

Thank’s wilson_xyz i use often this kind of solution, expecially when i need ajax return. But i think there’s a better solution using modal render method. Something like Modal::render(‘myView’) or Modal->render(‘myView’).

you can use any method of Modal like this:




        $modal = Modal::begin([

            'header' => '<h2>Hello world</h2>',

            'toggleButton' => ['label' => 'click me','url' => ['/index']],

            ]);

        

            $modal-> (any method of Modal such as setView()/getView() render()/renderFile());

        

        Modal::end(); 



Methods of Modal:

http://www.yiiframework.com/doc-2.0/yii-bootstrap-modal.html

Thank’s xPeng is what exactly i need.

Now that i look the code is quite simple solution, but it doesn’t come in my mind ;)

I read the documentation you link but there’s no expressly example.

Thank’s!

Sometimes the Yii isn’t Yii( yes it is , in chinese: 易)。You can always make your own code to “Yiisy” your work. For Example:

  1. make a new Modal:



<?php


namespace app\components;


class Modal extends \yii\bootstrap\Modal{

    

    public static function getContent($file){

        

        echo file_get_contents($file);

        

    }

}

  1. use the new Modal in your sense:



<?php


use app\components\Modal;


        Modal::begin([

            'header' => '<h2>Hello world</h2>',

            'toggleButton' => ['label' => 'click me','url' => ['/index']],

            ]);

            

            Modal::getContent('yourFile');

        

        Modal::end(); 



I would like this one personally:




        Modal::begin([

            'header' => '<h2>Hello world</h2>',

            'toggleButton' => ['label' => 'click me','url' => ['/index']],

            ]);

            

            echo file_get_contents($file);

        

        Modal::end();