Yii Framework Forum: Multiple Views, but using the same Models and Controllers - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Multiple Views, but using the same Models and Controllers Rate Topic: -----

#1 User is offline   mickeyckm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 25-March 10

Posted 25 March 2010 - 10:42 AM

Hi everyone,

I'm still investigating on different frameworks for my new projects. As my new projects require a different frontend, I was wondering whether Yii offers or any 3rd parties product that offers such capability.

An sample scenario: Display a storefront for user.

I will be using a Store controller to access the method/route display(). This display will grab the data from different models and display it out in a view. Depending on a param, $_GET['v']=iphone or $_GET['v']=web, it will grab a different view and throw it out.

Is it possible to do it implicitly without specifying if else in the controller? I'm sorry if this is a noob question as I'm still reading through the documentation and review. I'm very incline towards Yii but I will lean towards a framework that enable easy customisation of such features

Many thanks,
Mickey
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 25 March 2010 - 11:17 AM

Hi, welcome to the forum.

If I got you right you want different views/layouts based on a $_GET variable.

First of all you can change the layout inside of a controller by changing the $layout variable. The layout is the skeleton for the actual content.

Inside of a controller you will usually call the render() method in order to display the actual content-view (which gets automatically surrounded by the layout).

Now in order to automatically set views based on a get variable, you can create a parent-controller that will do the work for you by extending the render() method.

Simple example:

class ParentController extends CController
{

   public function render($view,$data=null,$return=false)
   {

      if (isset($_GET['v']))
      {
         $this->layout .= "_{$_GET['v']}";
         $view .= "_{$_GET['v']}";
      }

      return parent::render($view,$data=null,$return=false);

   }

}


Now let's assume you set your default layout to "index" and you render() the view called "items". Also $_GET['v'] is set to "iphone". Now what happens is the parent-controller will change "index" to "index_iphone" and "items" to "items_iphone". So based on the $_GET variable you have different views.

There are probably better ways to do this, but it should show you at least how easy it is at the end.
0

#3 User is offline   mickeyckm 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 10
  • Joined: 25-March 10

Posted 25 March 2010 - 07:05 PM

Cool. Thanks. Looks pretty simple.

I'm going to give it a try this weekend.

I was wondering whether we can put it in a folder structure.

Assuming this way:

/views/<controller_name>/<$_GET['v']>/<view_files>

i.e.

/views/store/web/display.php
/views/store/iphone/display.php

Do I just change the "if" statement to,

if (isset($_GET['v']))
{
    $this->layout = "/{$_GET['v']}/" . $this->layout;
    $view = "/{$_GET['v']}/" . $view;
}


Hope the view can be read from folders.

Cheers,
Mickey
0

#4 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 25 March 2010 - 07:12 PM

Yes that should work. Well just try it out and report back then. :)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users