Skip to content

Commit

Permalink
added logic to utilize passed vs assigned chart element ids
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinkhill committed Apr 12, 2017
1 parent 8640931 commit 9919188
Showing 1 changed file with 18 additions and 23 deletions.
41 changes: 18 additions & 23 deletions src/Symfony/Bundle/Twig/LavachartsExtension.php
Expand Up @@ -7,13 +7,6 @@

class LavachartsExtension extends \Twig_Extension
{
/**
* Twig Extension Name
*
* @var string
*/
const NAME = 'lavacharts_twig_extension';

/**
* The Lavacharts object passed in from the service container.
*
Expand All @@ -32,9 +25,16 @@ public function __construct(Lavacharts $lava)
}

/**
* Add Twig functions as short-cut methods to the rendering methods.
* Add Twig functions to utilize Lavacharts from the container.
*
* To render, just use the lowercase name of the chart:
* and pass through the raw filter.
*
* Example:
* {{ linechart('Stocks")|raw }}
*
*
* @return array
* @return array Array of twig functions
*/
public function getFunctions()
{
Expand All @@ -44,33 +44,28 @@ public function getFunctions()

foreach ($renderables as $renderable) {
$renderFunctions[] = new \Twig_SimpleFunction(strtolower($renderable),
function($label, $elementId) use ($renderable) {
return $this->renderChart($renderable, $label, $elementId);
function($label) use ($renderable) {
try {
$elementId = func_get_arg(1);

return $this->lava->render($renderable, $label, $elementId);
} catch (\Exception $e) {
return $this->lava->render($renderable, $label);
}
}
);
}

return $renderFunctions;
}

/**
* @param string $type
* @param string $label
* @param string $elementId
* @return string
*/
public function renderChart($type, $label, $elementId)
{
return $this->lava->render($type, $label, $elementId);
}

/**
* Returns the name of the Lavacharts Twig Extension
*
* @return string
*/
public function getName()
{
return self::NAME;
return 'lavacharts_twig_extension';
}
}

0 comments on commit 9919188

Please sign in to comment.