Yii - Ways to do include another view in this view

11,195

If I understand your question right, you can do a few things...

(I am assuming that you have a partial view file like views/model/_list.php.)

You can either renderPartial('/model/_list') on the initial page load, and then in your AJAX action just call renderPartial('/model/_list') again.

Or you can just have the AJAX action (containing renderPartial('/model/_list')), and call it on page load with jQuery instead of rendering it in the view (just call Yii::app()->getClientScript()->registerScript in your view where you want to load it, and use $.ajax() or something to make the call).

A third thing that I have done is make a function in the controller (not an action, just a regular public method) that returns the output of renderPartial (return $this->renderPartial('/model/_list',array(), true) I think). Then in view on the initial load I echo that function, and in the ajax action I echo it before calling Yii::app()->end();.

Widgets are useful when you will be rendering a bit of partial code all over the site, even on pages from different controllers. So if that list is in a sidebar all over the site it might be better to make a widget out of it, otherwise I would just use the controller of the model you are working with. What you would do with the widget is the same as you would with the regular view - either build the data in the Widget and render the partialView, or call it via AJAX. What you would probably do for the AJAX is POST to the controller you are getting the data from, so you will still need the AJAX action. The widget will just make it easier to drop it different places around the site.

Good luck!

Share:
11,195

Related videos on Youtube

Pablo
Author by

Pablo

Updated on June 04, 2022

Comments

  • Pablo
    Pablo almost 2 years

    My layout page:

    <html>
     <body>
      <div id="container">
       <ul id="list"></ul>
      </div>
      <input id="update" value="update" />
     </body>
    </html>
    

    in which the ul list is a partial which will be rendered either when

    1. the whole page gets loaded
    2. the when the update button gets clicked, an ajax reqeust is issued and the innerHTML of the container layer will be updated with the response ul list

    I'm new to Yii and am not sure how to reuse the ul partial both in both cases, I googled a bit and widgets seems to be the solution, not sure tho. Any ideas?

    Thanks.

    • thaddeusmt
      thaddeusmt over 13 years
      Did my answer help? Or do you need to clarify your question for a better answer? If your question is solved please select an answer, or provide your own. Thanks!
  • mwotton
    mwotton over 13 years
    The first option here is probably cleanest and most appropriate if I understand the original question.

Related