Skip to content
This repository was archived by the owner on Oct 20, 2019. It is now read-only.

Commit 8c7a6c6

Browse files
committed
Initial Commit
Ready to Use
0 parents  commit 8c7a6c6

5 files changed

Lines changed: 398 additions & 0 deletions

File tree

application/config/autoload.php

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
<?php
2+
defined('BASEPATH') OR exit('No direct script access allowed');
3+
4+
/*
5+
| -------------------------------------------------------------------
6+
| AUTO-LOADER
7+
| -------------------------------------------------------------------
8+
| This file specifies which systems should be loaded by default.
9+
|
10+
| In order to keep the framework as light-weight as possible only the
11+
| absolute minimal resources are loaded by default. For example,
12+
| the database is not connected to automatically since no assumption
13+
| is made regarding whether you intend to use it. This file lets
14+
| you globally define which systems you would like loaded with every
15+
| request.
16+
|
17+
| -------------------------------------------------------------------
18+
| Instructions
19+
| -------------------------------------------------------------------
20+
|
21+
| These are the things you can load automatically:
22+
|
23+
| 1. Packages
24+
| 2. Libraries
25+
| 3. Drivers
26+
| 4. Helper files
27+
| 5. Custom config files
28+
| 6. Language files
29+
| 7. Models
30+
|
31+
*/
32+
33+
/*
34+
| -------------------------------------------------------------------
35+
| Auto-load Packages
36+
| -------------------------------------------------------------------
37+
| Prototype:
38+
|
39+
| $autoload['packages'] = array(APPPATH.'third_party', '/usr/local/shared');
40+
|
41+
*/
42+
43+
$autoload['packages'] = array();
44+
45+
46+
/*
47+
| -------------------------------------------------------------------
48+
| Auto-load Libraries
49+
| -------------------------------------------------------------------
50+
| These are the classes located in the system/libraries folder
51+
| or in your application/libraries folder.
52+
|
53+
| Prototype:
54+
|
55+
| $autoload['libraries'] = array('database', 'email', 'session');
56+
|
57+
| You can also supply an alternative library name to be assigned
58+
| in the controller:
59+
|
60+
| $autoload['libraries'] = array('user_agent' => 'ua');
61+
*/
62+
63+
$autoload['libraries'] = array('Twig');
64+
65+
66+
/*
67+
| -------------------------------------------------------------------
68+
| Auto-load Drivers
69+
| -------------------------------------------------------------------
70+
| These classes are located in the system/libraries folder or in your
71+
| application/libraries folder within their own subdirectory. They
72+
| offer multiple interchangeable driver options.
73+
|
74+
| Prototype:
75+
|
76+
| $autoload['drivers'] = array('cache');
77+
*/
78+
79+
$autoload['drivers'] = array();
80+
81+
82+
/*
83+
| -------------------------------------------------------------------
84+
| Auto-load Helper Files
85+
| -------------------------------------------------------------------
86+
| Prototype:
87+
|
88+
| $autoload['helper'] = array('url', 'file');
89+
*/
90+
91+
$autoload['helper'] = array();
92+
93+
94+
/*
95+
| -------------------------------------------------------------------
96+
| Auto-load Config files
97+
| -------------------------------------------------------------------
98+
| Prototype:
99+
|
100+
| $autoload['config'] = array('config1', 'config2');
101+
|
102+
| NOTE: This item is intended for use ONLY if you have created custom
103+
| config files. Otherwise, leave it blank.
104+
|
105+
*/
106+
107+
$autoload['config'] = array('twig');
108+
109+
110+
/*
111+
| -------------------------------------------------------------------
112+
| Auto-load Language files
113+
| -------------------------------------------------------------------
114+
| Prototype:
115+
|
116+
| $autoload['language'] = array('lang1', 'lang2');
117+
|
118+
| NOTE: Do not include the "_lang" part of your file. For example
119+
| "codeigniter_lang.php" would be referenced as array('codeigniter');
120+
|
121+
*/
122+
123+
$autoload['language'] = array();
124+
125+
126+
/*
127+
| -------------------------------------------------------------------
128+
| Auto-load Models
129+
| -------------------------------------------------------------------
130+
| Prototype:
131+
|
132+
| $autoload['model'] = array('first_model', 'second_model');
133+
|
134+
| You can also supply an alternative model name to be assigned
135+
| in the controller:
136+
|
137+
| $autoload['model'] = array('first_model' => 'first');
138+
*/
139+
140+
$autoload['model'] = array();

application/config/twig.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
3+
/**
4+
* The following config items are for the integration of
5+
* the Twig template parsing system into CodeIgniter.
6+
*
7+
* @author Dimas Rullyan Danu <dimasdanz@gmail.com>
8+
* @link http://dimasdanz.com
9+
* @version 1.0.0
10+
* @license MIT
11+
*/
12+
13+
/**
14+
* --------------------------------------------------------------------------
15+
* Template Directory
16+
* --------------------------------------------------------------------------
17+
*
18+
* Set the directory where your templates (view files) are located.
19+
* Most users will not need to change this directory. It is pre-set
20+
* to CodeIgniter's default views directory.
21+
*
22+
* The default is a ternary op check to see if the CI 3.0+
23+
* constant VIEWPATH exists. You can customize this as you see fit.
24+
*
25+
* @var string
26+
* @since 1.0.0
27+
*/
28+
29+
$config['twig']['template_dir'] = VIEWPATH;
30+
31+
/**
32+
* --------------------------------------------------------------------------
33+
* File Extension
34+
* --------------------------------------------------------------------------
35+
*
36+
* Set the file extension of the templates (views) you're
37+
* using with the Twig template library.
38+
*
39+
* Common examples: php, html
40+
* Default: php
41+
*
42+
* @var string
43+
* @since 1.0.0
44+
*/
45+
$config['twig']['template_ext'] = 'php';
46+
47+
/**
48+
* --------------------------------------------------------------------------
49+
* Auto-escape Variables
50+
* --------------------------------------------------------------------------
51+
*
52+
* When TRUE, every variables parsed by Twig will be automatically escaped.
53+
* If you want to prevent escaping of a variable, use flag: {{ variable|raw }}
54+
*
55+
* Default: TRUE
56+
*
57+
* @var boolean
58+
* @since 1.0.0
59+
* @see http://twig.sensiolabs.org/doc/api.html
60+
*/
61+
$config['twig']['environment']['autoescape'] = TRUE;
62+
63+
/**
64+
* --------------------------------------------------------------------------
65+
* Cache Directory
66+
* --------------------------------------------------------------------------
67+
*
68+
* When set to TRUE all template loaders cache the compiled templates
69+
* on the filesystem for future reuse.
70+
* Default location is application/cache/twig/
71+
*
72+
* Or if you want to change the defaul location, you can opt to set a specific
73+
* path as well (e.g cache/twig)
74+
*
75+
* Default: FALSE;
76+
*
77+
* @var boolean|string
78+
* @since 1.0.0
79+
*/
80+
$config['twig']['environment']['cache'] = FALSE;
81+
82+
/**
83+
* --------------------------------------------------------------------------
84+
* Debug
85+
* --------------------------------------------------------------------------
86+
*
87+
* When set to TRUE, the generated templates have a __toString()
88+
* method that you can use to display the generated nodes.
89+
* escaped (via PHP's htmlspecialchars()). If you want to prevent
90+
* escaping of a variable, use the nofilter flag: {$variable nofilter}
91+
*
92+
* Default: FALSE
93+
*
94+
* @var boolean
95+
* @since 1.0.0
96+
* @see http://twig.sensiolabs.org/doc/api.html
97+
*/
98+
$config['twig']['environment']['debug'] = FALSE;
99+
100+
/* End of file twig.php */
101+
/* Location: ./application/config['twig']/twig/twig.php */

application/core/MY_Loader.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2+
3+
/**
4+
* MY_Loader class extends the core CI_Loader class for Twig.
5+
*
6+
* @author Dimas Rullyan Danu <dimasdanz@gmail.com>
7+
* @link http://dimasdanz.com
8+
* @version 1.0.0
9+
* @license MIT
10+
*/
11+
12+
class MY_Loader extends CI_Loader {
13+
14+
/**
15+
* Replace the default $this->load->view() method
16+
*
17+
* This method works identically to CI's default method,
18+
* in that you should pass parameters to it in the same way.
19+
*
20+
* @access public
21+
* @param string The template path name.
22+
* @param array An array of data to convert to variables.
23+
* @param bool Set to TRUE to return the loaded template as a string.
24+
* @return mixed If $return is TRUE, returns string. If not, returns void.
25+
*/
26+
public function view($template, $data = array(), $return = FALSE) {
27+
$CI =& get_instance();
28+
29+
try {
30+
$output = $CI->twig->render($template, $data);
31+
} catch (Exception $e) {
32+
show_error(htmlspecialchars_decode($e->getMessage()), 500, 'Twig Exception');
33+
}
34+
35+
// Return the output if the return value is TRUE.
36+
if ($return === TRUE) {
37+
return $output;
38+
}
39+
40+
// Otherwise append to output just like a view.
41+
$CI->output->append_output($output);
42+
}
43+
}

application/libraries/Twig.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2+
3+
/**
4+
* Twig template engine implementation for CodeIgniter
5+
*
6+
* Just a simple implementation of Twig template engine for CodeIgniter.
7+
*
8+
* @package CodeIgniter
9+
* @subpackage Twig
10+
* @category Libraries
11+
* @author Dimas Rullyan Danu <dimasdanz@gmail.com>
12+
* @license MIT
13+
* @version 1.0.0
14+
*/
15+
16+
class Twig {
17+
private $CI;
18+
19+
private $_config = array();
20+
private $_twig;
21+
private $_twig_loader;
22+
23+
/**
24+
* Constructor
25+
*/
26+
27+
public function __construct(){
28+
log_message('debug', 'Twig: library initialized');
29+
30+
$this->CI =& get_instance();
31+
32+
$this->_config = $this->CI->config->item('twig');
33+
34+
try {
35+
$this->_twig_loader = new Twig_Loader_Filesystem($this->_config['template_dir']);
36+
} catch (Exception $e) {
37+
show_error(htmlspecialchars_decode($e->getMessage()), 500, 'Twig Exception');
38+
}
39+
40+
//Check if cache is enabled
41+
if($this->_config['environment']['cache'] === true){
42+
$this->_config['environment']['cache'] = APPPATH.'cache/twig';
43+
}
44+
45+
$this->_twig = new Twig_Environment($this->_twig_loader, $this->_config['environment']);
46+
}
47+
48+
/**
49+
* Render
50+
* Return rendered string
51+
* @access public
52+
* @param string The template path name.
53+
* @param array An array of data to convert to variables.
54+
* @return string content of the template
55+
*/
56+
57+
public function render($template, $data = array()){
58+
$template = $this->addExtension($template);
59+
return $this->_twig->render($template, $data);
60+
}
61+
62+
/**
63+
* Display
64+
* Shortcut to output the template directly.
65+
* @access public
66+
* @param string The template path name.
67+
* @param array An array of data to convert to variables.
68+
* @return void echo content directly.
69+
*
70+
* I have no idea why I made this
71+
*/
72+
73+
public function display($template, $data = array()){
74+
$this->_twig->display($template, $data);
75+
}
76+
77+
/**
78+
* addExtension
79+
* Automatically add default extension if it's not there
80+
* @access public
81+
* @param string The template name.
82+
*/
83+
84+
private function addExtension($template){
85+
$ext = '.'.$this->_config['template_ext'];
86+
87+
if(substr($template, -strlen($ext)) !== $ext){
88+
return $template .= $ext;
89+
}
90+
91+
return $template;
92+
}
93+
}
94+
95+
/* End of file twig.php */
96+
/* Location: ./application/config/twig/twig.php */

0 commit comments

Comments
 (0)