简体   繁体   中英

Yii2 - How to include html on multiple pages

Ordinarily if I wanted to include some common code on multiple pages I would use something like:

<?php include_once(__DIR__.'/../someFolder/SomeFile.php' ?>

Just wondering what the preferred way to do this is in Yii2? I have looked at Widgets and Partial views but haven't really been able to conclude this is the best way to factorise common code between pages. I can provide more info, just not sure what too.

Came across this: http://www.yiiframework.com/doc-2.0/guide-structure-views.html#rendering-in-widgets is it likely to be where I should be looking?

Seems I have answered my own question.

<?= \Yii::$app->view->renderFile('@app/views/site/fileToInclude.php'); ?>

If someone can add to this, that would be fantastic, but this seems to answer my question.

Actually you can do the following: view->renderpartial or just view->render

For example you can use this: <?= \\Yii::$app->view->render('fileToInclude', ['id' => $somevariables]); ?> <?= \\Yii::$app->view->render('fileToInclude', ['id' => $somevariables]); ?> if it is in the same folder for example

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM