Partial Template support for li3
Plugin to pass template sections from view to the layout., (*1)
Installation
- Clone/Download the plugin into your app's
libraries
directory.
-
Tell your app to load the plugin by adding the following to your app's config/bootstrap/libraries.php
:, (*2)
Libraries::add('li3_partials');
Features
- Assign strings or entire blocks of markup to a partial.
Usage
Blocks, (*3)
There have been a number of times I was working on a project and had a clean layout rendered but the client required that something in the layout change based on what view was rendered. I used elements and view context
asignments for a while but that was neither elegant or enjoyable., (*4)
This is designed as a way to pass markup changes to section in a layout based on your view., (*5)
-
In your view
wrap the markup you want passed to the layout in <partial></partial>
tags with a name
attribute., (*6)
html
<partial name="sidebar"><h2>Sidebar for this view!</h2></partial>
, (*7)
-
In your layout
There are 2 ways to print a blocked partial in your view, (*8)
-
Call the blocks partial name and assign type block
, (*9)
php
<?php echo $this->partial->sidebar(array('type' => 'block')); ?>
, (*10)
-
Call a partial block and pass the partials name, (*11)
php
<?php echo $this->partial->block('sidebar');?>
, (*12)
Anywhere that you decide to place either of those will render the partial that was defined in your view at that location., (*13)
Strings, (*14)
Similar to blocks, this method is used to pass strings of text to a layout., (*15)
"Why?" asks you, "Good question!" I reply, (*16)
Think in terms of a page description or keywords, these may need to change based on a page view but I could find no easy way to get these requirements to the layout., (*17)
-
In your view
In the head of your view template (or anywhere, really, I just think it's cleaner to keep these together at the top) add:, (*18)
php
<?php $this->partial->keywords('awesome, li3, github, php, partials, woot, nifty, grand, pie, unicorns, alfalfa sprouts'); ?>
It doesn't matter what you name this method, just keep in mind that that name will be how you call it in the layout., (*19)
-
In your layout
Ok, so we defined keywords for our view! lets add them to the meta tag, (*20)
html
<meta name="keywords" content="<?php echo $this->partial->keywords(); ?>" />
The plugin will find the stored keywords partial and render its contents where it was called., (*21)
There is also a wrapper to ensure you only pull strings - much like blocks, (*22)
``` php
partial->string('keyword'); ?>, (*23)
_"That's foolish, it's easier just to use the other method!"_ you exclaim.
Hold tight, Fredword! I'll explain why you might want to do this below.
## Sharing Names
There may be a case where you define both a partial string and partial block with the same name.
To ensure that you pull the right one thee are 2 methods for each (actually 3 for .
__Strings__
```php
<?php echo $this->partial->string('methodName'); ?>
<?php echo $this->partial->methodName(array('type' => 'string')); ?>
Blocks, (*24)
<?php echo $this->partial->block('methodName'); ?>
<?php echo $this->partial->methodName(array('type' => 'block')); ?>
To come
- I plan on adding enhanced cache features to this so the rendering engine isn't constantly parsing templates for partials
- Dynamic partials - support for a data schema to auto load partials from a database.
- Drink a beer. Why not?
Contribute
Have ideas for improvements or features? Send a pull request, I would welcome collaboration!, (*25)