Commit:    e8bab3538a9d05d0da584c2ff8b18c7d9eea2fdd
Author:    Peter Kokot <[email protected]>         Thu, 23 May 2019 02:10:03 
+0200
Parents:   fe351f79ddc962b22235f00e66a13d8b4f4034f5
Branches:  master

Link:       
http://git.php.net/?p=web/bugs.git;a=commitdiff;h=e8bab3538a9d05d0da584c2ff8b18c7d9eea2fdd

Log:
Add variables import to included templates

Changed paths:
  M  docs/templates.md
  M  src/Template/Context.php
  M  tests/Unit/Template/ContextTest.php
  A  tests/fixtures/templates/forms/form_2.php
  M  tests/fixtures/templates/pages/including.php


Diff:
diff --git a/docs/templates.md b/docs/templates.md
index cea0976..0623d91 100644
--- a/docs/templates.md
+++ b/docs/templates.md
@@ -73,8 +73,13 @@ To include a partial template snippet file:
 <?php $this->include('forms/report_bug.php') ?>
 ```
 
-which is equivalent to `<?php include __DIR__.'/../forms/report_bug.php' ?>`.
-The variable scope is inherited by the template that included the file.
+which is equivalent to `<?php include __DIR__.'/../forms/contact.php' ?>`,
+except that the variable scope is not inherited by the template that included
+the file. To import variables into the included template snippet file:
+
+```php
+<?php $this->include('forms/contact.php', ['formHeading' => 'value', 'foo' => 
'bar']) ?>
+```
 
 ## Blocks
 
diff --git a/src/Template/Context.php b/src/Template/Context.php
index eaf9403..98994f4 100644
--- a/src/Template/Context.php
+++ b/src/Template/Context.php
@@ -141,8 +141,14 @@ class Context
      *
      * @return mixed
      */
-    public function include(string $template)
+    public function include(string $template, array $variables = [])
     {
+        if (count($variables) > extract($variables, EXTR_SKIP)) {
+            throw new \Exception(
+                'Variables with numeric names $0, $1... cannot be imported to 
scope '.$template
+            );
+        }
+
         return include $this->dir.'/'.$template;
     }
 
diff --git a/tests/Unit/Template/ContextTest.php 
b/tests/Unit/Template/ContextTest.php
index 8b82de7..c4d2252 100644
--- a/tests/Unit/Template/ContextTest.php
+++ b/tests/Unit/Template/ContextTest.php
@@ -52,6 +52,14 @@ class ContextTest extends TestCase
         $this->assertEquals(include TEST_FIXTURES_DIRECTORY . 
'/templates/includes/variable.php', $variable);
     }
 
+    public function testIncludeOnInvalidVariableCounts(): void
+    {
+        $this->expectException(\Exception::class);
+        $this->expectExceptionMessage('Variables with numeric names $0, $1... 
cannot be imported to scope includes/variable.php');
+
+        $this->context->include('includes/variable.php', ['var1', 'var2', 
'var3']);
+    }
+
     /**
      * @dataProvider attacksProvider
      */
diff --git a/tests/fixtures/templates/forms/form_2.php 
b/tests/fixtures/templates/forms/form_2.php
new file mode 100644
index 0000000..ba4a660
--- /dev/null
+++ b/tests/fixtures/templates/forms/form_2.php
@@ -0,0 +1,10 @@
+<?php $this->append('scripts'); ?>
+<script src="/path/to/file_2.js"></script>
+<?php $this->end('scripts'); ?>
+
+<?= $this->e($importedVariable) ?>
+
+<form method="post">
+<input type="text" name="foo">
+<input type="submit" value="Submit">
+</form>
diff --git a/tests/fixtures/templates/pages/including.php 
b/tests/fixtures/templates/pages/including.php
index 746b59a..fd157bd 100644
--- a/tests/fixtures/templates/pages/including.php
+++ b/tests/fixtures/templates/pages/including.php
@@ -1,5 +1,6 @@
 <?php $this->extends('layout.php', ['title' => 'Testing blocks appends']) ?>
 
 <?php $this->start('content') ?>
-<?php $this->include('forms/form.php') ?>
+<?php $someVariable = 'foobarbaz' ?>
+<?php $this->include('forms/form_2.php', ['importedVariable' => 
$someVariable]) ?>
 <?php $this->end('content') ?>


--
PHP Webmaster List Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to