Thursday, November 12, 2015

PHP - Render partial view within another controller's view

Render a view called view_B within another view called view_A.  Note that view_B belongs to a different controller than view_A and thus is in a different directory.

Put this within view_A's code, where "site" is the directory of view_B within the "views" directory and "dashmenu" is the name of view_B.

<?php echo $this->render('//site/dashmenu'); ?>

aka, generically:

<?php echo $this->render('//view_B_Directory/view_B'); ?>

Note that the "//" double slash here denotes the path is within the views directory.

There is also a more verbose way to do this:
<!-- a more verbose way -->
<?php echo Yii::$app->view->render('//site/dashmenu'); ?> 

PHP - Intro

Not a huge fan of PHP but unfortunately I was turned on to a framework called Yii2 to use during my first foray into web development.  My first iteration of a site that is an accompaniment to a piece of internet connected hardware has been built with PHP on the Yii2 framework.  The journey has involved a considerable amount of learning and I have decided to park some of those learnings here on Somewhere in the Boundary Layer.  Thus, you will start seeing tidbits of knowledge that I want to log for future use in short posts.

As an aside, I plan to build the second iteration of the site I am working on in node.js.  Thus, you will find some node and strewn about the blog as well.