N°2037 - New dashlet Gantt - add method to insert twig in an existing page

This commit is contained in:
acognet
2020-02-21 12:08:33 +01:00
parent 19809249a2
commit 4cc8b89f4e

View File

@@ -6,8 +6,10 @@
namespace Combodo\iTop\Application\TwigBase\Twig;
use Twig\Environment;
use Twig_Environment;
use Twig_Loader_Filesystem;
use WebPage;
class TwigHelper
@@ -20,4 +22,49 @@ class TwigHelper
return $oTwig;
}
/**
* Display the twig page based on the name or the operation onto the page specified with SetPage().
* Use this method if you have to insert HTML into an existing page.
*
* @api
*
* @param array $aParams Params used by the twig template
* @param string $sTemplateName Name of the twig template, ie MyTemplate for MyTemplate.html.twig
*
* @throws \Exception
*/
public static function RenderIntoPage(WebPage $oPage, $sViewPath, $sTemplateName, $aParams = array())
{
$oTwig = self::GetTwigEnvironment($sViewPath);
$oPage->add(self::RenderTemplate($oTwig, $aParams, $sTemplateName, 'html'));
$oPage->add_script(self::RenderTemplate($oTwig, $aParams, $sTemplateName, 'js'));
$oPage->add_ready_script(self::RenderTemplate($oTwig, $aParams, $sTemplateName, 'ready.js'));
}
/**
* @param $aParams
* @param $sName
* @param $sTemplateFileExtension
*
* @return string
* @throws \Exception
*/
private static function RenderTemplate(Environment $oTwig, $aParams, $sName, $sTemplateFileExtension)
{
try
{
return $oTwig->render($sName.'.'.$sTemplateFileExtension.'.twig', $aParams);
}
catch (Twig_Error $e)
{
// Ignore errors
if (!utils::StartsWith($e->getMessage(), 'Unable to find template'))
{
IssueLog::Error($e->getMessage());
}
}
return '';
}
}