Roby Sottini
Roby Sottini

Reputation: 2265

Yii2: How to renderPartial with Bootstrap classes?

I am trying to use renderPartial function and Bootstrap classes.

This is my function in the controller file:

public function actionPrintReport() {
    Yii::$app->response->format = 'pdf';
    return $this->renderPartial('_myReport', []);
}

This is my _myReport file that is rendered: My own styles works but not the Bootstrap classes.

<div class="box box-warning">
    <div class="box-body">
        Text
    </div>
</div>

But the result doesn't show any Bootstrap class:

Rendered result

I also tried renderAjax but the same result.

Upvotes: 0

Views: 568

Answers (3)

Vic Espino
Vic Espino

Reputation: 41

I was trying to do the same as you, I got the solution.

You just needed to check the struture of main file:

  1. In you "_..." file, add HTML
<?php $this->beginPage() ?>
<!DOCTYPE html>
<html lang="<?= Yii::$app->language ?>" class="h-100">
<head>
    <meta charset="<?= Yii::$app->charset ?>">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <?php $this->registerCsrfMetaTags() ?>
    <title><?= Html::encode($this->title) ?></title>
    <?php $this->head() ?>
</head>

<body class="d-flex flex-column h-100">
<?php $this->beginBody() ?>

<div class="container-xl outlined-box p-2 m-1 ">
    <div class="row  ">
    test
    </div>
    <a href="#" class="btn btn-primary ">asdasd</a>
</div>

<h1>asdasdasd</h1>

<?php $this->endBody() ?>
</body>
</html>

<?php $this->endPage();


  1. Don't forget to put this in the top of your PHP file (after imports btw)
    frontend\assets\AppAsset::register($this);

Upvotes: 0

Denis Staci
Denis Staci

Reputation: 61

You need to:

Asset::register($this);

in your view _myReports.

Upvotes: 0

J&#248;rgen
J&#248;rgen

Reputation: 3567

Well, that is much the point with the function renderPartial()

Renders a view without applying layout.

This method differs from render() in that it does not apply any layout.

The renderPartial function is often used when you'll render the same block of code several times in one page. What was your expected result here? What are you trying to do?

registerAssets is usually loaded in the main view file (the one that is not not rendered). You should have a look at the main layout file and understand how assets are loaded.

Upvotes: 0

Related Questions