Skip to content
Permalink
Browse files

Add debug tag that shows the assigned variables.

  • Loading branch information
bauffman committed Feb 14, 2012
1 parent 36805db commit 4bd7bb9b6278d3308d98f5b59d8c2e074be339d3
Showing with 108 additions and 2 deletions.
  1. +2 −1 Environment.php
  2. +33 −0 Parser/DebugNode.php
  3. +8 −1 README.md
  4. +65 −0 Tests/DebugNodeTest.php
@@ -116,7 +116,8 @@ public function __construct(array $options = array())
'else' => 'Spoon\Template\Parser\ElseNode',
'endif' => 'Spoon\Template\Parser\EndIfNode',
'for' => 'Spoon\Template\Parser\ForNode',
'endfor' => 'Spoon\Template\Parser\EndForNode'
'endfor' => 'Spoon\Template\Parser\EndForNode',
'debug' => 'Spoon\Template\Parser\DebugNode'
);
}

@@ -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);
}
}
@@ -162,4 +162,11 @@ These extra variables might come in handy for template designers. A brief exampl
</li>
{% endfor %}
</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 %}
@@ -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.
You can’t perform that action at this time.