|
@@ -8,16 +8,16 @@ use TemplateBundle\Entity\Template;
|
|
|
|
|
|
class TemplateService
|
|
|
{
|
|
|
-
|
|
|
+
|
|
|
const TEMPLATE_PATH = '/tmp/flowdat/templates';
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* @var EntityManagerInterface
|
|
|
*/
|
|
|
private $entityManager;
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
- * @var EntityRepository
|
|
|
+ * @var EntityRepository
|
|
|
*/
|
|
|
private $templateRepository;
|
|
|
|
|
@@ -30,60 +30,78 @@ class TemplateService
|
|
|
$this->entityManager = $entityManager;
|
|
|
$this->templateRepository = $entityManager->getRepository('TemplateBundle:Template');
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* @param string $name
|
|
|
- *
|
|
|
+ *
|
|
|
* @return Template
|
|
|
*/
|
|
|
public function getTemplateByName($name)
|
|
|
{
|
|
|
return $this->templateRepository->findOneByName($name);
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* @param string $name
|
|
|
* @param array $params
|
|
|
* @param string $filename
|
|
|
- *
|
|
|
+ * @param string $engine
|
|
|
+ *
|
|
|
* @return string
|
|
|
*/
|
|
|
- public function renderTemplate($name, $params = array(), $filename = null)
|
|
|
+ public function renderTemplate($name, $params = array(), $filename = null, $engine = 'twig')
|
|
|
{
|
|
|
$content = null;
|
|
|
$template = $this->getTemplateByName($name);
|
|
|
if (!is_null($template)) {
|
|
|
- $content = $this->render($template, $params);
|
|
|
- var_dump($content);
|
|
|
+ $content = $this->render($template, $params, $engine);
|
|
|
+ var_dump($content);
|
|
|
$filename = $this->createFile($content, $filename);
|
|
|
- }else{
|
|
|
- throw new \Exception ("Template $name not found");
|
|
|
- }
|
|
|
-
|
|
|
+ } else {
|
|
|
+ throw new \Exception("Template {$name} not found");
|
|
|
+ }
|
|
|
+
|
|
|
return $filename;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* @param Template $template
|
|
|
* @param array $params
|
|
|
- *
|
|
|
+ * @param string $engine
|
|
|
+ *
|
|
|
* @return string
|
|
|
*/
|
|
|
- public function render($template, $params = array())
|
|
|
+ public function render($template, $params = array(), $engine = 'twig')
|
|
|
{
|
|
|
- $loader = new \Twig_Loader_Array(array(
|
|
|
- $template->getName() => $template->getContent()
|
|
|
- ));
|
|
|
- $twig = new \Twig_Environment($loader, array(
|
|
|
- 'cache' => false,
|
|
|
- ));
|
|
|
- return $twig->render($template->getName(), $params);
|
|
|
+ if ($engine == 'twig') {
|
|
|
+ $loader = new \Twig_Loader_Array(array(
|
|
|
+ $template->getName() => $template->getContent()
|
|
|
+ ));
|
|
|
+ $twig = new \Twig_Environment($loader, array(
|
|
|
+ 'cache' => false,
|
|
|
+ ));
|
|
|
+
|
|
|
+ $content = $twig->render($template->getName(), $params);
|
|
|
+ } elseif ($engine == 'dwoo') {
|
|
|
+ $core = new \Dwoo\Core();
|
|
|
+
|
|
|
+ $tpl = new \Dwoo\Template\Str($template->getContent());
|
|
|
+
|
|
|
+ $data = new \Dwoo\Data();
|
|
|
+ $data->setData($params);
|
|
|
+
|
|
|
+ $content = $core->get($tpl, $data);
|
|
|
+ } else {
|
|
|
+ throw new \Exception("Invalid {$engine} engine value");
|
|
|
+ }
|
|
|
+
|
|
|
+ return $content;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
/**
|
|
|
* @param string $content
|
|
|
* @param string $filename
|
|
|
- *
|
|
|
+ *
|
|
|
* @return string
|
|
|
*/
|
|
|
public function createFile($content, $filename = null)
|
|
@@ -101,5 +119,5 @@ class TemplateService
|
|
|
|
|
|
return $filename;
|
|
|
}
|
|
|
-
|
|
|
+
|
|
|
}
|