Welcome Guest, Not a member yet? Register   Sign In
How to handle embedded dynamic page elements?
#1

[eluser]okoman[/eluser]
Hi!

I know there are several threads kind of "how to use a view/controller from within a view/controller" and so on. Also I know there is Wick, that gives one the ability to use controllers in other controllers.
But unfortunately most threads end up in vague recommondations and theoretical implementations.

Let's take an example: You have a simple Blog, for instance think of a fresh Wordpress installation. You have the main page, that is directly connected with the URL (it invokes the controller method). Then there is the sidebar. There one can add an undefined number of extra elements/plugins to the blog.
The sidebar with all of its components needs to be displayed anytime a visitor requests any single page on the blog.
So my question is: How would you (the people who are really using CI and have already sueccesfully implemented such a page structure) solve that problem?

As far as I see it, there are two ways: Either creating an own library or using Wick. The first aproach sounds well since it uses CI's native functionality. The second I would prefer since you can use a controller and a separate view for the sidebar.
Nevertheless with both solutions there is code redundancy since either Wick or the library needs to be accessed from any controller.
Also - when using a library - you can't use $this->load->view( '...', $certainData ), but have to use $this->data or something similar to pass the data that shall be provided by the current controller and the additional data to the view.
The Wick output also either must be passed to a "global" or "higher-level" view together with current data or the call has to be placed directly into another view.
All this sounds bad when you think of MVC as a seperation of concerns. (I don't neccessarily want to separate M/V/C but main page and sidebar.)

So what do you think is (at least for me) the better way? I don't want a definitive solution but I'm looking for expieriences user of CI already made. Maybe you can tell which unexpected or bad problems occured while you used one of the aproaches mentioned above?
#2

[eluser]okoman[/eluser]
Well... aren't there any developers who implemented something similar? I think I have described a quite common page structure. Just want to read something like "I did this and that and it worked." and not "You could do this, but you could also do this."
Just a small hint.. please Wink
#3

[eluser]Pascal Kriete[/eluser]
Quote:Also - when using a library - you can’t use $this->load->view( ‘...’, $certainData )

Not sure what you're tying here, but it will work as long as you operate on the CI super-object:
Code:
$CI =& get_instance();
$CI->load->view('view', $data);

I usually write my own library that acts as a mediator. Then I have a master template:
Code:
<?php $this->load->view('global/header') ?>

<div id="wrapper">

<div id="left_column">
    &lt;?php $this->load->view($content) ?&gt;
</div>

<div id="right_column">
    &lt;?php foreach($widgets as $widget): ?&gt;
        &lt;?= $widget ?&gt;
    &lt;?php endforeach; ?&gt;
</div>

</div>

&lt;?php $this->load->view('global/footer') ?&gt;

And library functions like add_widget() that assemble the widgets into an array. Add a render method that passes the widgets to the template along with the name of the content view, and you're pretty much done:
Code:
function render($view, $data = array())
{
    $data = array_merge(array(
                'widgets'        => $this->widgets,
                'title'            => $this->title,
                'content'        => $view
    ), $this->defaults, $data);
    
    $this->CI->load->view($this->template, $data);
}

Hope that helps a little bit. There is obviously no one-size-fits-all solution.
#4

[eluser]okoman[/eluser]
Thanks, but you have to build that $data array in each controller of your application. If you want to change something later you have to change it in all controllers. That's what I personally don't like concerning this aproach. (Although my opinion might be a bit paranoid since there'll never be so much controllers that it would lead to errors... Wink )

I think I will extend the main controller class and initialize an array as a member variable. This array gets some sidebar and header data. The specific controller then adds some more information and passes the array to a global view.

Nevertheless just wondering whether there is an "officially recommended" way for this. Perhaps at least a description of the possible approaches would be a handy extension of CI's documentation.
#5

[eluser]cozzmyn[/eluser]
take a look at last post in this thread, http://ellislab.com/forums/viewthread/88335/

There I'm saying how to handle the inclusion of partial views like header, footer... Also if some of the partial content is dynamic like a menu generated from db, you can create modules with HMVC (http://codeigniter.com/wiki/Modular_Extensions_-_HMVC/) and include those in your partial views.
#6

[eluser]Pascal Kriete[/eluser]
Quote:but you have to build that $data array in each controller of your application

See that $this->defaults variable - guess what that contains Wink .

And it's in a library, not a controller. The controller merely calls the layout library:
Code:
$this->layout->render('content_view', $specific_data);

Any data that is passed to the layout library overrides those defaults (hence the use of array_merge).

Regardless, there is no 'officially recommended' route.
#7

[eluser]Colin Williams[/eluser]
Extend the base Controller class when you need "global things." There's your recommendation. Call it official if you want to feel better about it.
#8

[eluser]okoman[/eluser]
[quote author="cozzmyn" date="1230358080"]take a look at last post in this thread, http://ellislab.com/forums/viewthread/88335/

There I'm saying how to handle the inclusion of partial views like header, footer... Also if some of the partial content is dynamic like a menu generated from db, you can create modules with HMVC (http://codeigniter.com/wiki/Modular_Extensions_-_HMVC/) and include those in your partial views.[/quote]

Haven't read about it until now, but seems to be exactly what I was looking for. Smile

Thanks for all your answers!




Theme © iAndrew 2016 - Forum software by © MyBB