Pārlūkot izejas kodu

add support for template comments

michelphp 3 nedēļas atpakaļ
vecāks
revīzija
0b53844ee6
2 mainītis faili ar 19 papildinājumiem un 3 dzēšanām
  1. 9 3
      src/Engine.php
  2. 10 0
      tests/PurePlateTest.php

+ 9 - 3
src/Engine.php

@@ -92,11 +92,17 @@ final class Engine
     {
         $html = file_get_contents($path);
         $this->blocks = [];
-        $lines = explode("\n", $html);
+        $lines = explode(PHP_EOL, $html);
         $output = "";
 
         foreach ($lines as $i => $line) {
             $num = $i + 1;
+
+            $line = preg_replace('/{#.*?#}/', '', $line);
+            if (trim($line) === '') {
+                $output .= $line .PHP_EOL;
+                continue;
+            }
             $line = preg_replace_callback('/{{(.*?)}}|{% (.*?) %}/', function ($m) use ($num, $path) {
                 $isBlock = !empty($m[2]);
                 $content = trim($isBlock ? $m[2] : $m[1]);
@@ -110,7 +116,7 @@ final class Engine
 
             }, $line);
 
-            $output .= $line . "\n";
+            $output .= $line .PHP_EOL;
         }
         return $output;
     }
@@ -138,7 +144,7 @@ final class Engine
 
         if ($cmd === 'include') {
             $phpExpr = $this->parseTokens($rawExpr);
-            return "<?php \$_epure->render($phpExpr); ?>";
+            return "<?php \$_pure->render($phpExpr); ?>";
         }
 
         if ($cmd === 'set') {

+ 10 - 0
tests/PurePlateTest.php

@@ -38,6 +38,7 @@ final class PurePlateTest extends TestCase
 
     protected function execute(): void
     {
+        $this->itRendersComment();
         $this->itRendersSimpleVariables();
         $this->itHandlesFiltersWithArguments();
         $this->itExecutesLogicBlocks();
@@ -50,6 +51,15 @@ final class PurePlateTest extends TestCase
         $this->itHandlesNativeArraySyntax();
     }
 
+    public function itRendersComment()
+    {
+        $tpl = "{#        {% include 'flash_bag.html.twig' %}#}\n{#        {% include 'flash_bag.html.twig' %}#}";
+        file_put_contents($this->tplDir . '/comment.html', $tpl);
+        $output = $this->engine->render('comment.html', []);
+        $this->assertEquals('', trim($output));
+
+    }
+
     /** @test */
     public function itRendersSimpleVariables()
     {