Skip to content

Commit

Permalink
[WebBundle] Fixed issue in Mustache when a value is not provided for …
Browse files Browse the repository at this point in the history
…a token.
  • Loading branch information
kriswallsmith authored and fabpot committed Apr 21, 2010
1 parent 9313e26 commit c6f21e4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/Symfony/Framework/WebBundle/Util/Mustache.php
Expand Up @@ -20,14 +20,19 @@
*/
class Mustache
{
static public function renderFile($file, $parameters)
static public function renderString($string, $parameters)
{
$replacer = function ($match) use($parameters)
{
return isset($parameters[$match[1]]) ? $parameters[$match[1]] : "{{ $match[0] }}";
return isset($parameters[$match[1]]) ? $parameters[$match[1]] : $match[0];
};

file_put_contents($file, preg_replace_callback('/{{\s*(.+?)\s*}}/', $replacer, file_get_contents($file)));
return preg_replace_callback('/{{\s*(.+?)\s*}}/', $replacer, $string);
}

static public function renderFile($file, $parameters)
{
file_put_contents($file, static::renderString(file_get_contents($file), $parameters));
}

static public function renderDir($dir, $parameters)
Expand Down
25 changes: 25 additions & 0 deletions tests/Symfony/Tests/Framework/WebBundle/Util/MustacheTest.php
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of the symfony package.
*
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Framework\WebBundle\Util;

use Symfony\Framework\WebBundle\Util\Mustache;

class MustacheTest extends \PHPUnit_Framework_TestCase
{
public function testRenderString()
{
$template = 'Hi {{ you }}, my name is {{ me }}!';
$expected = 'Hi {{ you }}, my name is Kris!';

$this->assertEquals(Mustache::renderString($template, array('me' => 'Kris')), $expected, '::renderString() does not modify unknown parameters');
}
}

0 comments on commit c6f21e4

Please sign in to comment.