Skip to content

Commit

Permalink
Use icon helper with different icon-set
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Dlugosz committed Sep 20, 2015
1 parent 5708f4f commit 48386aa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
35 changes: 35 additions & 0 deletions Tests/Twig/BootstrapIconExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,23 @@ public function testIconFilter()
);
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapIconExtension::iconFunction
*/
public function testIconFilterWithDifferntPrefix()
{
$this->assertEquals(
'<span class="glyphicon glyphicon-heart"></span>',
$this->getIconExtension('default')->iconFunction('heart', 'glyphicon'),
'->iconFunction() returns the HTML code for the given icon.'
);
$this->assertEquals(
'<span class="fa fa-heart"></span>',
$this->getIconExtension('default')->iconFunction('heart', 'fa'),
'->iconFunction() uses the iconPrefix passed into the IconExtension constructor.'
);
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapIconExtension::parseIconsFilter
*/
Expand All @@ -79,6 +96,24 @@ public function testParseIconsFilter()
);
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapIconExtension::parseIconsFilter
*/
public function testParseIconsFilterWithDifferntPrefix()
{
$this->assertEquals(
'<span class="glyphicon glyphicon-heart"></span> foobar',
$this->getIconExtension('default')->parseIconsFilter('.glyphicon-heart foobar'),
'->parseIconsFilter() returns the HTML code with the replaced icons.'
);

$this->assertEquals(
'<span class="fa fa-heart"></span> foobar',
$this->getIconExtension('default')->parseIconsFilter('.fa-heart foobar'),
'->parseIconsFilter() uses the iconPrefix passed into the IconExtension constructor.'
);
}

/**
* @covers Braincrafted\Bundle\BootstrapBundle\Twig\BootstrapIconExtension::getName()
*/
Expand Down
12 changes: 7 additions & 5 deletions Twig/BootstrapIconExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public function parseIconsFilter($text)
$that = $this;

return preg_replace_callback(
'/\.icon-([a-z0-9+-]+)/',
'/\.([a-z]+)-([a-z0-9+-]+)/',
function ($matches) use ($that) {
return $that->iconFunction($matches[1]);
return $that->iconFunction($matches[2], $matches[1]);
},
$text
);
Expand All @@ -94,14 +94,16 @@ function ($matches) use ($that) {
* Returns the HTML code for the given icon.
*
* @param string $icon The name of the icon
* @param string $iconSet The icon-set name
*
* @return string The HTML code for the icon
*/
public function iconFunction($icon)
public function iconFunction($icon, $iconSet = 'icon')
{
$icon = str_replace('+', ' '.$this->iconPrefix.'-', $icon);
if ($iconSet == 'icon') $iconSet = $this->iconPrefix;
$icon = str_replace('+', ' '.$iconSet.'-', $icon);

return sprintf('<%1$s class="%2$s %2$s-%3$s"></%1$s>', $this->iconTag, $this->iconPrefix, $icon);
return sprintf('<%1$s class="%2$s %2$s-%3$s"></%1$s>', $this->iconTag, $iconSet, $icon);
}

/**
Expand Down

0 comments on commit 48386aa

Please sign in to comment.