Include A View Of Another Controller Within Views/site/index

Hi, I can’t figure out how to solve this problem i’m facing. Alright, so basically i have a default view/site/index.php view file which is generating the home page. Now i have built two separated Models and their associated Controllers and views. I want to include those view of the controllers in the default home page view i.e. views/site/index.php. Does it make any sense ?

So my home page is divided into several sections. The header and footer parts are the same throughout the whole site, which is why i’ve written them in the /themes/brushed/layouts folder under separate files. Those are being loaded fine.

The two controller i was talking about are named as FrontPageCategoriesController.php and TopProductsController.php. They are generating their separate view files i.e. index.php in their respective view folders.

How will i include those view files (index.php) into the main site view file (views/site/index.php). I just can’t include those files using include_once() because the data is being generated yet by its controller. So whats the way to achieve this ?

Can anyone please help me out with this. Thanks in advance.

$this->render(’//FrontPageCategories/index’);

Does this help you?

You can always render views in other views via renderPartial(). Your problem here: You don’t have partial views to begin with. From the name of your controllers I take it you wish to display the top products and relecant categories in your layout? You will most likely need to fetch those in your layout itself as there won’t be a backing controller to do this for you.

My bad… $this->renderPartial(…)

Ok, first of all thanks for your advice. I tried your code but it gives me an error


Undefined variable: dataProvider 

Here are the controllers actionIndex() methods.


class FrontPageCategoriesController extends Controller {

public function actionIndex() {

        $dataProvider = new FrontPageCategories;

        $this->render('index', array(

            'dataProvider' => $dataProvider,

        ));

    }

}


class TopProductsController extends Controller {

public function actionIndex() {

        $dataProvider = new TopProducts;

        $this->render('index', array(

            'dataProvider' => $dataProvider,

        ));

    }

}

And here is the view file /frontPageControllers/views/index.php


        <div id="work" class="page">

            <div class="container">

                <!-- Title Page -->

                <div class="row">

                    <div class="span12">

                        <div class="title-page">

                            <h2 class="title">Welcome To Easy Aesthetics</h2>

                            <h3 class="title-description">Check Out New Supplements Prices in  <a href="#">here</a>.</h3>

                        </div>

                    </div>

                </div>

                <!-- End Title Page -->


                <!-- Portfolio Projects -->

                <div class="row">

                    <div class="span3">

                        <!-- Filter -->

                        <nav id="options" class="work-nav">

                            <ul id="filters" class="option-set" data-option-key="filter">

                                <li class="type-work">Categories</li>

                                <li><a href="#filter" data-option-value="*" class="selected">All</a></li>

                                <li><a href="#filter" data-option-value=".design">Training</a></li>

                                <li><a href="#filter" data-option-value=".photography">Nutritions</a></li>

                                <li><a href="#filter" data-option-value=".video">Video</a></li>

                            </ul>

                        </nav>

                        <!-- End Filter -->

                    </div>


                    <div class="span9">

                        <div class="row">

                            <section id="projects">

                                <ul id="thumbs">


                                    <!-- Item Project and Filter Name -->

                                    <li class="item-thumbs span3 design">

                                        <!-- Fancybox - Gallery Enabled - Title - Full Image -->

                                         <?php 

                                         $liValue = $dataProvider->findByPk(1);

                                         $summaryTextParts = explode(";;;", $liValue->summary);

                                         

                                         

                                         ?>

                                        <a class="hover-wrap fancybox" data-fancybox-group="gallery" title="<?php $liValue->summary_title; ?>" href="<?php echo $liValue->image_path; ?>">

                                            <span class="overlay-img"></span>

                                            <span class="overlay-img-thumb font-icon-plus"></span>

                                        </a>

                                        <!-- Thumb Image and Description -->

                                        <img src="<?php echo $summaryTextParts[0]; ?>" alt="<?php echo $summaryTextParts[1]; ?>">

                                    </li>

                                    <!-- End Item Project -->


                                    <!-- Item Project and Filter Name -->

                                    <li class="item-thumbs span3 design">

                                        <!-- Fancybox - Gallery Enabled - Title - Full Image -->

                                        

                                        <?php 

                                         $liValue = $dataProvider->findByPk(2);

                                         $summaryTextParts = explode(";;;", $liValue->summary);

                                         

                                         

                                         ?>

                                        

                                        <a class="hover-wrap fancybox" data-fancybox-group="gallery" title="<?php $liValue->summary_title; ?>" href="<?php echo $liValue->image_path; ?>">

                                            <span class="overlay-img"></span>

                                            <span class="overlay-img-thumb font-icon-plus"></span>

                                        </a>

                                        <!-- Thumb Image and Description -->

                                        <img src="<?php echo $summaryTextParts[0]; ?>" alt="<?php echo $summaryTextParts[1]; ?>">

                                    </li>

                                    <!-- End Item Project -->


                                    <!-- Item Project and Filter Name -->

                                    <li class="item-thumbs span3 photography">

                                        <!-- Fancybox - Gallery Enabled - Title - Full Image -->

                                        <?php 

                                         $liValue = $dataProvider->findByPk(5);

                                         $summaryTextParts = explode(";;;", $liValue->summary);

                                         

                                         

                                         ?>

                                        <a class="hover-wrap fancybox" data-fancybox-group="gallery" title="<?php $liValue->summary_title; ?>" href="<?php echo $liValue->image_path; ?>">

                                            <span class="overlay-img"></span>

                                            <span class="overlay-img-thumb font-icon-plus"></span>

                                        </a>

                                        <!-- Thumb Image and Description -->

                                        <img src="<?php echo $summaryTextParts[0]; ?>" alt="<?php echo $summaryTextParts[1]; ?>">

                                    </li>

                                    <!-- End Item Project -->


                                    <!-- Item Project and Filter Name -->

                                    <li class="item-thumbs span3 video">

                                        <!-- Fancybox Media - Gallery Enabled - Title - Link to Video -->

                                        <?php 

                                         $liValue = $dataProvider->findByPk(<img src='http://www.yiiframework.com/forum/public/style_emoticons/default/cool.gif' class='bbc_emoticon' alt='8)' />;

                                         

                                         

                                         

                                         ?>

                                        <a class="hover-wrap fancybox-media fancybox.iframe" data-fancybox-group="video" title="<?php $liValue->summary_title; ?>" href="<?php echo $liValue->summary; ?>">

                                            <span class="overlay-img"></span>

                                            <span class="overlay-img-thumb font-icon-plus"></span>

                                        </a>

                                        <!-- Thumb Image and Description -->

                                        <img src="<?php echo $liValue->image_path; ?>" alt="Video">

                                    </li>

                                    <!-- End Item Project -->


                                    <!-- Item Project and Filter Name -->

                                    <li class="item-thumbs span3 photography">

                                        <!-- Fancybox - Gallery Enabled - Title - Full Image -->

                                         <?php 

                                         $liValue = $dataProvider->findByPk(6);

                                         $summaryTextParts = explode(";;;", $liValue->summary);

                                         

                                         

                                         ?>

                                        <a class="hover-wrap fancybox" data-fancybox-group="gallery" title="<?php $liValue->summary_title; ?>" href="<?php echo $liValue->image_path; ?>">

                                            <span class="overlay-img"></span>

                                            <span class="overlay-img-thumb font-icon-plus"></span>

                                        </a>

                                        <!-- Thumb Image and Description -->

                                        <img src="<?php echo $summaryTextParts[0]; ?>" alt=" <?php echo $summaryTextParts[1]; ?> ">

                                    </li>

                                    <!-- End Item Project -->


                                    <!-- Item Project and Filter Name -->

                                    <li class="item-thumbs span3 photography">

                                        <!-- Fancybox - Gallery Enabled - Title - Full Image -->

                                        <?php 

                                         $liValue = $dataProvider->findByPk(7);

                                         $summaryTextParts = explode(";;;", $liValue->summary);

                                         

                                         

                                         ?>

                                        <a class="hover-wrap fancybox" data-fancybox-group="gallery" title="<?php $liValue->summary_title; ?>" href="<?php echo $liValue->image_path; ?>">

                                            <span class="overlay-img"></span>

                                            <span class="overlay-img-thumb font-icon-plus"></span>

                                        </a>

                                        <!-- Thumb Image and Description -->

                                        <img src="<?php echo $summaryTextParts[0]; ?>" alt="<?php echo $summaryTextParts[1]; ?> ">

                                    </li>

                                    <!-- End Item Project -->


                                    <!-- Item Project and Filter Name -->

                                    <li class="item-thumbs span3 video">

                                        <?php 

                                         $liValue = $dataProvider->findByPk(9);

                                         

                                         

                                         

                                         ?>

                                        <!-- Fancybox Media - Gallery Enabled - Title - Link to Video https://www.youtube.com/watch?v=yuTyZMxrPMQ -->

                                        <a class="hover-wrap fancybox-media fancybox.iframe" data-fancybox-group="video" title="<?php $liValue->summary_title; ?>" href="<?php echo $liValue->summary; ?>">

                                            <span class="overlay-img"></span>

                                            <span class="overlay-img-thumb font-icon-plus"></span>

                                        </a>

                                        <!-- Thumb Image and Description -->

                                        <img src="<?php echo $liValue->image_path; ?>" alt="Video">

                                    </li>

                                    <!-- End Item Project -->


                                    <!-- Item Project and Filter Name -->

                                    <li class="item-thumbs span3 design">

                                        <!-- Fancybox - Gallery Enabled - Title - Full Image -->

                                        <?php 

                                         $liValue = $dataProvider->findByPk(3);

                                         $summaryTextParts = explode(";;;", $liValue->summary);

                                         

                                         

                                         ?>

                                        <a class="hover-wrap fancybox" data-fancybox-group="gallery" title="<?php $liValue->summary_title; ?>" href="<?php echo $liValue->image_path; ?> ">

                                            <span class="overlay-img"></span>

                                            <span class="overlay-img-thumb font-icon-plus"></span>

                                        </a>

                                        <!-- Thumb Image and Description -->

                                        <img src="<?php echo $summaryTextParts[0]; ?>" alt="<?php echo $summaryTextParts[1]; ?>">

                                    </li>

                                    <!-- End Item Project -->


                                    <!-- Item Project and Filter Name -->

                                    <li class="item-thumbs span3 design">

                                        <!-- Fancybox - Gallery Enabled - Title - Full Image -->

                                         <?php 

                                         $liValue = $dataProvider->findByPk(4);

                                         $summaryTextParts = explode(";;;", $liValue->summary);

                                         

                                         

                                         ?>

                                        <a class="hover-wrap fancybox" data-fancybox-group="gallery" title="<?php $liValue->summary_title; ?>" href="<?php echo $liValue->image_path; ?>">

                                            <span class="overlay-img"></span>

                                            <span class="overlay-img-thumb font-icon-plus"></span>

                                        </a>

                                        <!-- Thumb Image and Description -->

                                        <img src="<?php echo $summaryTextParts[0]; ?>" alt="<?php echo $summaryTextParts[1]; ?>">

                                    </li>

                                    <!-- End Item Project -->

                                </ul>

                            </section>


                        </div>

                    </div>

                </div>

                <!-- End Portfolio Projects -->

            </div>

        </div>

Here is the TopProducts view file located in topProducts/views/index.php


<div id="about" class="page-alternate">

            <div class="container">

                <!-- Title Page -->

                <div class="row">

                    <div class="span12">

                        <div class="title-page">

                            <h2 class="title">Top Products</h2>

                            <h3 class="title-description">Check Out The Cheap Prices On These Products</h3>

                        </div>

                    </div>

                </div>

                <!-- End Title Page -->


                <!-- People -->

                <div class="row">

                    

                    <!-- Start Profile -->

                    <div class="span4 profile">

                        <?php 

                             $liValue = $dataProvider->findByPk(1);

                        ?>

                        <div class="image-wrap">

                            <div class="hover-wrap">

                                <span class="overlay-img"></span>

                                <span class="overlay-text-thumb">#1 Products</span>

                            </div>

                            <img src="<?php echo $liValue->image_path; ?>" alt="#1 Product">

                        </div>

                        <h3 class="profile-name"><?php echo $liValue->title; ?></h3>

                        <p class="profile-description"> <?php echo $liValue->summary; ?></p>


                        <div class="social">

                            <ul class="social-icons">

                                <li><a href="#"><i class="font-icon-social-twitter"></i></a></li>

                                <li><a href="#"><i class="font-icon-social-dribbble"></i></a></li>

                                <li><a href="#"><i class="font-icon-social-facebook"></i></a></li>

                            </ul>

                        </div>

                    </div>

                    <!-- End Profile -->


                    <!-- Start Profile -->

                    <div class="span4 profile">

                        <?php 

                             $liValue = $dataProvider->findByPk(2);

                        ?>

                        <div class="image-wrap">

                            <div class="hover-wrap">

                                <span class="overlay-img"></span>

                                <span class="overlay-text-thumb">#2 Product</span>

                            </div>

                            <img src="<?php echo $liValue->image_path; ?>" alt="#2 Product">

                        </div>

                        <h3 class="profile-name"><?php echo $liValue->title; ?></h3>

                        <p class="profile-description"><?php echo $liValue->summary; ?></p>


                        <div class="social">

                            <ul class="social-icons">

                                <li><a href="#"><i class="font-icon-social-twitter"></i></a></li>

                                <li><a href="#"><i class="font-icon-social-email"></i></a></li>

                            </ul>

                        </div>

                    </div>

                    <!-- End Profile -->


                    <!-- Start Profile -->

                    <div class="span4 profile">

                        <?php 

                             $liValue = $dataProvider->findByPk(3);

                        ?>

                        <div class="image-wrap">

                            <div class="hover-wrap">

                                <span class="overlay-img"></span>

                                <span class="overlay-text-thumb">#3 Product</span>

                            </div>

                            <img src="<?php echo $liValue->image_path; ?>" alt="#3 Product">

                        </div>

                        <h3 class="profile-name"><?php echo $liValue->title; ?></h3>

                        <p class="profile-description"><?php echo $liValue->summary; ?></p>


                        <div class="social">

                            <ul class="social-icons">

                                <li><a href="#"><i class="font-icon-social-twitter"></i></a></li>

                                <li><a href="#"><i class="font-icon-social-linkedin"></i></a></li>

                                <li><a href="#"><i class="font-icon-social-google-plus"></i></a></li>

                                <li><a href="#"><i class="font-icon-social-vimeo"></i></a></li>

                            </ul>

                        </div>

                    </div>

                    <!-- End Profile -->


                </div>

                <!-- End People -->

            </div>

        </div>

        <!-- End About Section -->

And finally here is the site/views/index.php


<?php

/* @var $this SiteController */


$this->pageTitle=Yii::app()->name;


$this->render('//frontPageCategories/index');

$this->render('//topProducts/index');


?>




So, you see i want to include those view files in the above site/views/index.php. How would i do that ? Is there even a way to do that without resorting to PHP’s require_once() method.

Thanks,

Maxx

Hi please see forum

i hope it’s some help.

Yes thats my issue.

The $dataProvider vairable isn’t available to the views if i call renderPartial() or render() from a different controller other than the ones with which those views belong.

In my case, as you can see i’m trying to call the views of FrontPageCategoriesController and TopProductsController in the default /site/views/index.php

Alright, thanks for that link. It gave me some insights, but you see my problem is that i want to include the two view files which are being rendered by different controllers in the site/view/index.php. Is it even possible to do that ? Or am i on a wrong path ?

Again, really appreciate your time for providing a helping hand.

I am a bit puzzled by your views. As I see it, they won’t actually pull the n top categories / products but rather the first n that got inserted into the db?

In any case, this should work for you in [font="Courier New"]site/views/index.php[/font]:




<?php

/* @var $this SiteController */


$this->pageTitle=Yii::app()->name;


$this->render('//frontPageCategories/index', array('dataProvider' => new FrontPageCategories));

$this->render('//topProducts/index', array('dataProvider' => new TopProducts));


?>



Wonderful that helped right away. Although


$this->render()

keeps on loading the page, maybe its stuck on a endless loop somehow. But changing it to


$this->renderPartial

worked flawlessly.

You see the views are for two small image gallery that just shows the recent articles, products. And its limited to only show 9 items thats why i refered the AR instances using the Primary Key, on each li items.

Anyways, i can’t thank you enough for this. I’m really grateful to you.

Thanks,

Maxx

Ah, yes. I’ve been a little to quick to post that solution. Sorry.

Hm, but that will never be dynamic? And what will happen if you delete on of those items in the db?

Glad I could help ;)

4726

FrontPageCategories.jpg

Yes i know. The admin is supposed to edit the content of that (image_path, summary, title, etc.). And if one of those item gets deleted somehow then that correponding image content will not show up :slight_smile:

Hi,

Even I am trying to trigger two views (from two controllers) into the default views/site/index page like in the above example.

I tried the above recommendation by Da:Sourcerer to render the two views explicitly in the views/site/index.php, but I get an error "Catalog and its behaviors do not have a method or closure named "getData". (Catalog is the first controller I want to include into my default view). I am able to individually render these views without any issues (so DB connection is fine & the records are being pulled out perfectly). Attaching the stack trace along with.

Can you pl. help? (since I am a newbie).

PS: I am on yii v1.1.15

I think there is a problem at:

$this->render(’//catalog/index’, array(‘dataProvider’ => new Catalog));

Is ‘Catalog’ model a DataProvider? I think it’s just a model. You have to pass DataProvider into the view

Even my code is pretty much the same as the above example. My two classes, Catalog & Users are extending from the Controller class like shown above (FrontPageCategoriesController & TopProductsController are derived from Controller). I even tried to create the dataprovider instance with CActiveDataProvider class as you see with the commented lines in the stacktrace code snippet, but I was getting a missing filename error within CBaseListView->renderSection() code which I am not able to follow much unfortunately :(

Checked the code further, created a CActiveDataProvider instance of the Catalog class now. I still face an error, "filename not found" for the $viewfile within CBaseController.php(framework code). I see that the the filename is indeed being formed properly in the stack trace(See #9). I suspect there is some problem in the parser code in the framework (see #5) which is resulting in an unknown call in #4, which in turn is causing this error.

Can someone check this for me please? Attached is the updated stack trace for reference.

Thanks

look at


CBaseController->renderFile(false, array("index" => 0, "data" => Catalog, "widget" => CListView))

first parameter: false

check if you really have view "_view" in your file system


CBaseController->widget("zii.widgets.CListView", array("dataProvider" => CActiveDataProvider, "itemView" => "_view"))

Not sure of how to debug it :( will you be able to help debug it with a screen sharing session using team viewer?