Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8375301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:08:25+00:00 2026-06-09T15:08:25+00:00

I’m using the Yii framework to develop a site that uses a 2-column layout.

  • 0

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).

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.

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.

  • Fetch the data in the Controller::beforeRender method, then render the menu in a partial view which displays the data in a CClipWidget 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 beforeRender. 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 beforeRender() is called for renderPartial() as well, or just for render().

  • 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.

  • Turn the menu into a widget. The data is fetched in the run() 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 renderPartial() and are forced to use render() 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 render() or renderPartial() 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 application.views.controller.view, as clunky as it may be. Furthermore, I’m still not sure if widgets should fetch database data by themselves.

All of these methods work, 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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T15:08:26+00:00Added an answer on June 9, 2026 at 3:08 pm

    You should be able to avoid most of the performance hit for re-requesting this data by simply caching the database results (either in memory or in files it’s up to you and your set up). Here’s a small, simple example:

    $posts = Post::model()->cache(600)->with('comments')->findAll(array('blah=blahdyblah'));
    

    This will cache the data returned for 10 minutes.

    Here’s a link to the Yii caching guide again only for completeness:
    Caching Guide

    To actual get this data on every page, you would better off placing the code in your own widget, and having it called somewhere in the layout file.

    The widget restrictions you list can be dealt with:

    • Firstly when the widget renders it will only render(), but without a layout. This is fine for most cases, but if you do want a page with just that on, say you want a real render(), then do exactly that, just create a view that only calls that widget.

    • Secondly, There’s no reason widgets have to be entirely abstracted from your application and not use its data + models. Yes it is a very nice feature that they can be ‘pluggable’, but it doesn’t mean they have to be, you’re just using them to separate code.

    • Lastly, if you do want to use ‘some’ of the data from the widget but not all, or change it slightly, the methods and views you use within the widget should be built with just enough abstraction that you can use the pieces you need separately. A good way to do this is to make good use of scopes on ActiveRecords, mini view files, and using components for larger chunks of non-data logic.

    Of course if you’re running really on the limit of performance and need to trim thousandths of a second off your request time then you should be looking into view caching and using fragment caching of views:

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
I know there's a lot of other questions out there that deal with this
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.