PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/application/libraries/template.php

https://github.com/santanu1122/codeigniter_hmvc_kickstart
PHP | 117 lines | 83 code | 18 blank | 16 comment | 11 complexity | 8fdd14016f8d44791ab79ad2fe03ceba MD5 | raw file
  1. <?php
  2. (defined('BASEPATH')) OR exit('No direct script access allowed');
  3. /**
  4. * @author Sinh Quan <qvsinh@gmail.com>
  5. * @todo manage view, template in codeigniter
  6. */
  7. class template {
  8. protected $ci;
  9. var $theme = "default";
  10. var $layout = "/layout/default_layout";
  11. var $data = array();
  12. var $compress_html_output = FALSE;
  13. function __construct() {
  14. $this->ci = & get_instance();
  15. }
  16. function set($name, $value) {
  17. $this->data[$name] = $value;
  18. }
  19. function set_title($value) {
  20. $this->data['html']['title'] = $value;
  21. }
  22. function set_description($value) {
  23. $this->data['html']['description'] = $value;
  24. }
  25. function set_keyword($value) {
  26. $this->data['html']['keyword'] = $value;
  27. }
  28. function set_layout($layout) {
  29. if ($layout) {
  30. $this->layout = "/layout/$layout";
  31. }
  32. }
  33. function include_layout($layout){
  34. $this->ci->load->view($this->theme . "/layout/$layout", array('view' => $this->data));
  35. }
  36. /**
  37. * @todo include view in same module
  38. * @example include_view('view_name');
  39. */
  40. function include_view($view_file){
  41. if ($this->compress_html_output) {
  42. $this->registerFilter('output', 'compress_html_output'); //compress_html_output function writen in base_helper.php
  43. }
  44. $this->ci->load->view($view_file, array('view' => $this->data));
  45. }
  46. /**
  47. * @todo include view in other module
  48. * @example include_module_view('module_name/view_name');
  49. */
  50. function include_module_view($view_file){
  51. if ($this->compress_html_output) {
  52. $this->registerFilter('output', 'compress_html_output'); //compress_html_output function writen in base_helper.php
  53. }
  54. $this->ci->load->view($this->theme . "/modules/$view_file", array('view' => $this->data));
  55. }
  56. function view($view_file = FALSE) {
  57. if ($view_file)
  58. $this->data['module_view'] = $this->theme . "/modules/$view_file";
  59. if ($this->compress_html_output) {
  60. $this->registerFilter('output', 'compress_html_output'); //compress_html_output function writen in base_helper.php
  61. }
  62. $this->ci->load->view($this->theme . $this->layout, array('view' => $this->data));
  63. }
  64. function get_view($view_file = FALSE) {
  65. if ($view_file) {
  66. $view_file = $this->theme . "/modules/$view_file";
  67. if ($this->compress_html_output) {
  68. $this->registerFilter('output', 'compress_html_output'); //compress_html_output function writen in base_helper.php
  69. }
  70. return $this->ci->load->view($view_file, array('view' => $this->data), TRUE);
  71. }
  72. return FALSE;
  73. }
  74. //add assets(js, css) files
  75. /**
  76. * @todo add assets(js, css) files. auto detect by file extension
  77. */
  78. function add_asset($file_path){
  79. if (is_array($file_path)) {
  80. foreach ($file_path as $file) {
  81. $file_path_temp = strtok($file, '?');
  82. $file_ext = strtolower(substr(strrchr($file_path_temp, '.'), 1));
  83. if($file_ext=='js'){
  84. $this->data['html_assets']['more_js'][] = $file;
  85. }elseif($file_ext=='css'){
  86. $this->data['html_assets']['more_css'][] = $file;
  87. }
  88. }
  89. } else {
  90. $file_path_temp = strtok($file_path, '?');
  91. $file_ext = strtolower(substr(strrchr($file_path_temp, '.'), 1));
  92. if($file_ext=='js'){
  93. $this->data['html_assets']['more_js'][] = $file_path;
  94. }elseif($file_ext=='css'){
  95. $this->data['html_assets']['more_css'][] = $file_path;
  96. }
  97. }
  98. }
  99. }