Permalink
Browse files

Added Twig as a submodule

  • Loading branch information...
1 parent 04f9fcb commit 33129fdd18fbfb61691d753e5e931607f91a6467 @bjornbjorn committed Aug 25, 2011
View
@@ -0,0 +1,3 @@
+[submodule "application/libraries/Twig"]
+ path = application/libraries/Twig
+ url = git://github.com/fabpot/Twig.git
@@ -52,7 +52,7 @@
| $autoload['libraries'] = array('database', 'session', 'xmlrpc');
*/
-$autoload['libraries'] = array();
+$autoload['libraries'] = array('twig');
/*
@@ -0,0 +1,4 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+
+$config['template_dir'] = APPPATH.'views';
+$config['cache_dir'] = APPPATH.'cache/twig';
@@ -19,7 +19,7 @@ class Welcome extends CI_Controller {
*/
public function index()
{
- $this->load->view('welcome_message');
+ $this->twig->display('welcome_message.twig');
}
}
Submodule Twig added at 517c23
@@ -0,0 +1,73 @@
+<?php if (!defined('BASEPATH')) {exit('No direct script access allowed');}
+
+class Twig
+{
+ private $CI;
+ private $_twig;
+ private $_template_dir;
+ private $_cache_dir;
+
+ /**
+ * Constructor
+ *
+ */
+ function __construct($debug = false)
+ {
+ $this->CI =& get_instance();
+ $this->CI->config->load('twig');
+
+ ini_set('include_path',
+ ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'libraries/Twig/lib/Twig/');
+
+ require_once (string) "Autoloader" . EXT;
+
+ log_message('debug', "Twig Autoloader Loaded");
+
+ Twig_Autoloader::register();
+
+ $this->_template_dir = $this->CI->config->item('template_dir');
+ $this->_cache_dir = $this->CI->config->item('cache_dir');
+
+ $loader = new Twig_Loader_Filesystem($this->_template_dir);
+
+ $this->_twig = new Twig_Environment($loader, array(
+ 'cache' => $this->_cache_dir,
+ 'debug' => $debug,
+ ));
+ }
+
+ public function add_global($name, $obj)
+ {
+ $this->_twig->addGlobal($name, $obj);
+ }
+
+ public function add_function($name, $is_safe=FALSE)
+ {
+ if($is_safe)
+ {
+ $this->_twig->addFunction($name, new Twig_Function_Function($name, array('is_safe' => array('html'))));
+ }
+
+ else
+ {
+ $this->_twig->addFunction($name, new Twig_Function_Function($name));
+ }
+
+ }
+
+ public function render($template, $data = array())
+ {
+ $template = $this->_twig->loadTemplate($template);
+ return $template->render($data);
+ }
+
+ public function display($template, $data = array())
+ {
+ $template = $this->_twig->loadTemplate($template);
+ /* elapsed_time and memory_usage */
+ $data['elapsed_time'] = $this->CI->benchmark->elapsed_time('total_execution_time_start', 'total_execution_time_end');
+ $memory = (!function_exists('memory_get_usage')) ? '0' : round(memory_get_usage()/1024/1024, 2) . 'MB';
+ $data['memory_usage'] = $memory;
+ $template->display($data);
+ }
+}
@@ -0,0 +1,77 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+ <meta charset="utf-8">
+ <title>Welcome to CodeIgniter</title>
+
+ <style type="text/css">
+
+ ::selection{ background-color: #E13300; color: white; }
+ ::moz-selection{ background-color: #E13300; color: white; }
+ ::webkit-selection{ background-color: #E13300; color: white; }
+
+ body {
+ background-color: #fff;
+ margin: 40px;
+ font: 13px/20px normal Helvetica, Arial, sans-serif;
+ color: #4F5155;
+ }
+
+ a {
+ color: #003399;
+ background-color: transparent;
+ font-weight: normal;
+ }
+
+ h1 {
+ color: #444;
+ background-color: transparent;
+ border-bottom: 1px solid #D0D0D0;
+ font-size: 19px;
+ font-weight: normal;
+ margin: 0 0 14px 0;
+ padding: 14px 15px 10px 15px;
+ }
+
+ code {
+ font-family: Consolas, Monaco, Courier New, Courier, monospace;
+ font-size: 12px;
+ background-color: #f9f9f9;
+ border: 1px solid #D0D0D0;
+ color: #002166;
+ display: block;
+ margin: 14px 0 14px 0;
+ padding: 12px 10px 12px 10px;
+ }
+
+ #body{
+ margin: 0 15px 0 15px;
+ }
+
+ p.footer{
+ text-align: right;
+ font-size: 11px;
+ border-top: 1px solid #D0D0D0;
+ line-height: 32px;
+ padding: 0 10px 0 10px;
+ margin: 20px 0 0 0;
+ }
+
+ #container{
+ margin: 10px;
+ border: 1px solid #D0D0D0;
+ -webkit-box-shadow: 0 0 8px #D0D0D0;
+ }
+ </style>
+</head>
+<body>
+
+<div id="container">
+
+ {% block page_content %}{% endblock %}
+
+ <p class="footer">CI+Twig</p>
+</div>
+
+</body>
+</html>
@@ -0,0 +1,19 @@
+{% extends "base.twig" %}
+
+{% block page_content %}
+
+<h1>Welcome to CodeIgniter!</h1>
+
+<div id="body">
+ <p>The page you are looking at is being generated dynamically by CodeIgniter.</p>
+
+ <p>If you would like to edit this page you'll find it located at:</p>
+ <code>application/views/welcome_message.twig</code>
+
+ <p>The corresponding controller for this page is found at:</p>
+ <code>application/controllers/welcome.php</code>
+
+ <p>If you are exploring CodeIgniter for the very first time, you should start by reading the <a href="user_guide/">User Guide</a>.</p>
+</div>
+
+{% endblock %}

0 comments on commit 33129fd

Please sign in to comment.