Welcome Guest, Not a member yet? Register   Sign In
Do we really need a view library?
#1

[eluser]Rick Jolly[/eluser]
The way a developer loads a view and its data is a matter of personal preference and choice is good. I just wanted to make a few of points in favor of CI’s current implementation and maybe provoke a bit of thought and discussion.

1) Coupling a View to its Data

Coupling a view to its data when loading is good style. As we know, with CI you can load a view and its data all at once:
Code:
$this->load->view(‘view’, $data);
$view_part = $this->load->view(‘view_part’, $data, true);
When setting view variables using $this->load->vars() or other syntax, there is no traceable link between the view and its variables. View variables could be loaded throughout the code, making it difficult to read and maintain.

2) Locally Scoped Variables for Partial Views

Another advantage to the $this->load->view(‘view_part’, $data, true); method: it allows locally scoped variables. When setting view variables in a global way (like when using $this->load->vars(); and other custom methods) every partial view must share the same variables.

3) A View is a View

CI treats a view as a view. You load views within views (partials/fragments/blocks) in the same generic way as a regular view. Some developers prefer to differentiate between views and nested views by using different loading syntax. I find it confusing to make those differentiations, and there is additional syntax to remember.
#2

[eluser]Imkow@CN[/eluser]
Yes, I personally think I need a enhanced version of View that has built-in support for Layouts.
A layout is a framwork of a page that contains a number of Views or components of text.
#3

[eluser]coolfactor[/eluser]
Yes, I think a View library is needed. The Loader is trying to do too much and its compromising on simplicity. For me, that's the bottom line.
#4

[eluser]Rick Jolly[/eluser]
[quote author="Imkow@CN" date="1185580008"]Yes, I personally think I need a enhanced version of View that has built-in support for Layouts.
A layout is a framwork of a page that contains a number of Views or components of text.[/quote]

What kind of support do you envision? Something other than how we'd currently do a layout in CI:
Code:
<html>
<body>
<?php $this->load->view('header'); ?>
// or <?=$header;?>
<?=$somevar;?>
...
</body>
</html>
#5

[eluser]Imkow@CN[/eluser]
yes, fundamentally same with the current practice. It could be sweetier if there is a wrapper with a througtful design over it . e.g. some function handling the layout directory automatically(and/or separately from those of View's).
#6

[eluser]sophistry[/eluser]
@Rick,

Interesting point. CI usage does tend to foster questions that begin with "do we really need this new feature?" Or maybe it's the people it attracts are interested in keeping trim code.

I think the example above (as you say "how we'd currently do a layout in CI") amply demonstrates the de-coupling (untraceability) of variables from views.

That is, loading the header view part *inside* of the main view is convenient but too simplistic and leads to the very problem you mention in the first post. The view parts can change and require their own variables. If your design involves setting views "in the view" then you can't even see where the controller-assigned variables are going to end up.

A new approach to the view library can help with overall architecture of organizing layouts as well as making 'traceability' even better. I believe the main value of having a modular view library is encapsulation - which also addresses the point of 'traceability' of variables.
#7

[eluser]Rick Jolly[/eluser]
[quote author="sophistry" date="1185581326"]
...That is, loading the header view part *inside* of the main view is convenient but too simplistic and leads to the very problem you mention in the first post. The view parts can change and require their own variables. If your design involves setting views "in the view" then you can't even see where the controller-assigned variables are going to end up.
[/quote]
Agreed. I like to use partial view placeholders in the layout view for anything except possibly static partial views. Then, of course, in the controller use the $placeholder = $this->load->view('partial_view',$data,true); syntax.

The advantages again: same generic load view method, view variables are traceable, and the variables can be specific to the partial view if desired. Other methods like $this->load->vars(); forces the layout view and all partials to share the same variables.
#8

[eluser]tamara6[/eluser]
I like the way Django does it's templates/views:

Define a basic template of a page, with sections that can be changed by your view and defaults for those sections in case your view doesn't change them. This one template page has a layout defined once - it has the graphic at the top of the page, the footer at the bottom, the standard css files imported in the header. And a section labeled "main content" that is replaced by the content that corresponds to whatever controller you've just run.

The advantage to this is that you can make a change in just one spot and all of your web pages reflect it. And it all happens automatically. Your views are always just "partial views", stuck into your template at the right spot. And it is very easy to understand what is going on.

This was probably the only thing that I liked about using Django (OK, I like python, too), and I just don't understand why more frameworks don't try to do it this way....

Tamara
#9

[eluser]jbowman[/eluser]
I think every project using CI is going to require some sort of view library to handle the layout and return variables. However, I have yet to see the view library that should be shipped with CI, and I don't think I ever will. Why? Because at that point CI will cease to be as adaptable as it is, and would be something more like Drupal. Same reason there's so many user auth systems floating around.
#10

[eluser]座頭市[/eluser]
Absolutely not. Views work just fine out of the box.

The bottom line for me is that none of the view libraries I've seen fit my work flow or design patterns at the moment, and, in fact, over complicate things in many cases. If the project in question needs a view library, I'll roll my own, otherwise the standard, and very adaptable, CI take on views is simple, elegant and gets the job done.




Theme © iAndrew 2016 - Forum software by © MyBB