Skip to content

Commit 4bd7bb9

Browse files
author
Bauffman
committed
Add debug tag that shows the assigned variables.
1 parent 36805db commit 4bd7bb9

File tree

4 files changed

+108
-2
lines changed

4 files changed

+108
-2
lines changed

Environment.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ public function __construct(array $options = array())
116116
'else' => 'Spoon\Template\Parser\ElseNode',
117117
'endif' => 'Spoon\Template\Parser\EndIfNode',
118118
'for' => 'Spoon\Template\Parser\ForNode',
119-
'endfor' => 'Spoon\Template\Parser\EndForNode'
119+
'endfor' => 'Spoon\Template\Parser\EndForNode',
120+
'debug' => 'Spoon\Template\Parser\DebugNode'
120121
);
121122
}
122123

Parser/DebugNode.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Spoon Library.
5+
*
6+
* (c) Davy Hellemans <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the license
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Spoon\Template\Parser;
13+
use Spoon\Template\TokenStream;
14+
use Spoon\Template\Environment;
15+
use Spoon\Template\Writer;
16+
17+
/**
18+
* Writes the debug node to the writer.
19+
*
20+
* @author Davy Hellemans <[email protected]>
21+
*/
22+
class DebugNode extends Node
23+
{
24+
/**
25+
* Writes the compiled PHP code to the writer object.
26+
*
27+
* @param Spoon\Template\Writer $writer
28+
*/
29+
public function compile(Writer $writer)
30+
{
31+
$writer->write("var_dump(\$context);\n", $this->line);
32+
}
33+
}

README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,4 +162,11 @@ These extra variables might come in handy for template designers. A brief exampl
162162
</li>
163163
{% endfor %}
164164
</ul>
165-
{% endif %}
165+
{% endif %}
166+
167+
##Debug
168+
You can see which variables are available in the current template by using the debug tag.
169+
This will use var_dump to show the contents of the assigned template variables.
170+
171+
{% debug %}
172+

Tests/DebugNodeTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Spoon Library.
5+
*
6+
* (c) Davy Hellemans <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the license
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Spoon\Template\Tests;
13+
use Spoon\Template\Autoloader;
14+
use Spoon\Template\Writer;
15+
use Spoon\Template\TokenStream;
16+
use Spoon\Template\Token;
17+
use Spoon\Template\Environment;
18+
use Spoon\Template\Parser\DebugNode;
19+
20+
require_once realpath(dirname(__FILE__) . '/../') . '/Autoloader.php';
21+
require_once 'PHPUnit/Framework/TestCase.php';
22+
23+
class DebugNodeTest extends \PHPUnit_Framework_TestCase
24+
{
25+
/**
26+
* @var Spoon\Template\Parser\DebugNode
27+
*/
28+
protected $node;
29+
30+
/**
31+
* @var Spoon\Template\Writer
32+
*/
33+
protected $writer;
34+
35+
public function setUp()
36+
{
37+
Autoloader::register();
38+
$this->writer = new Writer();
39+
40+
// {endif}
41+
$stream = new TokenStream(
42+
array(
43+
new Token(Token::BLOCK_START, null, 1),
44+
new Token(Token::NAME, 'endif', 1),
45+
new Token(Token::BLOCK_END, null, 1)
46+
)
47+
);
48+
$this->node = new DebugNode($stream, new Environment());
49+
}
50+
51+
public function tearDown()
52+
{
53+
$this->writer = null;
54+
$this->node = null;
55+
}
56+
57+
public function testCompile()
58+
{
59+
$this->node->compile($this->writer);
60+
$this->assertEquals(
61+
"// line 1\nvar_dump(\$context);\n",
62+
$this->writer->getSource()
63+
);
64+
}
65+
}

0 commit comments

Comments
 (0)