Yii Framework Forum: render shared partial without using widget or porlet - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

render shared partial without using widget or porlet Rate Topic: -----

#1 User is offline   shallway 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 12-October 10

Posted 12 October 2010 - 09:48 AM

Consider the following senario :

You have a controller inside a module - defaultController, in which there is this action - actionIndex, this action renders the view which resides in views/default/index.php, thus the folder structure looks like :

modules
  - module1
     - controllers
       - defaultController.php
     - views
       - default
         - index.php



now in the index.php view, I want to include a partial from another module - module2, let's call the view "anotherView.php", how do I include it?

I've tried to put the following code in index.php

<?php $this->renderPartial('anotherView', array('params'=>...)) ?>


But that seemed not working since Yii seems to be looking for anotherView in module1/views/default/anotherView.php

One solution seems to be using the widget, that is, to be specific, to move module2/views/.../anotherView.php into application/components/views/anotherView.php and then declare a widget in application/components/AnotherView.php and then use it in index.php as :

<?php $this->widget('AnotherView', array('params'=>...)); ?>


My question is how do I directly fetch any arbitrary view from any views folder and render it in my current view without using a widget or portlet?
0

#2 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 12 October 2010 - 10:40 AM

I don't want to sound too discouraging, but in my opinion, this problem evolves from bad design. It should never be necessary to render a view from another module as a module should be self contained. This is IMHO, of course.

If you can solve it with a widget, that's much cleaner.
0

#3 User is offline   shallway 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 8
  • Joined: 12-October 10

Posted 12 October 2010 - 07:28 PM

@Mike: Thanks for pointing it out. Actually I had the similar feeling. I came from the symfony world, there the views can render any other partials from arbitrary locations. In symfony "components" are defined as self-contain parts which maps to widgets in Yii, still I think it provides more flexibility if Yii can support arbitrary view rendering, especially during the prototyping phase of a project, you may not want to convert everything into widgets.
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,016
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 October 2010 - 02:09 AM

Just for the record: I didn't say, it's not possible, i just never thought about wether and how to do it. So if you still need it, do some research on the forum here, i think this question already came up several times.
0

#5 User is offline   zaccaria 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 2,232
  • Joined: 04-October 09
  • Location:Moscow

Posted 13 October 2010 - 03:04 AM

I am of the same opinion of Max, that render view from another module (and even from another controller) is a bad idea.

Aniway you can do like that:

$this->renderPartial('module2.views.AnotherController.AnotherView');



Mike said:

Just for the record: I didn't say, it's not possible, i just never thought about wether and how to do it. So if you still need it, do some research on the forum here, i think this question already came up several times.


That's true.
0

#6 User is offline   davehere 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 6
  • Joined: 19-September 10

Posted 14 February 2011 - 06:27 AM

View Postzaccaria, on 13 October 2010 - 03:04 AM, said:

That's true.


:)
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

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