Welcome Guest, Not a member yet? Register   Sign In
New Layout View Library Supporting Partial Views [Update 2]
#1

[eluser]Gyorgy Fekete[/eluser]
Hey, this is update 2 of my layout view library. You can check out the original library and what is all about here: http://ellislab.com/forums/viewthread/62366/

Update 2: It's partially rewritten code. Now supporting auto loading data variables in partial views. And is PHP5 compatible ONLY.

I will write down each method in detail:

clear()
Without any argument it resets the layout library.
If of one of these strings 'partials','datas','layout' then is resets the respective partials, data variables or layout parts.
Example:
Code:
$this->layout->clear();
$this->layout->clear('partials');
$this->layout->clear('datas');
$this->layout->clear('layout');

addPartial(arg1,arg2,arg3)
Adds a new partial view to the current layout.
arg1 = the name of the partial view (not the filename)
arg2 = the filename of the partial view (without the .php extension)
arg3 = optional data array to be passed to the partial view

addPartialData(arg1, arg2)
Adds data variables in form of an array to a partial view.
arg1 = the name of the partial view (not the filename)
arg2 = the data array to be passed to the partial view

addPartialLayout(arg1,arg2)
Adds a partial layout to the current layout.
arg1 = the name of the partial layout
arg2 = optional argument. The filename of the partial layout (without the .php extension). It can be set later with the setLayout() method.

Example:
Code:
$this->addPartialayout('header','partials/header_layout');
$this->header->addPartial('header_left','partials/header/header_left');

hasPartial(arg1)
Checks if the current layout has the specified partial view.
arg1 = the name of the partial view (not the filename)

hasData(arg1)
Checks if the current partial view has any data defined.
arg1 = the name of the partial view (not the filename)

getPartialType(arg1)
Returns the type of the specified partial view.
arg1 = the name of the partial view (not the filename)
returns: partial view, partial layout or NULL if the specified partial doesn't exists

getLayout()
Returns the filename of the current layout. Layouts doesn't have name. You only set the filename (without the .php extension)

setLayout(arg1)
Sets the current layout.
arg1 = the filename of the layout (without the .php extension)

view(arg1)
Render the layout.
arg1 = optional argument. Boolean, whether to render or return the layout.


Example of using this library:

Code:
$this->layout->clear();

$this->layout->addPartialLayout('header','test/header');
$this->layout->header->addPartial('bottom','test/bottom');

$this->layout->header->addPartialLayout('top','test/top');
$this->layout->header->top->addPartial('top_left','test/top_left');
$this->layout->header->top->addPartial('top_right','test/top_right');
        
$footer_data['custom_data'] = 'this is footer custom data';
$this->layout->addPartial('footer','test/footer',$footer_data);
$this->layout->setLayout('test/test_layout');
        
$this->layout->view();

You can set an auto loading configuration file. Just name it after the layout library filename and put it into your configs folder.

Example configuration file:
Code:
/*
reserved words are between ::
*/

$config[':partials:'] = array('header' => ':layout:','footer' => 'test/footer');

$config[':layoutfile:'] = 'test/test_layout';
$config[':datas:']['footer']['custom_data'] = 'this is footer custom data';


$config['header'][':layoutfile:'] = 'test/header';
$config['header'][':partials:'] = array('top' => ':layout:', 'bottom' => 'test/bottom');

$config['header']['top'][':layoutfile:'] = 'test/top';
$config['header']['top'][':partials:'] = array('top_left' => 'test/top_left', 'top_right' => 'test/top_right');

The word between :: are reserved words. It is self explanatory.

You can download a working example with the library included here. After downloading just extract the zip file into your application folder. And browse to test/configure_no or test/configure_yes controllers. The first doesn't auto load the configuration file the second auto loads it.

The link is: http://www.primalskill.com/layout_update2.zip
#2

[eluser]Unknown[/eluser]
Hello Gyorgy Fekete.
Thanks for your countribution.

Quote:And browse to test/configure_no or test/configure_yes controllers

Since the functions in test.php are config_yes & config_no I think we must browse test/config_yes (or "no").

However. Look at the error message I've got:
Code:
[20-Oct-2007 09:23:04] PHP Fatal error:  Call to a member function view() on a non-object in \ci\system\application\controllers\test.php on line 16
#3

[eluser]Thorpe Obazee[/eluser]
[quote author="rainbow2009" date="1240900529"]pret auto
Your link worked well. Many thanks to summer student ^_^[/quote]

Actually, 'Summer Student' is a rank Smile
#4

[eluser]Unknown[/eluser]
Ahh now that makes sense, I had argument issues until I saw this.
#5

[eluser]Renea077[/eluser]
Thank you very much for this information. I was also having argument issues. I really didn't get it at first, but it does make a lot of sense now. I really appreciate the examples you presented, these have been very helpful in my understanding of this issue.




Theme © iAndrew 2016 - Forum software by © MyBB