Skip to content

Commit b21d8bf

Browse files
committed
added variable injection to partials.
1 parent 7ead110 commit b21d8bf

File tree

4 files changed

+19
-15
lines changed

4 files changed

+19
-15
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php Neuron\Mvc\Partial( 'test' ); ?>
1+
<?php Neuron\Mvc\partial( 'test' ); ?>

src/Bootstrap.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
* @throws \Exception
1717
*/
1818

19-
function Boot( string $ConfigPath ) : Application
19+
function boot( string $ConfigPath ) : Application
2020
{
2121
/** @var Neuron\Data\Setting\Source\ISettingSource $Settings */
2222

@@ -42,7 +42,7 @@ function Boot( string $ConfigPath ) : Application
4242
*
4343
* @param Application $App
4444
*/
45-
function Dispatch( Application $App ) : void
45+
function dispatch( Application $App ) : void
4646
{
4747
$Route = Get::filterScalar( 'route' ) ?? "";
4848

@@ -69,19 +69,20 @@ function Dispatch( Application $App ) : void
6969
* @param Application $App
7070
* @return int Number of entries removed
7171
*/
72-
function ClearExpiredCache( Application $App ) : int
72+
function clearExpiredCache( Application $App ) : int
7373
{
7474
return $App->clearExpiredCache();
7575
}
7676

7777
/**
7878
* Render a partial view from the shared directory.
7979
* This function looks for a file named _{name}.php in the shared views directory.
80-
* @param string $name
80+
* @param string $name The name of the partial (without underscore prefix or .php extension)
81+
* @param array $Data Optional data array to pass to the partial as variables
8182
* @return void
8283
* @throws NotFound
8384
*/
84-
function Partial( string $name ) : void
85+
function partial( string $name, array $Data = [] ) : void
8586
{
8687
$Path = Registry::getInstance()
8788
->get( "Views.Path" );
@@ -99,6 +100,9 @@ function Partial( string $name ) : void
99100
throw new NotFound( "Partial not found: $View" );
100101
}
101102

103+
// Extract data array as variables in the partial's scope
104+
extract( $Data );
105+
102106
ob_start();
103107
require( $View );
104108
$Content = ob_get_contents();

tests/BootstrapTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// Import the namespaced functions
1111
use function Neuron\Mvc\boot;
1212
use function Neuron\Mvc\dispatch;
13-
use function Neuron\Mvc\ClearExpiredCache;
13+
use function Neuron\Mvc\clearExpiredCache;
1414

1515
class BootstrapTest extends TestCase
1616
{
@@ -263,7 +263,7 @@ public function testClearExpiredCache()
263263
->willReturn( 42 ); // Mock cleared 42 entries
264264

265265
// Clear cache
266-
$ClearedCount = ClearExpiredCache( $App );
266+
$ClearedCount = clearExpiredCache( $App );
267267

268268
// Assert return value
269269
$this->assertEquals( 42, $ClearedCount );
@@ -281,7 +281,7 @@ public function testClearExpiredCacheNoEntries()
281281
->willReturn( 0 ); // No entries cleared
282282

283283
// Clear cache
284-
$ClearedCount = ClearExpiredCache( $App );
284+
$ClearedCount = clearExpiredCache( $App );
285285

286286
// Assert return value
287287
$this->assertEquals( 0, $ClearedCount );

tests/PartialTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use PHPUnit\Framework\TestCase;
88

99
// Import the namespaced function
10-
use function Neuron\Mvc\Partial;
10+
use function Neuron\Mvc\partial;
1111

1212
class PartialTest extends TestCase
1313
{
@@ -104,10 +104,10 @@ private function createPartial( string $Name, string $Content, ?string $Path = n
104104
/**
105105
* Helper to capture output from Partial function
106106
*/
107-
private function capturePartialOutput( string $Name ): string
107+
private function capturePartialOutput( string $Name, array $Data = [] ): string
108108
{
109109
ob_start();
110-
Partial( $Name );
110+
partial( $Name, $Data );
111111
return ob_get_clean();
112112
}
113113

@@ -131,7 +131,7 @@ public function testPartialNotFound()
131131
$this->expectException( NotFound::class );
132132
$this->expectExceptionMessage( 'Partial not found' );
133133

134-
Partial( 'nonexistent' );
134+
partial( 'nonexistent' );
135135
}
136136

137137
/**
@@ -284,7 +284,7 @@ public function testPartialWithExistingOutput()
284284
echo "Before partial ";
285285

286286
// Partial now echoes directly
287-
Partial( 'test' );
287+
partial( 'test' );
288288

289289
echo " After partial";
290290
$FullOutput = ob_get_clean();
@@ -336,4 +336,4 @@ public function testRealTestPartial()
336336

337337
$this->assertEquals( "This is a test.\n", $Result );
338338
}
339-
}
339+
}

0 commit comments

Comments
 (0)