Hvordan er en template engine som Smarty ude af proportion og en omvæltning i CI standarden?
Du kan ikke lave templates i CI - det er CI ikke til. :o)
CI har en View component. I trit med denne component kan du implementere en template engine, som f.eks. Smarty, Twig m.fl. Det er heller ikke din template engine der styrer hvor templates ligger, ej heller database halløj. Det gør dit model, eller service lag. Controlleren kommanderer View til at indlæse en template ud fra viden den får fra din model.
Hvis det skal være 100% rigtigt, skal du kunne flytte en template fra dit CI projekt til f.eks et ZF eller Symfony projekt, uden at ændre noget som helst.
Hvis du vil have inspiration til en render engine, må du gerne udnytte mit eget setup:
- <?php
- namespace Engine;
-
- /**
- * Render Engine
- *
- * This is a simple implementation of a PHTML parser. There's no cache implementation.
- * This engine uses output buffering.
- *
- * Instantiation example
- * ---------------------
- *
- * index.php:
- * $data = array(
- * 'page_title' => 'Index page',
- * 'page_content => 'Welcome to the index page'
- * );
- * $engine = new RenderEngine($view);
- * $output = $engine->render('/dir/to/theme/index.phtml', $data);
- *
- * index.phtml:
- * <html>
- * <body>
- * <h1><?php echo $page_title; ?></h1>
- * <p><?php echo $page_content; ?></p>
- * <?php $this->block('child.phtml'); ?>
- * </body>
- * </html>
- *
- * child.phtml:
- * <p>This is a child template.</p>
- */
- class RenderEngine {
- /**
- * View component
- *
- * @var View
- */
- private $view;
-
- /**
- * Path to ancestor file.
- * Used to figured out where child templates may be found.
- *
- * @var string
- */
- private $ancestor = null;
-
- /**
- * Constructor
- *
- * @param View $view
- */
- public function __construct(View $view) {
- $this->view = $view;
- }
-
- /**
- * Sets a master template / master page.
- *
- * @see RenderEngine::render() for more information.
- * @param string $template
- */
- public function setMaster($template) {
- throw new \Exception('Not implemented');
- }
-
- /**
- * Renders defined $template and passed in $data.
- *
- * The View component detects real path to template file.
- * When a master template set the engine renders this as the template,
- * and puts $template as a child template.
- *
- * @param string $template
- * @param array $data
- * @return string
- */
- public function render($template, array $data = array()) {
- $file = $this->view->getTemplate($template);
- $this->ancestor = $file;
- $this->data = $data;
- return $this->doRender($file, $data);
- }
-
- /**
- * Render a child template.
- *
- * The View component detects real path to template file.
- *
- * @param string $template
- * @param array $data
- */
- public function block($template, array $data = array()) {
- $file = $this->view->getTemplate($template, $this->ancestor);
- echo $this->doRender($file, array_merge($this->data, $data));
- }
-
- /**
- * The actual rendering process.
- *
- * @param string $file
- * @param array $data
- * @throws \Exception
- */
- public function doRender($file, array $data) {
- $output = null;
- $exception = null;
-
- if (!is_file($file)) {
- throw new \Exception('Could not load template ' . $file);
- }
-
- ob_start();
- try {
- extract($data);
- require($file);
- $output = ob_get_contents();
- } catch(\Exception $e) {
- $exception = $e;
- }
- ob_end_clean();
-
- if($exception) {
- throw new \Exception('Rendering template failed: '.$exception->getMessage(), $exception->getCode(), $exception);
- }
-
- return $output;
- }
-
- /**
- * Transforms special characters into HTML entities for safe display.
- *
- * @param string $content
- * @return string
- */
- protected function escape($content) {
- return htmlentities($content, ENT_QUOTES, 'UTF-8');
- }
-
- /**
- * Parses text to pretty display.
- *
- * @todo implement autop() and shorttags like wordpress.
- * @param string $text
- * @return string
- */
- protected function content($text) {
- $text = nl2br($text);
- return $text;
- }
- }
Indlæg senest redigeret d. 04.11.2014 23:30 af Bruger #10216