Note that there are some explanatory texts on larger screens.

plurals
  1. POYii Framework: Menu content that appears on every page
    primarykey
    data
    text
    <p>I'm using the Yii framework to develop a site that uses a 2-column layout. One column is the actual content, while the other is a menu with site links, some information about the logged in user and a list of the latest posts. The menu is present on all the pages, but it has no information that is related to the current page (or route).</p> <p>Every time I render the menu I need to retrieve the list of latest posts and user-related data from the database. I'm not sure how it would be best to render this menu so that I don't repeat the code that fetches the data in every action on the site.</p> <p>I have thought of a few approaches and I would like to know which one of them (if any) is the right way to handle this situation.</p> <ul> <li><p>Fetch the data in the <code>Controller::beforeRender</code> method, then render the menu in a partial view which displays the data in a <code>CClipWidget</code> block. Then, in the layout view, display the block where the menu is supposed to be. This method works, but I feel that it's quite clunky because of the <code>beforeRender</code>. If I ever add a page that doesn't have the menu on it, I need to include a check for that. Also, after reading the Yii documentation, I don't understand if <code>beforeRender()</code> is called for <code>renderPartial()</code> as well, or just for <code>render()</code>.</p></li> <li><p>Keep a partial view of the menu and render it from the layout view. The data is fetched in the menu view from a static method placed somewhere else (possibly in a model). This involves writing very little code, but I'm not sure if it's good practice for the MVC paradigm. Fetching the data in the view makes me cringe a little, even though it's just calling a static function.</p></li> <li><p>Turn the menu into a widget. The data is fetched in the <code>run()</code> method and rendered from a widget view. However, using a widget imposes a few additional restrictions. If I want to use the view that renders the latest posts in a controller, I would run into issues. Widgets cannot use <code>renderPartial()</code> and are forced to use <code>render()</code> all the time. I could work around this if I figure out how to check what is rendering the view (widget or controller) and call <code>render()</code> or <code>renderPartial()</code> appropriately. Also, the widget views have to be separate from the site views, but I could work around that by specifying the full view path, like <code>application.views.controller.view</code>, as clunky as it may be. Furthermore, I'm still not sure if widgets should fetch database data by themselves.</p></li> </ul> <p>All of these methods <em>work</em>, but they all come with a few catches. I'm sure many sites are in the same situation and I'd like to see what the best option is.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload