Skip to content

Commit

Permalink
master : Blade Alternate - this is an updated verion of /Kindari/blad…
Browse files Browse the repository at this point in the history
…e-alternator
  • Loading branch information
Aden committed Dec 2, 2014
0 parents commit 342a123
Show file tree
Hide file tree
Showing 8 changed files with 782 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/vendor
composer.phar
composer.lock
.DS_Store
7 changes: 7 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## License

Blade-Alternate is offered as free software: you can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License(WTFPL), any version.

When obtained under the Do What The Fuck You Want To Public License, no fucking warranty or guarantee is given. You accept that a band of furious weasels may infest your undergarments as a result of using this software, and acknowledge that it is your own fucking fault. You should have received a copy of the Do What The Fuck You Want To Public License along with Blade-Alternate. If not, you may obtain it [here](http://sam.zoy.org/wtfpl/).

When obtained under the GNU General Public License, Blade-Alternate is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with Blade-Alternate. If not, you may obtain it [here](http://www.gnu.org/licenses/).
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Blade Alternate

Blade Alternate is a small extension to blade that adds the common operation of changing values throughout a loop. Now thats a horrible description, so heres an example:

@foreach($users as $user)
<tr class="@alternate('odd', 'even')">...</tr>
@endforeach

Which would result in alternating rows of a table, with the class switching between odd and even every time. This works with any number of arguments, for example, you might see a image gallery with three columns.

@foreach($images as $image)
<img src="{{ $img->url }}" class="@alternate('left', 'mid', 'right')" />
@endforeach

## Installation

Add `adenfraser/blade-alternate` to `composer.json`.

"adenfraser/blade-alternate": "1.0.*"

Run `composer update` to pull down the latest version of Blade Alternate. Now open up `app/config/app.php` and add the service provider to your `providers` array, *after* the ViewServiceProvider.

'providers' => array(
...
'Illuminate\View\ViewServiceProvider',
'AdenFraser\BladeAlternate\BladeAlternateServiceProvider',
...
)
20 changes: 20 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "adenfraser/blade-alternate",
"description": "",
"authors": [
{
"name": "AdenFraser",
"email": "[email protected]"
}
],
"require": {
"php": ">=5.3.0",
"illuminate/support": "4.*"
},
"autoload": {
"psr-0": {
"adenfraser\\BladeAlternate": "src/"
}
},
"minimum-stability": "dev"
}
621 changes: 621 additions & 0 deletions licenses/GPLv3.txt

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions licenses/WTFPL.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004

Copyright (C) 2004 Sam Hocevar <[email protected]>

Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.

DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

0. You just DO WHAT THE FUCK YOU WANT TO.
34 changes: 34 additions & 0 deletions src/AdenFraser/BladeAlternate/BladeAlternate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php namespace adenfraser\BladeAlternate;

class BladeAlternate {

public $counts = array();

public function increment($key) {

if ( ! array_key_exists($key, $this->counts) ) $this->reset($key);
$this->counts[$key]++;
return $this->counts[$key];
}

public function reset($key) {
$this->counts[$key] = 0;
return $this;
}

public function choose() {

$items = func_get_args();
$index = $this->increment($key = $this->getKey() );
if ($index == count($items)) $this->reset($key);
return $items[$index - 1];
}

public function getKey() {
$bt = debug_backtrace();
$trace = $bt[1];
return "{$trace['file']}@{$trace['line']}";

}

}
55 changes: 55 additions & 0 deletions src/AdenFraser/BladeAlternate/BladeAlternateServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php namespace adenfraser\BladeAlternate;

use Illuminate\Support\ServiceProvider;

class BladeAlternateServiceProvider extends ServiceProvider {

/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;


/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->registerAlternate();

$this->registerBladeExtension();
}

public function registerAlternate() {

$this->app->singleton('blade.alternator', 'adenfraser\BladeAlternate\BladeAlternate');

}

public function registerBladeExtension() {

$compiler = $this->app['view.engine.resolver']->resolve('blade')->getCompiler();
$compiler->extend(function($value, $blade){

$pattern = $blade->createMatcher('alternate');
return preg_replace($pattern, "$1<?php echo app('blade.alternator')->choose$2; ?>", $value);
});

}


/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return array('blade.alternator');
}

}

0 comments on commit 342a123

Please sign in to comment.