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
Page 1 of 1
Multiple Views, but using the same Models and Controllers
#2
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:
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.
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.
#3
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,
Hope the view can be read from folders.
Cheers,
Mickey
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
Share this topic:
Page 1 of 1

Help












