mirror of
https://github.com/Combodo/iTop.git
synced 2026-05-18 14:58:43 +02:00
N°2037 - Add Twig template rendering to the WebPage
This commit is contained in:
@@ -17,6 +17,8 @@
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
*/
|
||||
|
||||
use Combodo\iTop\Application\TwigBase\Twig\TwigHelper;
|
||||
|
||||
/**
|
||||
* Generic interface common to CLI and Web pages
|
||||
*/
|
||||
@@ -185,6 +187,21 @@ class WebPage implements Page
|
||||
$this->s_content .= $s_html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add any rendered text or HTML fragment to the body of the page using a twig template
|
||||
*
|
||||
* @param string $sViewPath Absolute path of the templates folder
|
||||
* @param string $sTemplateName Name of the twig template, ie MyTemplate for MyTemplate.html.twig
|
||||
* @param array $aParams Params used by the twig template
|
||||
* @param string $sDefaultType default type of the template ('html', 'xml', ...)
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public function add_twig_template($sViewPath, $sTemplateName, $aParams = array(), $sDefaultType = 'html')
|
||||
{
|
||||
TwigHelper::RenderIntoPage($this, $sViewPath, $sTemplateName, $aParams, $sDefaultType);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add any text or HTML fragment (identified by an ID) at the end of the body of the page
|
||||
* This is useful to add hidden content, DIVs or FORMs that should not
|
||||
|
||||
@@ -6,9 +6,12 @@
|
||||
|
||||
namespace Combodo\iTop\Application\TwigBase\Twig;
|
||||
|
||||
use IssueLog;
|
||||
use Twig\Environment;
|
||||
use Twig_Environment;
|
||||
use Twig_Error;
|
||||
use Twig_Loader_Filesystem;
|
||||
use utils;
|
||||
use WebPage;
|
||||
|
||||
|
||||
@@ -27,28 +30,30 @@ class TwigHelper
|
||||
* 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 \WebPage $oPage
|
||||
* @param string $sViewPath Absolute path of the templates folder
|
||||
* @param string $sTemplateName Name of the twig template, ie MyTemplate for MyTemplate.html.twig
|
||||
* @param array $aParams Params used by the twig template
|
||||
* @param string $sDefaultType default type of the template ('html', 'xml', ...)
|
||||
*
|
||||
* @throws \Exception
|
||||
* @api
|
||||
*/
|
||||
public static function RenderIntoPage(WebPage $oPage, $sViewPath, $sTemplateName, $aParams = array())
|
||||
public static function RenderIntoPage(WebPage $oPage, $sViewPath, $sTemplateName, $aParams = array(), $sDefaultType = 'html')
|
||||
{
|
||||
$oTwig = self::GetTwigEnvironment($sViewPath);
|
||||
$oPage->add(self::RenderTemplate($oTwig, $aParams, $sTemplateName, 'html'));
|
||||
$oPage->add(self::RenderTemplate($oTwig, $aParams, $sTemplateName, $sDefaultType));
|
||||
$oPage->add_script(self::RenderTemplate($oTwig, $aParams, $sTemplateName, 'js'));
|
||||
$oPage->add_ready_script(self::RenderTemplate($oTwig, $aParams, $sTemplateName, 'ready.js'));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Twig\Environment $oTwig
|
||||
* @param $aParams
|
||||
* @param $sName
|
||||
* @param $sTemplateFileExtension
|
||||
*
|
||||
* @return string
|
||||
* @throws \Exception
|
||||
*/
|
||||
private static function RenderTemplate(Environment $oTwig, $aParams, $sName, $sTemplateFileExtension)
|
||||
{
|
||||
@@ -58,11 +63,14 @@ class TwigHelper
|
||||
}
|
||||
catch (Twig_Error $e)
|
||||
{
|
||||
// Ignore errors
|
||||
if (!utils::StartsWith($e->getMessage(), 'Unable to find template'))
|
||||
{
|
||||
IssueLog::Error($e->getMessage());
|
||||
}
|
||||
else
|
||||
{
|
||||
IssueLog::Debug($e->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
return '';
|
||||
|
||||
Reference in New Issue
Block a user