CodeIgniter Forums
View system for CodeIgniter {beta} - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Libraries & Helpers (https://forum.codeigniter.com/forum-22.html)
+--- Thread: View system for CodeIgniter {beta} (/thread-53973.html)



View system for CodeIgniter {beta} - El Forum - 08-16-2012

[eluser]almightyolive[/eluser]
Disclaimer: I have only been playing with CodeIgniter for a few weeks now, so willing to take a few pointers or be directed to already existed projects that do the same thing.

Basically, I brought in a guy to help me out with some web design but he was pretty sketchy with PHP code. Not only that, but I have enough work on my hands than to continually write complex data array structures to pass and loading of views (and so on and so forth...). Also, I wanted something with in-built language support (since some of my projects require this feature).

There are plenty of existing projects that looked awesome but weren't quite what I was looking for (such as Ocular-Template-Library and Modular Extensions - HMVC). But none quite offered everything I was looking for.... so I decided to code my own.

The result is here:

https://github.com/almightynassar/AlmightyOlive-CodeIgniter-View

================================
SUMMARY:
================================

This project means that your Controller developer does not have to explicitly define which view/s to load up, and build complex data array structures that are parsed on the View end. For your View developer, they no longer have to know ANY PHP code as the View parser will convert the complex data array and automagically insert the data in the view (and will automagically load up languages as well).

================================
FEATURES:
================================

- In-built language support; no need to load and manage your language files in the controller
- Uses in-built Parser; Your Web Designer no longer needs to know PHP!
- Automagic view loading; Semi-intelligent loading of view files (thanks to Ocular for inspiration)
- Config file; change the defaults to suit your project
- Templating system; A simple, automagic templating system


View system for CodeIgniter {beta} - El Forum - 08-16-2012

[eluser]almightyolive[/eluser]
Did I mention that I wanted feedback? Anything and everything will be appreciated!!

What are the pros? The cons? The flaws?

Are there any projects out there that do something similar?

Do you think this project would be useful? Would YOU use this project, and why/why not?

Cheers!


View system for CodeIgniter {beta} - El Forum - 08-17-2012

[eluser]alexwenzel[/eluser]
I really like the idea of build-in language support. i maybe will adobt this idea for my own.


View system for CodeIgniter {beta} - El Forum - 08-24-2012

[eluser]almightyolive[/eluser]
Ok, I've made extensive changes to the code base. Note that I have changed the URL....

* Made it modular; you can now add stand-alone modules that will inject HTML into your template.
* Made it more sane; well, saner to me.... no longer big files that are hard to follow.
* Decoupled it from the CodeIgniter framework (so you can apply it to your own project if you like it)

As for the language stuff, I had to edit the core language file so that I could change the system default language (in some cases the system will load error messages in english, but the site language was something else).

Now my default site language can be dynamically set by a user cookie... see the following code:

Code:
$lang_temp = $this->input->cookie('language');
if ( $lang_temp )
{
  // NOTE: getDefault() is one of my core Lang.php modifications
  if (!($lang_temp === $this->lang->getDefault()))
  {
   // NOTE: setDefault() is one of my core Lang.php modifications
   $this->lang->setDefault($lang_temp);
  }
} else {
  // Set a cookie with system default
  $this->input->set_cookie('language', $this->lang->getDefault(), '7200', '.' . $_SERVER['HTTP_HOST'], '/', NULL, FALSE);
}

I also make it so that when I load a language I pass it directly to the view. The idea is that the Controller Developer doesn't care about managing the keys and sending specific messages... however your requirements may be different...

Also, this is using the parser. So in your language file you would have:

// French
$lang['menu_home'] = 'Maison';

You would refer to it as {menu_home}.

Code:
// Data array to be sent to parser
$data = array();

// Load up specific language file
$data = array_merge($data, $this->lang->load('filename', NULL, TRUE));
  
$this->parser->parse('view file', $data);



View system for CodeIgniter {beta} - El Forum - 08-24-2012

[eluser]Unknown[/eluser]
I have a 'hello world' that does almost the same thing - i just load one layout and then make the layout call the little views (elements) - take a look - this2.is-slick.com


View system for CodeIgniter {beta} - El Forum - 08-26-2012

[eluser]almightyolive[/eluser]
[quote author="rxhector" date="1345849915"]I have a 'hello world' that does almost the same thing - i just load one layout and then make the layout call the little views (elements) - take a look - this2.is-slick.com[/quote]

Yeah, pretty much the same thing! And I came to the same conclusion with what you said in Chapter 3; CodeIgniter allows way too much View code in the Controller.

The biggest difference between my code and your code (that I can see) is that I try and do away with the controller ever having to call
Code:
$this->load->view('template');

I do this by creating a Post-Controller hook that runs my View library's display() function. The display() function will then try to load the appropriate template and view (either through configuration or convention) and then parse the result.

The way I parse it is similar to Ocular (https://github.com/lonnieezell/Ocular-Template-Library). I freely admit that I borrowed heavily from this library and added my language customisations and the addition of 'Modules'.

For example if you had a welcome controller with the index function, the display() function will load up a template (based on router directory, controller or system default) and then a 'view instance' which matches the function. In this case, it will try to load up a view file named as ./application/views/welcome/index.php

Finally, it will glue these files together and another other values you have parsed through (as well as modules).