Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
  • 2 commits
  • 10 files changed
  • 0 commit comments
  • 1 contributor
View
3  classes/controller/kotwig.php
@@ -0,0 +1,3 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+abstract class Controller_Kotwig extends Kohana_Controller_Kotwig {}
View
62 classes/controller/twig.php
@@ -1,62 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-/**
- * Twig template controller
- *
- * @package Kohana-Twig
- * @author John Heathco <jheathco@gmail.com>
- */
-abstract class Controller_Twig extends Controller {
-
- /**
- * @var Twig_Environment
- */
- public $twig;
-
- /**
- * @var boolean Auto-render template after controller method returns
- */
- public $auto_render = TRUE;
-
- /**
- * @var array|object Stores mapping of template vars => values
- */
- public $context;
-
- public function __construct(Request $request, Response $response)
- {
- // Setup the Twig loader environment
- $this->twig = Kohana_Twig::instance();
-
- if (Kohana_Twig::$config->context_object)
- {
- // Context treated as an object
- $this->context = new stdClass;
- }
- else
- {
- // Context treated as an array
- $this->context = array();
- }
-
- // Auto-generate template filename ('index' method called on Controller_Admin_Users looks for 'admin/users/index')
- $this->template = $request->controller().'/'.$request->action().Kohana_Twig::$config->suffix;
-
- if ($request->directory())
- {
- // Preprend directory if needed
- $this->template = $request->directory().'/'.$this->template;
- }
-
- parent::__construct($request, $response);
- }
-
- public function after()
- {
- if ($this->auto_render)
- {
- // Auto-render the template
- $this->response->body($this->twig->loadTemplate($this->template)->render((array) $this->context));
- }
- }
-
-} // End Controller_Twig
View
49 classes/kohana/controller/kotwig.php
@@ -0,0 +1,49 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+/**
+ * Twig template controller
+ *
+ * @package Kotwig
+ * @author John Heathco <jheathco@gmail.com>
+ */
+abstract class Kohana_Controller_Kotwig extends Controller {
+
+ /**
+ * @var boolean Auto-render template after controller method returns
+ */
+ public $auto_render = TRUE;
+
+ /**
+ * @var Kotwig_View Kohana twig template
+ */
+ public $template = NULL;
+
+ public function before()
+ {
+ if ($this->auto_render)
+ {
+ // Auto-generate template filename ('index' method called on Controller_Admin_Users looks for 'admin/users/index')
+ if ($this->template === NULL)
+ {
+ $this->template = $this->request->controller().'/'.$this->request->action();
+
+ if ($this->request->directory())
+ {
+ // Preprend directory if needed
+ $this->template = $this->request->directory().'/'.$this->template;
+ }
+ }
+
+ $this->template = Kotwig_View::factory($this->template);
+ }
+ }
+
+ public function after()
+ {
+ if ($this->auto_render)
+ {
+ // Auto-render the template
+ $this->response->body($this->template->render());
+ }
+ }
+
+} // End Controller_Kotwig
View
55 classes/kohana/kotwig.php
@@ -0,0 +1,55 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+/**
+ * Twig loader.
+ *
+ * @package Kotwig
+ * @author John Heathco <jheathco@gmail.com>
+ */
+class Kohana_Kotwig {
+
+ /**
+ * @var object Kotwig instance
+ */
+ public static $instance;
+
+ /**
+ * @var Twig_Environment
+ */
+ public $twig;
+
+ /**
+ * @var object Kotwig configuration (Kohana_Config object)
+ */
+ public $config;
+
+ public static function instance()
+ {
+ if ( ! Kotwig::$instance)
+ {
+ Kotwig::$instance = new Kotwig;
+
+ // Load Twig configuration
+ Kotwig::$instance->config = Kohana::config('kotwig');
+
+ // Create the the loader
+ $loader = new Twig_Loader_Filesystem(Kotwig::$instance->config->templates);
+
+ // Set up Twig
+ Kotwig::$instance->twig = new Twig_Environment($loader, Kotwig::$instance->config->environment);
+
+ foreach (Kotwig::$instance->config->extensions as $extension)
+ {
+ // Load extensions
+ Kotwig::$instance->twig->addExtension(new $extension);
+ }
+ }
+
+ return Kotwig::$instance;
+ }
+
+ final private function __construct()
+ {
+ // This is a singleton class
+ }
+
+} // End Kotwig
View
54 classes/kohana/kotwig/view.php
@@ -0,0 +1,54 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+/**
+ * Twig view.
+ *
+ * @package Kotwig
+ * @author John Heathco <jheathco@gmail.com>
+ */
+class Kohana_Kotwig_View extends View {
+
+ public static function factory($file = NULL, array $data = NULL)
+ {
+ return new Kotwig_View($file, $data);
+ }
+
+ protected static function capture($kohana_view_filename, array $kohana_view_data)
+ {
+ return Kotwig::instance()
+ ->twig
+ ->loadTemplate($kohana_view_filename)
+ ->render(array_merge($kohana_view_data, View::$_global_data));
+ }
+
+ public function set_filename($file)
+ {
+ $ext = Kotwig::instance()->config['suffix'];
+
+ if ($ext === NULL)
+ {
+ $this->_file = $file;
+ }
+ else
+ {
+ $this->_file = $file.'.'.$ext;
+ }
+
+ return $this;
+ }
+
+ public function render($file = NULL)
+ {
+ if ($file !== NULL)
+ {
+ $this->set_filename($file);
+ }
+
+ if (empty($this->_file))
+ {
+ throw new Kohana_View_Exception('You must set the file to use within your view before rendering');
+ }
+
+ // Combine local and global data and capture the output
+ return Kotwig_View::capture($this->_file, $this->_data);
+ }
+}
View
48 classes/kohana/twig.php
@@ -1,48 +0,0 @@
-<?php defined('SYSPATH') or die('No direct script access.');
-/**
- * Twig loader.
- *
- * @package Kohana-Twig
- * @author John Heathco <jheathco@gmail.com>
- */
-class Kohana_Twig {
-
- /**
- * @var object Kohana_Twig instance
- */
- public static $instance;
-
- /**
- * @var object Kohana_Twig configuration (Kohana_Config object)
- */
- public static $config;
-
- public static function instance()
- {
- if ( ! Kohana_Twig::$instance)
- {
- // Load Twig configuration
- Kohana_Twig::$config = Kohana::config('twig');
-
- // Create the the loader
- $loader = new Twig_Loader_Filesystem(Kohana_Twig::$config->templates);
-
- // Set up Twig
- Kohana_Twig::$instance = new Twig_Environment($loader, Kohana_Twig::$config->environment);
-
- foreach (Kohana_Twig::$config->extensions as $extension)
- {
- // Load extensions
- Kohana_Twig::$instance->addExtension(new $extension);
- }
- }
-
- return Kohana_Twig::$instance;
- }
-
- final private function __construct()
- {
- // This is a static class
- }
-
-} // End Kohana_Twig
View
3  classes/kotwig.php
@@ -0,0 +1,3 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Kotwig extends Kohana_Kotwig {}
View
3  classes/kotwig/view.php
@@ -0,0 +1,3 @@
+<?php defined('SYSPATH') or die('No direct script access.');
+
+class Kotwig_View extends Kohana_Kotwig_View {}
View
5 config/twig.php → config/kotwig.php
@@ -18,7 +18,6 @@
(
// List extension class names
),
- 'templates' => APPPATH.'views/twig',
- 'suffix' => '.html',
- 'context_object' => TRUE,
+ 'templates' => APPPATH.'views',
+ 'suffix' => 'twig',
);
2  vendor/Twig
@@ -1 +1 @@
-Subproject commit c7307ca6b2bf9236b60bd6a56eff46601c07df3b
+Subproject commit 325f1997eafb49954333aad840b1e46feab81f21

No commit comments for this range

Something went wrong with that request. Please try again.