Permalink
Browse files

Super simple sfTwigView which to my delight supports symfonys two ste…

…p rendering
  • Loading branch information...
1 parent f1ded5a commit a0219bcacae72784cd616da1f1e8147c6e8830d0 Henrik Bjørnskov committed Oct 10, 2009
Showing with 80 additions and 0 deletions.
  1. +5 −0 config/autoload.yml
  2. +11 −0 config/settings.yml
  3. +5 −0 lib/view/sfTwigPartialView.class.php
  4. +59 −0 lib/view/sfTwigView.class.php
View
@@ -0,0 +1,5 @@
+autoload:
+ Twig:
+ name: Twig classes
+ ext: .php
+ path: <?php echo realpath(dirname(__FILE__) . '/../lib') . "\n"; ?>
View
@@ -0,0 +1,11 @@
+prod:
+ .settings:
+ twig_template_cache: true
+
+dev:
+ .settings:
+ twig_template_cache: false
+
+test:
+ .settings:
+ twig_template_cache: false
@@ -0,0 +1,5 @@
+<?php
+
+class sfTwigPartialView extends sfTwigView
+{
+}
@@ -0,0 +1,59 @@
+<?php
+/*
+ * This file is part of the symfony package.
+ * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
+ * (c) 2004-2006 Sean Kerr <sean@code-box.org>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+
+/**
+ * A view that uses PHP as the templating engine.
+ *
+ * @package symfony
+ * @subpackage sfTwigPlugin
+ * @author Henrik Bjornskov <yep@iamhenrik.se>
+ */
+class sfTwigView extends sfPHPView
+{
+ protected
+ $twig = null,
+ $extension = '.html';
+
+ /**
+ * Sets up a Twig_Environment
+ *
+ * @return void
+ */
+ public function execute()
+ {
+ $loader = new Twig_Loader_Filesystem('/', sfConfig::get('sf_app_cache_dir') . '/twig', sfConfig::get('sf_twig_template_cache', false));
+ $this->twig = new Twig_Environment($loader);
+ }
+
+ /**
+ * Returns the initiated Twig_Environment object
+ *
+ * @return Twig_Environment
+ */
+ public function getEngine()
+ {
+ return $this->twig;
+ }
+
+ /**
+ * Renders a Twig_Template
+ *
+ * @oaram $_sfFile string fullpath to a template file
+ * @return string the rendered file
+ */
+ protected function renderFile($_sfFile)
+ {
+ $this->loadCoreAndStandardHelpers();
+
+ $template = $this->twig->loadTemplate(substr($_sfFile, 1));
+ return $template->render($this->attributeHolder->toArray());
+ }
+}

0 comments on commit a0219bc

Please sign in to comment.