Skip to content

Commit

Permalink
Add debug tag that shows the assigned variables.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bauffman committed Feb 14, 2012
1 parent 36805db commit 4bd7bb9
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Environment.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ public function __construct(array $options = array())
'else' => 'Spoon\Template\Parser\ElseNode', 'else' => 'Spoon\Template\Parser\ElseNode',
'endif' => 'Spoon\Template\Parser\EndIfNode', 'endif' => 'Spoon\Template\Parser\EndIfNode',
'for' => 'Spoon\Template\Parser\ForNode', 'for' => 'Spoon\Template\Parser\ForNode',
'endfor' => 'Spoon\Template\Parser\EndForNode' 'endfor' => 'Spoon\Template\Parser\EndForNode',
'debug' => 'Spoon\Template\Parser\DebugNode'
); );
} }


Expand Down
33 changes: 33 additions & 0 deletions Parser/DebugNode.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of Spoon Library.
*
* (c) Davy Hellemans <davy@spoon-library.com>
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/

namespace Spoon\Template\Parser;
use Spoon\Template\TokenStream;
use Spoon\Template\Environment;
use Spoon\Template\Writer;

/**
* Writes the debug node to the writer.
*
* @author Davy Hellemans <davy@spoon-library.com>
*/
class DebugNode extends Node
{
/**
* Writes the compiled PHP code to the writer object.
*
* @param Spoon\Template\Writer $writer
*/
public function compile(Writer $writer)
{
$writer->write("var_dump(\$context);\n", $this->line);
}
}
9 changes: 8 additions & 1 deletion README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -162,4 +162,11 @@ These extra variables might come in handy for template designers. A brief exampl
</li> </li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}

##Debug
You can see which variables are available in the current template by using the debug tag.
This will use var_dump to show the contents of the assigned template variables.

{% debug %}

65 changes: 65 additions & 0 deletions Tests/DebugNodeTest.php
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/*
* This file is part of Spoon Library.
*
* (c) Davy Hellemans <davy@spoon-library.com>
*
* For the full copyright and license information, please view the license
* file that was distributed with this source code.
*/

namespace Spoon\Template\Tests;
use Spoon\Template\Autoloader;
use Spoon\Template\Writer;
use Spoon\Template\TokenStream;
use Spoon\Template\Token;
use Spoon\Template\Environment;
use Spoon\Template\Parser\DebugNode;

require_once realpath(dirname(__FILE__) . '/../') . '/Autoloader.php';
require_once 'PHPUnit/Framework/TestCase.php';

class DebugNodeTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Spoon\Template\Parser\DebugNode
*/
protected $node;

/**
* @var Spoon\Template\Writer
*/
protected $writer;

public function setUp()
{
Autoloader::register();
$this->writer = new Writer();

// {endif}
$stream = new TokenStream(
array(
new Token(Token::BLOCK_START, null, 1),
new Token(Token::NAME, 'endif', 1),
new Token(Token::BLOCK_END, null, 1)
)
);
$this->node = new DebugNode($stream, new Environment());
}

public function tearDown()
{
$this->writer = null;
$this->node = null;
}

public function testCompile()
{
$this->node->compile($this->writer);
$this->assertEquals(
"// line 1\nvar_dump(\$context);\n",
$this->writer->getSource()
);
}
}

0 comments on commit 4bd7bb9

Please sign in to comment.