Skip to content

Commit

Permalink
- 2019-12-21 3.32
Browse files Browse the repository at this point in the history
    * For #96 added $loop->even and $loop->odd and $loop->iteration
    * Also, **$loop->index now starts with 0 instead of 1**
  • Loading branch information
jorgecc committed Dec 21, 2019
1 parent f8ca692 commit ba1ad2a
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 28 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,9 @@ Also, BladeOneHTML adds multiple select, fixed values (without array), grouped s

## Version

- 2019-12-21 3.32
* For #96 added $loop->even and $loop->odd and $loop->iteration
* Also, **$loop->index now starts with 0 instead of 1**
- 2019-08-09 3.31 The operations _e(), _n() and _ef() used for language are now integrated on BladeOne.
So we don't need BladeOneLang anymore.
- 2019-06-14 3.30 Here it goes again.Returned cause psr-2 (but it breaks phpstorm so I added exceptions)
Expand Down
5 changes: 3 additions & 2 deletions examples/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
* Copyright (c) 2016 Jorge Patricio Castro Castillo MIT License.
*/

use eftec\bladeone\BladeOne;

include "../lib/BladeOne.php";

$blade=new \eftec\bladeone\BladeOne(null, null, \eftec\bladeone\BladeOne::MODE_DEBUG);

$blade=new BladeOne(null, null, BladeOne::MODE_DEBUG);

$blade->setCompiledExtension();

//<editor-fold desc="Example data">
$name="New User";
Expand Down
3 changes: 1 addition & 2 deletions examples/testloop.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@

$views = __DIR__ . '/views';
$compiledFolder = __DIR__ . '/compiled';
$blade=new BladeOne($views,$compiledFolder,BladeOne::MODE_SLOW);

$blade=new BladeOne($views,$compiledFolder,BladeOne::MODE_DEBUG);


//<editor-fold desc="Example data">
Expand Down
44 changes: 44 additions & 0 deletions examples/testloop2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Copyright (c) 2016 Jorge Patricio Castro Castillo MIT License.
*/
include "../lib/BladeOne.php";
use eftec\bladeone\BladeOne;

$views = __DIR__ . '/views';
$compiledFolder = __DIR__ . '/compiled';
$blade=new BladeOne($views,$compiledFolder,BladeOne::MODE_DEBUG);


$users=[];

$john=new stdClass();
$john->name="John";
$john->post=[];

$annah=new stdClass();
$annah->name="Annah";
$annah->post=[];

$post1=new stdClass();
$post1->subject="Hi there 1";

$post2=new stdClass();
$post2->subject="Hi there 2";

$john->posts[]=$post1;
$john->posts[]=$post2;

$annah->posts[]=$post1;
$annah->posts[]=$post2;

$users[]=$john;
$users[]=$annah;


try {
echo $blade->run("Test.loop2"
, ["users" => $users]);
} catch (Exception $e) {
echo "error found ".$e->getMessage()."<br>".$e->getTraceAsString();
}
24 changes: 12 additions & 12 deletions examples/views/Test/loop.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
</pre>

@foreach ($users as $user)
This is user {{ $user->id }}<br>
This is user {{ $john->id }}<br>
@endforeach
<br><h2>@@splitforeach</h2>
<em>Split a foreach cycle by showing a text each "x" number of elements.
Expand Down Expand Up @@ -113,7 +113,7 @@
@@endforelse
</pre>
@forelse ($users as $user)
<li>{{ $user->name }}</li>
<li>{{ $john->name }}</li>
@empty
<p>No users</p>
@endforelse
Expand All @@ -135,37 +135,37 @@
<br><h2>@@continue/break (foreach)</h2>
<pre>
@@foreach ($users as $user)
@@if($user-&gt;type == 1) // ignores the first user John Smith
@@if($john-&gt;type == 1) // ignores the first user John Smith
@@continue
@@endif
&lt;li&gt;@{{ $user->type }} - @{{ $user-&gt;name }}&lt;/li&gt;

@@if($user-&gt;number == 5) // ends the cycle.
@@if($john-&gt;number == 5) // ends the cycle.
@@break
@@endif
@@endforeach
</pre>
@foreach ($users as $user)
@if($user->type == 1)
@if($john->type == 1)
@continue
@endif
{{ $user->type }} - {{ $user->name }}<br>
@if($user->number == 5)
{{ $john->type }} - {{ $john->name }}<br>
@if($john->number == 5)
@break
@endif
@endforeach
<pre>
@@foreach ($users as $user)
@@continue($user-&gt;type == 1)
@@continue($john-&gt;type == 1)
@{{ $user->type }} - @{{ $user-&gt;name }}&lt;/br&gt;
@@break($user-&gt;number == 5)
@@break($john-&gt;number == 5)
@@endforeach
</pre>

@foreach ($users as $user)
@continue($user->type == 1)
{{ $user->type }} -{{ $user->name }}</br>
@break($user->number == 5)
@continue($john->type == 1)
{{ $john->type }} -{{ $john->name }}</br>
@break($john->number == 5)
@endforeach
<br><h2>@@each</h2>
<pre>
Expand Down
24 changes: 24 additions & 0 deletions examples/views/Test/loop2.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<html>
<head>
<title>Example of Loop 2</title>
<!-- Google Fonts -->
<link rel="stylesheet" href="//fonts.googleapis.com/css?family=Roboto:300,300italic,700,700italic">
<!-- CSS Reset -->
<link rel="stylesheet" href="//cdn.rawgit.com/necolas/normalize.css/master/normalize.css">
<!-- Milligram CSS minified -->
<link rel="stylesheet" href="//cdn.rawgit.com/milligram/milligram/master/dist/milligram.min.css">
</head>
<body>
<h1>Example of Loops</h1>
@foreach ($users as $user)
<hr>
Name user:{{$user->name}}<br>
<ul>
@foreach ($user->posts as $post)
<li>{{$loop->parent->index}} {{$loop->index}} {{$loop->even}}: Subject:{{$post->subject}}</li>

@endforeach
</ul>
@endforeach
</body>
</html>
2 changes: 1 addition & 1 deletion examples/views/Test/test24.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
@endforelse

@forelse ($emptyArray as $user)
<li>{{ $user->name }}</li>
<li>{{ $john->name }}</li>
@empty
<p>No users</p>
@endforelse
Expand Down
12 changes: 9 additions & 3 deletions lib/BladeOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
* @copyright Copyright (c) 2016-2019 Jorge Patricio Castro Castillo MIT License.
* Don't delete this comment, its part of the license.
* Part of this code is based in the work of Laravel PHP Components.
* @version 3.31 2019-08-09
* @version 3.32 2019-12-21
* @link https://github.com/EFTEC/BladeOne
*/
class BladeOne
Expand Down Expand Up @@ -1304,10 +1304,13 @@ public function addLoop($data)
$length = \is_array($data) || $data instanceof Countable ? \count($data) : null;
$parent = static::last($this->loopsStack);
$this->loopsStack[] = [
'index' => 0,
'index' => -1,
'iteration' => 0,
'remaining' => isset($length) ? $length + 1 : null,
'count' => $length,
'first' => true,
'even' => false,
'odd' => false,
'last' => isset($length) ? $length == 1 : null,
'depth' => \count($this->loopsStack) + 1,
'parent' => $parent ? (object)$parent : null,
Expand All @@ -1323,7 +1326,10 @@ public function incrementLoopIndices()
{
$loop = &$this->loopsStack[\count($this->loopsStack) - 1];
$loop['index']++;
$loop['first'] = $loop['index'] == 1;
$loop['iteration']++;
$loop['first'] = $loop['index'] == 0;
$loop['even']=$loop['index'] % 2 ==0;
$loop['odd']=!$loop['even'];
if (isset($loop['count'])) {
$loop['remaining']--;
$loop['last'] = $loop['index'] == $loop['count'];
Expand Down
41 changes: 33 additions & 8 deletions tests/LoopTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
* @author G.J.W. Oolbekkink <[email protected]>
* @since 17/09/2018
*/
class LoopTest extends AbstractBladeTestCase {
class LoopTest extends AbstractBladeTestCase
{
/**
* @throws \Exception
*/
public function testFor() {
public function testFor()
{
$bladeSource = /** @lang Blade */
<<<'BLADE'
@for($i = 1; $i <= 6; $i++)
Expand All @@ -23,7 +25,8 @@ public function testFor() {
/**
* @throws \Exception
*/
public function testForEach() {
public function testForEach()
{
$bladeSource = /** @lang Blade */
<<<'BLADE'
@foreach($items as $item)
Expand All @@ -32,11 +35,33 @@ public function testForEach() {
BLADE;
$this->assertEqualsIgnoringWhitespace("Item a Item b", $this->blade->runString($bladeSource, ['items' => ['a', 'b']]));
}

/**
* @throws \Exception
*/
public function testForElse() {
public function testForEachLoopVar()
{
$bladeSource = /** @lang Blade */
<<<'BLADE'
@foreach($items as $item)
Item_index:{{$loop->index}},iteration:{{$loop->iteration}},first:{{$loop->first}}
,last:{{$loop->last}},remaining:{{$loop->remaining}}
,count:{{$loop->count}},depth:{{$loop->depth}},parent:{{$loop->parent}}
,even:{{$loop->even}},odd:{{$loop->odd}},value{{ $item }}
@endforeach
BLADE;
$this->assertEqualsIgnoringWhitespace("Item_index:0,iteration:1,first:1"."
,last:,remaining:2"."
,count:2,depth:1,parent:"."
,even:1,odd:,valuea"."
Item_index:1,iteration:2,first:"."
,last:,remaining:1,count:2,depth:1,parent:"."
,even:,odd:1,valueb", $this->blade->runString($bladeSource, ['items' => ['a', 'b']]));
}
/**
* @throws \Exception
*/
public function testForElse()
{
$bladeSource = /** @lang Blade */
<<<'BLADE'
@forelse($items as $item)
Expand All @@ -52,7 +77,8 @@ public function testForElse() {
/**
* @throws \Exception
*/
public function testWhile() {
public function testWhile()
{
$bladeSource = <<<'BLADE'
@php($i = 1)
@while($i < 5)
Expand All @@ -61,6 +87,5 @@ public function testWhile() {
@endwhile
BLADE;
$this->assertEqualsIgnoringWhitespace("Item 2 Item 3 Item 4 Item 5", $this->blade->runString($bladeSource, []));

}
}
}

0 comments on commit ba1ad2a

Please sign in to comment.