├── views ├── footer.php ├── index.php ├── footer_javascript.php └── header.php ├── controllers └── Example.php ├── README.md └── libraries └── layout.php /views/footer.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | -------------------------------------------------------------------------------- /views/index.php: -------------------------------------------------------------------------------- 1 |
2 |

This is just a test

3 |
4 | -------------------------------------------------------------------------------- /views/footer_javascript.php: -------------------------------------------------------------------------------- 1 | document.write("

Hello World!

"); 2 | document.write("

Just put javascript here, the script HTML tag will be added automatically.

"); 3 | -------------------------------------------------------------------------------- /views/header.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | <?= CI_title() ?> 6 | 7 | 8 | > 9 | -------------------------------------------------------------------------------- /controllers/Example.php: -------------------------------------------------------------------------------- 1 | load->library(array('session', 'layout')); 16 | $this->load->helper(array('url')); 17 | 18 | $this->layout->add_custom_meta('meta', array( 19 | 'charset' => 'utf-8' 20 | )); 21 | 22 | $this->layout->add_custom_meta('meta', array( 23 | 'http-equiv' => 'X-UA-Compatible', 24 | 'content' => 'IE=edge' 25 | )); 26 | 27 | $this->layout->add_css_files(array('bootstrap.min.css','style.css'), base_url().'assets/css/'); 28 | 29 | $css_text = <<load->view('footer_javascript', '', true); 42 | 43 | $this->layout->add_css_rawtext($css_text); 44 | $this->layout->add_js_rawtext($js_text); 45 | $this->layout->add_js_rawtext($js_text_footer, 'footer'); 46 | 47 | } 48 | 49 | /** 50 | * index function. 51 | * 52 | * @access public 53 | * @param mixed $slug (default: false) 54 | * @return void 55 | */ 56 | public function index() 57 | { 58 | $this->layout->set_title('Test! This is test title'); 59 | $this->layout->set_body_attr(array('id' => 'home', 'class' => 'test more_class')); 60 | 61 | // load views and send data 62 | $this->load->view('header', $data); 63 | $this->load->view('index', $data); 64 | $this->load->view('footer', $data); 65 | } 66 | } 67 | 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | # Simple Template Library for Codeigniter 3 | 4 | The simplest template libary for Codeigniter ever. The code is easy to understand and modify it for your needs. 5 | 6 | ### Installation 7 | 8 | Download library.php to your library folder. 9 | 10 | ```php 11 | $this->load->library('layout'); 12 | ``` 13 | Put **CI_head()** inside **<head>** and **</head>** tag in view. 14 | 15 | Put **CI_footer()** before **</body>** tag in view. 16 | 17 | Put **CI_title()** inside **<title>** and **</title>** tag in view. 18 | 19 | Put **CI_body_attr()** into **<body>** to control attrbutes such as "id", "class" and more. 20 | 21 | ```html 22 | 23 | 24 | 25 | <?= CI_title() ?> 26 | 27 | 28 | > 29 | ``` 30 | 31 | ```html 32 | 33 | 34 | 35 | ``` 36 | 37 | All HTML output are handled by CI_head() , CI_footer(), CI_body_attr() and CI_title() 38 | 39 | 40 | ### API Examples 41 | Use the following APIs in controller. 42 | 43 | 44 | ####add_meta 45 | 46 | Parameters 47 | ```php 48 | /* 49 | * add_meta (string $name, string $value, string $type = 'meta') 50 | * @param string $name 51 | * @param string $value 52 | * @param string $type - 'meta' or 'link'. Default: 'meta' 53 | */ 54 | ``` 55 | Example 56 | ```php 57 | $this->layout->add_meta('author', 'Terry Lin'); 58 | ``` 59 | Output 60 | ```html 61 | 62 | ``` 63 | 64 | ####add_custom_meta 65 | 66 | Parameters 67 | ```php 68 | /* 69 | * add_custom_meta(string $type, array $attributes) 70 | * @param string $type 71 | * @param string array $attributes 72 | */ 73 | ``` 74 | Example 75 | ```php 76 | $this->layout->add_custom_meta('link', array( 77 | 'href' => 'test.php', 78 | 'rel' => 'parent', 79 | 'rev' => 'subsection', 80 | 'hreflang' => 'en' 81 | )); 82 | ``` 83 | Output 84 | ```html 85 | 86 | ``` 87 | Build special meta tags: 88 | ```php 89 | $this->layout->add_custom_meta('meta', array( 90 | 'charset' => 'utf-8' 91 | )); 92 | $this->layout->add_custom_meta('meta', array( 93 | 'http-equiv' => 'X-UA-Compatible', 94 | 'content' => 'IE=edge' 95 | )); 96 | ``` 97 | Output 98 | ```html 99 | 100 | 101 | ``` 102 | 103 | ####add_css_file 104 | 105 | Parameters 106 | ```php 107 | /* 108 | * add_css_file ( string $tag_css, string $path = '' ) 109 | * @param string $tag_css 110 | * @param string $path - $path can be ignored if you set $tag_css as a remote file with full URL, or a local file with absolute path. 111 | */ 112 | ``` 113 | Example 114 | ```php 115 | $this->layout->add_css_file('https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css'); 116 | ``` 117 | Output 118 | ```html 119 | 120 | ``` 121 | 122 | ####add_css_files 123 | 124 | Parameters 125 | ```php 126 | /* 127 | * add_css_files ( array $tag_css, string $path = '') 128 | * @param array $tag_css 129 | * @param string $path - $path can be ignored if you set $tag_css as remote files with full URL, or local files with absolute path. 130 | */ 131 | ``` 132 | Example 133 | ```php 134 | $this->layout->add_css_files(array('bootstrap.min.css','style.css'), base_url().'assets/css/'); 135 | ``` 136 | Output 137 | ```html 138 | 139 | 140 | ``` 141 | 142 | ####add_css_rawtext 143 | 144 | Parameter 145 | ```php 146 | /* 147 | * add_css_rawtext ( string $content ) 148 | * @param string $content 149 | */ 150 | ``` 151 | Example 152 | ```php 153 | $css_text = <<layout->add_css_rawtext($css_text); 162 | ``` 163 | Output 164 | ```html 165 | 174 | ``` 175 | 176 | ####add_js_file 177 | 178 | Parameters 179 | ```php 180 | /* 181 | * add_js_file ( string $tag_js , string $path = '', string $position = 'header' ) 182 | * @param string $tag_js 183 | * @param string $path - $path can be ignored if you set $tag_js as a remote file with full URL, or a local file with absolute path. 184 | * @param string $position - 'header' or 'footer' 185 | */ 186 | ``` 187 | Example 188 | ```php 189 | $this->layout->add_js_file('https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js'); 190 | ``` 191 | Output 192 | ```html 193 | 194 | ``` 195 | 196 | ####add_js_files 197 | 198 | Parameters 199 | ```php 200 | /* 201 | * add_js_files ( array $tag_js , string $path = '', string $position = 'header' ) 202 | * @param array $tag_js 203 | * @param string $path - $path can be ignored if you set $tag_js as remote files with full URL, or local files with absolute path. 204 | * @param string $position - 'header' or 'footer'. Default: 'header'. 205 | */ 206 | ``` 207 | Example 208 | ```php 209 | $this->layout->add_js_files(array('bootstrap.min.js','script'), base_url().'assets/js/'); 210 | ``` 211 | Output 212 | ```html 213 | 214 | 215 | ``` 216 | 217 | ####add_js_rawtext 218 | 219 | Parameters 220 | ```php 221 | /* 222 | * add_js_rawtext ( string $content , string $position = 'header') 223 | * @param string $content 224 | * @param string $position - 'header' or 'footer'. Default: 'header'. 225 | */ 226 | ``` 227 | Example 228 | ```php 229 | $js_text = <<layout->add_js_rawtext($js_text, 'header'); 234 | ``` 235 | Output 236 | ```html 237 | 242 | ``` 243 | 244 | ####set_title 245 | 246 | Parameter 247 | ```php 248 | /* 249 | * set_title ( string $title ) 250 | * @param string $title 251 | */ 252 | ``` 253 | Example 254 | ```php 255 | $this->layout->set_title('Test! This is a test title'); 256 | ``` 257 | Output 258 | ```html 259 | Test! This is a test title 260 | ``` 261 | ####set_body_attr 262 | 263 | Parameter 264 | ```php 265 | /* 266 | * set_body_attr ( array $attribute ) 267 | * @param array $attribute 268 | */ 269 | ``` 270 | Example 271 | ```php 272 | $this->layout->set_body_attr(array('id' => 'home', 'class' => 'global white-bg')); 273 | ``` 274 | Output 275 | ```html 276 | 277 | ``` 278 | 279 | ####get_title 280 | alias => CI_title() 281 | ```php 282 | $this->layout->get_title(); 283 | ``` 284 | 285 | ####get_body_attr 286 | alias => CI_body_attr() 287 | ```php 288 | $this->layout->get_body_attr(); 289 | ``` 290 | 291 | ####get_header 292 | alias => CI_head() 293 | ```php 294 | $this->layout->get_header(); 295 | ``` 296 | 297 | ####get_footer 298 | alias => CI_footer() 299 | ```php 300 | $this->layout->get_footer(); 301 | ``` 302 | 303 | 304 | ### Built-in Meta Tags 305 | 306 | #### $meta_default 307 | author, viewport, keywords, description, canonical 308 | 309 | ```php 310 | // examples 311 | $this->layout->meta_default['author'] = 'Terry Lin'; 312 | $this->layout->meta_default['description'] = 'This is just a test file'; 313 | ``` 314 | 315 | #### $meta_twitter 316 | url, site, creator, card, title, description, image_src 317 | 318 | ```php 319 | // examples 320 | $this->layout->meta_twitter['card'] = 'summary_large_image'; 321 | $this->layout->meta_twitter['image_src'] = 'http://test.test/test.gif'; 322 | ``` 323 | 324 | #### $meta_facebook 325 | site_name, url, title, type, description, image, admins, app_id 326 | 327 | ```php 328 | // examples 329 | $this->layout->meta_facebook['site_name'] = 'Demo Site'; 330 | $this->layout->meta_facebook['description'] = 'This is just a test file'; 331 | $this->layout->meta_facebook['app_id'] = '1123123152'; 332 | ``` 333 | -------------------------------------------------------------------------------- /libraries/layout.php: -------------------------------------------------------------------------------- 1 | '', 44 | 'viewport' => 'width=device-width, initial-scale=1.0', 45 | 'keywords' => '', 46 | 'description' => '', 47 | 'canonical' => '' 48 | ); 49 | 50 | public $meta_twitter = array( 51 | 'url' => '', 52 | 'site' => '', 53 | 'creator' => '', 54 | 'card' => '', // summary_large_image 55 | 'title' => '', 56 | 'description' => '', 57 | 'image_src' => '' 58 | ); 59 | 60 | public $meta_facebook = array( 61 | 'site_name' => '', 62 | 'url' => '', 63 | 'title' => '', 64 | 'type' => '', 65 | 'description' => '', 66 | 'image' => '', 67 | 'admins' => '', 68 | 'app_id' => '' 69 | ); 70 | 71 | public static $instance; 72 | 73 | public function __construct() 74 | { 75 | self::$instance = null; 76 | } 77 | 78 | /** 79 | * Instance() 80 | * Singleton pattern 81 | * The instance is made for easy use the function call. 82 | */ 83 | 84 | public static function instance() 85 | { 86 | if ( !self::$instance ) 87 | { 88 | self::$instance = new layout(); 89 | } 90 | return self::$instance; 91 | } 92 | /** 93 | * Add a meta tag 94 | * 95 | * @param string $name 96 | * @param string $value 97 | * @param string $type 98 | */ 99 | 100 | public function add_meta($name, $value, $type = 'meta') 101 | { 102 | switch ($type) 103 | { 104 | case 'meta': 105 | self::instance()->tag_meta .= self::instance()->pre_space . '' . "\n"; 106 | break; 107 | case 'link': 108 | self::instance()->tag_meta .= self::instance()->pre_space . '' . "\n"; 109 | break; 110 | } 111 | } 112 | 113 | /** 114 | * Add a custom meta tag 115 | * 116 | * @param string $type 117 | * @param array $attribute 118 | */ 119 | 120 | public function add_custom_meta($type, $attribute = array()) 121 | { 122 | $attr_array = array(); 123 | 124 | foreach ($attribute AS $key => $value) 125 | { 126 | $attr_array[] = $key . '="' . $value . '"'; 127 | } 128 | $attr_string = implode(' ', $attr_array); 129 | 130 | self::instance()->tag_meta .= self::instance()->pre_space . '<' . $type . ' ' . $attr_string . ' />' . "\n"; 131 | } 132 | 133 | /** 134 | * Add single css file 135 | * 136 | * @param string $tag_css 137 | */ 138 | 139 | public function add_css_file($tag_css, $path = '') 140 | { 141 | self::instance()->tag_css .= self::instance()->pre_space . '' . "\n"; 142 | } 143 | 144 | /** 145 | * Add multiple css files at once 146 | * 147 | * @param array $tag_css 148 | */ 149 | 150 | public function add_css_files($tag_css = array(), $path = '') 151 | { 152 | if (is_array($tag_css)) 153 | { 154 | foreach ($tag_css AS $value) 155 | { 156 | self::instance()->tag_css .= self::instance()->pre_space . '' . "\n"; 157 | } 158 | } 159 | } 160 | 161 | /** 162 | * Add raw text to css section in header 163 | * 164 | * @param string $content 165 | */ 166 | 167 | public function add_css_rawtext($content) 168 | { 169 | self::instance()->rawtext_css .= "\n" . $content . "\n"; 170 | } 171 | /** 172 | * Add single js file 173 | * 174 | * @param string $tag_js 175 | * @param string $path 176 | * @param string $position 177 | */ 178 | 179 | public function add_js_file($tag_js, $path = '', $position = 'header') 180 | { 181 | switch ($position) 182 | { 183 | case 'header': 184 | self::instance()->tag_header_js .= self::instance()->pre_space . '' . "\n"; 185 | break; 186 | case 'footer': 187 | self::instance()->tag_footer_js .= self::instance()->pre_space . '' . "\n"; 188 | break; 189 | } 190 | } 191 | 192 | /** 193 | * Add multiple js files at once 194 | * 195 | * @param string $tag_js 196 | * @param string $path 197 | * @param string $position 198 | */ 199 | 200 | public function add_js_files($tag_js = array(), $path = '', $position = 'header') 201 | { 202 | if (is_array($tag_js)) 203 | { 204 | switch ($position) 205 | { 206 | case 'header': 207 | foreach ($tag_js AS $value) 208 | { 209 | self::instance()->tag_header_js .= self::instance()->pre_space . '' . "\n"; 210 | } 211 | break; 212 | case 'footer': 213 | foreach ($tag_js AS $value) 214 | { 215 | self::instance()->tag_footer_js .= self::instance()->pre_space . '' . "\n"; 216 | } 217 | break; 218 | } 219 | } 220 | } 221 | 222 | /** 223 | * Add raw text to javascript section 224 | * 225 | * @param string $tag_js 226 | * @param string $path 227 | * @param string $position 228 | */ 229 | 230 | public function add_js_rawtext($content, $position = 'header') 231 | { 232 | switch ($position) 233 | { 234 | case 'header': 235 | self::instance()->rawtext_header_js .= "\n" . $content . "\n"; 236 | break; 237 | case 'footer': 238 | self::instance()->rawtext_footer_js .= "\n" . $content . "\n"; 239 | break; 240 | } 241 | } 242 | 243 | /** 244 | * Output default title to prevent "Undefined variable" error 245 | * 246 | * @return string 247 | */ 248 | 249 | public function get_title() 250 | { 251 | return self::instance()->title; 252 | } 253 | 254 | /** 255 | * Set title 256 | * 257 | * @param string $type 258 | * @param array $attribute 259 | */ 260 | 261 | public function set_title($title) 262 | { 263 | self::instance()->title = $title; 264 | 265 | return self::instance()->title; 266 | } 267 | 268 | /** 269 | * Output data in head 270 | * 271 | * @return string 272 | */ 273 | 274 | public function get_header() 275 | { 276 | self::instance()->meta_default(); 277 | self::instance()->meta_twitter(); 278 | self::instance()->meta_facebook(); 279 | 280 | $_header = self::instance()->tag_meta . self::instance()->tag_css . self::instance()->tag_header_js; 281 | 282 | if (!empty(self::instance()->rawtext_header_js)) 283 | { 284 | $_header .= '' . "\n"; 287 | } 288 | 289 | if (!empty(self::instance()->rawtext_css)) 290 | { 291 | $_header .= '' . "\n"; 294 | } 295 | 296 | return $_header; 297 | } 298 | 299 | /** 300 | * Output data in footer 301 | * 302 | * @return string 303 | */ 304 | 305 | public function get_footer() 306 | { 307 | $_footer = self::instance()->tag_footer_js; 308 | 309 | if (!empty(self::instance()->rawtext_footer_js)) 310 | { 311 | $_footer .= '' . "\n"; 314 | } 315 | 316 | return $_footer; 317 | } 318 | 319 | 320 | /** 321 | * Get attributes from HTML body tag 322 | * 323 | * @return string 324 | */ 325 | 326 | public function get_body_attr() 327 | { 328 | return self::instance()->body_attribute; 329 | } 330 | 331 | 332 | /** 333 | * Set attributes to HTML body tag 334 | * 335 | * @param string $type 336 | * @param array $attribute 337 | */ 338 | 339 | public function set_body_attr($attribute = array()) 340 | { 341 | $attr_array = array(); 342 | 343 | foreach ($attribute AS $key => $value) 344 | { 345 | $attr_array[] = $key . '="' . $value . '"'; 346 | } 347 | $attr_string = implode(' ', $attr_array); 348 | 349 | self::instance()->body_attribute .= $attr_string; 350 | } 351 | 352 | /**************************************************************** 353 | * Private functions * 354 | ****************************************************************/ 355 | 356 | private function meta_default() 357 | { 358 | foreach (self::instance()->meta_default AS $key => $value) 359 | { 360 | if ($value != '') 361 | { 362 | if ($key == 'canonical') 363 | { 364 | self::instance()->tag_meta .= self::instance()->pre_space . '' . "\n"; 365 | } 366 | else 367 | { 368 | self::instance()->tag_meta .= self::instance()->pre_space . '' . "\n"; 369 | } 370 | } 371 | } 372 | } 373 | 374 | private function meta_twitter() 375 | { 376 | foreach (self::instance()->meta_twitter AS $key => $value) 377 | { 378 | if ($value != '') 379 | { 380 | if ($key == 'image_src') 381 | { 382 | $key = 'image:src'; 383 | } 384 | 385 | self::instance()->tag_meta .= self::instance()->pre_space . '' . "\n"; 386 | } 387 | } 388 | } 389 | 390 | private function meta_facebook() 391 | { 392 | foreach (self::instance()->meta_facebook AS $key => $value) 393 | { 394 | if ($value != '') 395 | { 396 | if ($key == 'admins' OR $key == 'app_id') 397 | { 398 | $property = 'fb'; 399 | } 400 | else 401 | { 402 | $property = 'og'; 403 | } 404 | 405 | self::instance()->tag_meta .= self::instance()->pre_space . '' . "\n"; 406 | } 407 | } 408 | } 409 | } 410 | function CI_title() 411 | { 412 | return layout::instance()->get_title(); 413 | } 414 | 415 | function CI_head() 416 | { 417 | return layout::instance()->get_header(); 418 | } 419 | 420 | function CI_footer() 421 | { 422 | return layout::instance()->get_footer(); 423 | } 424 | 425 | function CI_body_attr() 426 | { 427 | if (!empty(layout::instance()->get_body_attr())) 428 | { 429 | return ' ' . layout::instance()->get_body_attr(); 430 | } 431 | } 432 | --------------------------------------------------------------------------------