├── README.md ├── application ├── config │ └── config_layout.php ├── controllers │ └── Welcome.php ├── libraries │ └── Layout.php ├── templates │ ├── alternative_template │ │ ├── config.php │ │ └── menu_block.php │ ├── common_template │ │ ├── common_template.php │ │ └── config.php │ ├── footer_partial │ │ ├── config.php │ │ └── footer_partial.php │ └── main_template │ │ ├── config.php │ │ └── menu_block.php └── views │ └── controllers │ └── Welcome │ └── actions │ ├── create_update.php │ ├── hello.php │ └── index.php ├── license.txt └── web ├── css ├── app.css └── controllers │ └── Welcome │ ├── actions │ ├── create_update.css │ ├── hello.css │ └── index.css │ └── controller.css └── js ├── app.js └── controllers └── Welcome ├── actions ├── create_update.js ├── hello.js └── index.js └── controller.js /README.md: -------------------------------------------------------------------------------- 1 | # CodeIgniter Layout Library 2 | * **Author** : Vincent MOULIN 3 | * **License** : MIT License Copyright (c) 2017-2019 Vincent MOULIN 4 | * **Version** : 4.1.0 5 | * **CodeIgniter Forum Thread** : [https://forum.codeigniter.com/thread-68021.html](https://forum.codeigniter.com/thread-68021.html) 6 | 7 | ## Installation 8 | 1. Copy the file ./application/libraries/Layout.php 9 | 2. Copy the file ./application/config/config_layout.php 10 | 3. Open the file ./application/config/autoload.php: 11 | * add 'layout' to the array $autoload['libraries'] 12 | * add 'url' to the array $autoload['helper'] if not already added 13 | * add 'config_layout' to the array $autoload['config'] 14 | 4. Create the structure ("Welcome" and "hello" is for example): 15 | 16 | ```text 17 | ├── application 18 | │ ├── templates 19 | │ └── views 20 | │ └── controllers 21 | │ └── Welcome 22 | │ └── actions 23 | │ └── hello.php 24 | │ 25 | └── web 26 | ├── css 27 | │ ├── controllers 28 | │ │ └── Welcome 29 | │ │ ├── actions 30 | │ │ │ └── hello.css 31 | │ │ │ 32 | │ │ └── controller.css 33 | │ │ 34 | │ └── app.css 35 | │ 36 | └── js 37 | ├── controllers 38 | │ └── Welcome 39 | │ ├── actions 40 | │ │ └── hello.js 41 | │ │ 42 | │ └── controller.js 43 | │ 44 | └── app.js 45 | ``` 46 | Note: Create the css and javascript files only if needed ! 47 | 48 | ## Description 49 | ### The getters 50 | * **get_template()** 51 | * **get_title()** 52 | * **get_charset()** 53 | * **get_metadata()** 54 | * **get_http_equiv()** 55 | * **get_breadcrumb()** 56 | * **get_sections()** 57 | * **get_section()** 58 | * **get_css()** 59 | * **get_js()** 60 | 61 | ### The setters 62 | * **set_template()** : Set the template of the page 63 | * **set_title()** : Set the title of the page 64 | * **set_charset()** : Set the charset of the page 65 | * **set_metadata()** : Set a "name" metadata of the page 66 | * **unset_metadata()** : Unset a "name" metadata or all the "name" metadata 67 | * **set_http_equiv()** : Set a "http-equiv" metadata of the page 68 | * **unset_http_equiv()** : Unset a "http-equiv" metadata or all the "http-equiv" metadata 69 | * **add_breadcrumb_item()** : Add an item to the breadcrumb 70 | * **remove_breadcrumb_item()** : Remove an item from the breadcrumb 71 | 72 | ### The "breadcrumb" section 73 | * **return_breadcrumb()** : Return the breadcrumb 74 | 75 | ### The "asset" section 76 | * **add_css_uri()** : Add a css uri asset to the layout 77 | Example: `$CI->layout->add_css_uri('css/app.css');` 78 | * **add_css_str()** : Add a css string asset to the layout 79 | Example: `$CI->layout->add_css_str('/* some css code */');` 80 | * **add_css_php()** : Add a css php asset to the layout 81 | Example: `$CI->layout->add_css_php('my_function');` 82 | `my_function()` is a php function which returns some css code. 83 | * **add_js_uri()** : Add a javascript uri asset to the layout 84 | Example: `$CI->layout->add_js_uri('js/app.js');` 85 | * **add_js_str()** : Add a javascript string asset to the layout 86 | Example: `$CI->layout->add_js_str("var base_url = '" . $CI->config->item('base_url') . "';");` 87 | * **add_js_php()** : Add a javascript php asset to the layout 88 | Example: `$CI->layout->add_js_php(['My_class', 'my_method']);` 89 | `My_class::my_method()` is a php function which returns some javascript code. 90 | * **add_basic_css()** : Add the basic css assets according to the given tags or all the basic css assets 91 | * **add_basic_js()** : Add the basic javascript assets according to the given tags or all the basic javascript assets 92 | * **add_basic_assets()** : Add the basic css and javascript assets according to the given tags or all the basic css and javascript assets 93 | * **add_basic_css_except()** : Add the basic css assets except those that have a tag in common with the given tags 94 | * **add_basic_js_except()** : Add the basic javascript assets except those that have a tag in common with the given tags 95 | * **add_basic_assets_except()** : Add the basic css and javascript assets except those that have a tag in common with the given tags 96 | 97 | Note: The basic assets are defined in the variables `$config['layout_basic_css']` and `$config['layout_basic_js']` of the configuration file `config_layout.php`. 98 | 99 | Note: If a basic asset is a string, it is considered as a local uri asset with no attributes and no tags. 100 | 101 | Note: Tags are useful if you want to put some javascript at one place (for example in the head section) and some other javascript at another place (for example in the bottom of the page). 102 | All tags must be declared in the variables `$config['layout_css_tags']` and `$config['layout_js_tags']` of the configuration file `config_layout.php`. 103 | 104 | Example: 105 | 106 | $config['layout_basic_css'] = array( 107 | 'css/app.css', 108 | array( 109 | 'type' => 'uri', 110 | 'uri' => 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', 111 | 'location' => 'remote', 112 | 'attributes' => array( 113 | 'integrity' => 'sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u', 114 | 'crossorigin' => 'anonymous', 115 | ), 116 | ), 117 | ); 118 | $config['layout_basic_js'] = array( 119 | 'js/app.js', 120 | array( 121 | 'type' => 'uri', 122 | 'uri' => 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', 123 | 'location' => 'remote', 124 | 'attributes' => array( 125 | 'integrity' => 'sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa', 126 | 'crossorigin' => 'anonymous', 127 | 'async' => true, 128 | 'defer' => false, 129 | ), 130 | ), 131 | array( 132 | 'type' => 'str', 133 | 'content' => '/* some javascript code */', 134 | 'tags' => ['page_head'], 135 | ), 136 | array( 137 | 'type' => 'php', 138 | 'callback' => 'my_function', 139 | 'tags' => ['page_bottom'], 140 | ), 141 | array( 142 | 'type' => 'php', 143 | 'callback' => ['My_class', 'my_method'], 144 | ), 145 | ); 146 | $config['layout_css_tags'] = []; 147 | $config['layout_js_tags'] = ['page_head', 'page_bottom']; 148 | 149 | ### The triggers 150 | The triggers have to be used in the templates. 151 | * **trigger_title()** : Trigger the insertion of the title of the page 152 | * **trigger_charset()** : Trigger the insertion of the charset of the page 153 | * **trigger_metadata()** : Trigger the insertion of all the "name" metadata 154 | * **trigger_http_equiv()** : Trigger the insertion of all the "http-equiv" metadata 155 | * **trigger_breadcrumb()** : Trigger the insertion of the breadcrumb 156 | * **trigger_section()** : Trigger the insertion of a section 157 | * **trigger_css()** : Trigger the insertion of the css assets according to the given tags or all the css assets 158 | * **trigger_js()** : Trigger the insertion of the javascript assets according to the given tags or all the javascript assets 159 | * **trigger_css_except()** : Trigger the insertion of the css assets except those that have a tag in common with the given tags 160 | * **trigger_js_except()** : Trigger the insertion of the javascript assets except those that have a tag in common with the given tags 161 | 162 | ### The "template" section 163 | * **include_template()** : Include a template 164 | * **block()** : Define a block which should be implemented in a sub-template (but may also be implemented in the same template or inherited from an ancestor template) 165 | 166 | Note: 167 | * A template is a folder in the `./application/templates` folder. 168 | * Each template folder contains a PHP or YAML configuration file (`config.php` or `config.yml`). 169 | * This configuration file contains only one data: `parent_template` whose value is null or the name of the parent template. 170 | * For more details, see the examples in the `./application/templates` folder. 171 | 172 | ### The "view" section 173 | * **render_action_view()** : Render an action view with the defined template 174 | The rendered view corresponds to the controller action where this method is called. 175 | The application, controller and action css/javascript files are automatically added (provided that the structure of the Layout web folder is compliant with the requirements). 176 | Note: This is the method that you will probably use most of the time. 177 | * **render_virtual_action_view()** : Render a virtual action view with the defined template 178 | The application, controller and virtual action css/javascript files are automatically added (provided that the structure of the Layout web folder is compliant with the requirements). 179 | See the example: [./application/controllers/Welcome.php](https://github.com/vmoulin78/codeigniter-layout-library/blob/master/application/controllers/Welcome.php) 180 | * **render_view()** : Render a view with the defined template 181 | * **load_view()** : Load a view output in a section 182 | Example: 183 | * in the controller: `$this->layout->load_view('widget_view', $data_for_widget_view, 'widget_section');` 184 | * in the template, you put `layout->trigger_section('widget_section'); ?>` where you want to insert the loaded view "widget_view". 185 | * **return_view()** : Return a view output -------------------------------------------------------------------------------- /application/config/config_layout.php: -------------------------------------------------------------------------------- 1 | item('charset'); 34 | $config['layout_default_section'] = 'main'; 35 | 36 | $config['layout_basic_css'] = array(); 37 | $config['layout_basic_js'] = array(); 38 | $config['layout_css_tags'] = array(); 39 | $config['layout_js_tags'] = array(); 40 | 41 | $config['layout_breadcrumb_opening_tag'] = '
'; 43 | $config['layout_breadcrumb_item_opening_tag'] = ''; 44 | $config['layout_breadcrumb_item_closing_tag'] = ''; 45 | $config['layout_breadcrumb_item_separator'] = ' > '; 46 | -------------------------------------------------------------------------------- /application/controllers/Welcome.php: -------------------------------------------------------------------------------- 1 | layout->add_basic_assets() 8 | ->render_action_view(); 9 | } 10 | 11 | public function hello($name = 'World') { 12 | $data['name'] = $name; 13 | 14 | $this->layout->set_template('alternative_template') 15 | ->set_title('The hello title') 16 | ->set_metadata('description', 'The description') 17 | ->set_http_equiv('refresh', 30) 18 | ->add_breadcrumb_item('item 1') 19 | ->add_breadcrumb_item('item 2', site_url('Welcome/index')) 20 | ->add_breadcrumb_item('item 3', site_url('Welcome/hello')) 21 | ->add_basic_assets() 22 | ->render_action_view($data); 23 | } 24 | 25 | /** 26 | * Example for the virtual action view 27 | * The virtual action is : create_update 28 | * The create and update view are very similar, so following the DRY concept, we have only one common view create_update.php 29 | */ 30 | public function create() { 31 | $data = array( 32 | // some data 33 | ); 34 | 35 | $this->layout->add_basic_assets() 36 | ->render_virtual_action_view('create_update', $data); 37 | } 38 | 39 | public function update() { 40 | $data = array( 41 | // some data 42 | ); 43 | 44 | $this->layout->add_basic_assets() 45 | ->render_virtual_action_view('create_update', $data); 46 | } 47 | } -------------------------------------------------------------------------------- /application/libraries/Layout.php: -------------------------------------------------------------------------------- 1 | CI =& get_instance(); 53 | 54 | $default_section = $this->CI->config->item('layout_default_section'); 55 | $this->sections = array($default_section => ''); 56 | 57 | $this->set_template($this->CI->config->item('layout_default_template')); 58 | $this->set_title($this->CI->config->item('layout_default_title')); 59 | $this->set_charset($this->CI->config->item('layout_default_charset')); 60 | } 61 | 62 | /******************************************************************************/ 63 | 64 | /* 65 | |------------------------------------------------------------------------------- 66 | | The getters 67 | |------------------------------------------------------------------------------- 68 | */ 69 | 70 | public function get_template() { 71 | return $this->template; 72 | } 73 | 74 | public function get_title() { 75 | return $this->title; 76 | } 77 | 78 | public function get_charset() { 79 | return $this->charset; 80 | } 81 | 82 | public function get_metadata() { 83 | return $this->metadata; 84 | } 85 | 86 | public function get_http_equiv() { 87 | return $this->http_equiv; 88 | } 89 | 90 | public function get_breadcrumb() { 91 | return $this->breadcrumb; 92 | } 93 | 94 | public function get_sections() { 95 | return $this->sections; 96 | } 97 | 98 | public function get_section($section) { 99 | return $this->sections[$section]; 100 | } 101 | 102 | public function get_css() { 103 | return $this->css; 104 | } 105 | 106 | public function get_js() { 107 | return $this->js; 108 | } 109 | 110 | /******************************************************************************/ 111 | 112 | /* 113 | |------------------------------------------------------------------------------- 114 | | The setters 115 | |------------------------------------------------------------------------------- 116 | */ 117 | 118 | /** 119 | * Set the template of the page 120 | * 121 | * @access public 122 | * @param $template 123 | * @return $this or false if the template $template is incorrect 124 | */ 125 | public function set_template($template) { 126 | if ( ! $this->is_template($template)) { 127 | show_error('Layout error: Incorrect template'); 128 | } 129 | 130 | $this->template = $template; 131 | return $this; 132 | } 133 | 134 | /** 135 | * Set the title of the page 136 | * 137 | * @access public 138 | * @param $title 139 | * @return $this 140 | */ 141 | public function set_title($title) { 142 | $this->title = $title; 143 | return $this; 144 | } 145 | 146 | /** 147 | * Set the charset of the page 148 | * 149 | * @access public 150 | * @param $charset 151 | * @return $this 152 | */ 153 | public function set_charset($charset) { 154 | $this->charset = $charset; 155 | return $this; 156 | } 157 | 158 | /** 159 | * Set the metadata whose name is $key with the content $value 160 | * 161 | * @access public 162 | * @param $key 163 | * @param $value 164 | * @return $this 165 | */ 166 | public function set_metadata($key, $value) { 167 | $this->metadata[$key] = $value; 168 | return $this; 169 | } 170 | 171 | /** 172 | * Unset the metadata whose name is $key 173 | * If $key is null, all the "name" metadata are unsetted. 174 | * 175 | * @access public 176 | * @param $key 177 | * @return $this 178 | */ 179 | public function unset_metadata($key = null) { 180 | if (is_null($key)) { 181 | $this->metadata = array(); 182 | } else { 183 | unset($this->metadata[$key]); 184 | } 185 | 186 | return $this; 187 | } 188 | 189 | /** 190 | * Set the metadata whose http-equiv is $key with the content $value 191 | * 192 | * @access public 193 | * @param $key 194 | * @param $value 195 | * @return $this 196 | */ 197 | public function set_http_equiv($key, $value) { 198 | $this->http_equiv[$key] = $value; 199 | return $this; 200 | } 201 | 202 | /** 203 | * Unset the metadata whose http-equiv is $key 204 | * If $key is null, all the "http-equiv" metadata are unsetted. 205 | * 206 | * @access public 207 | * @param $key 208 | * @return $this 209 | */ 210 | public function unset_http_equiv($key = null) { 211 | if (is_null($key)) { 212 | $this->http_equiv = array(); 213 | } else { 214 | unset($this->http_equiv[$key]); 215 | } 216 | 217 | return $this; 218 | } 219 | 220 | /** 221 | * Add the breadcrumb item with the label $label and the href $href 222 | * If $href is null, the breadcrumb item will be a simple text instead of a link. 223 | * If $position is 'first' (resp. 'last'), the breadcrumb item is added at the beginning (resp. end) of the breadcrumb. 224 | * 225 | * @access public 226 | * @param $label 227 | * @param $href 228 | * @param $position ['first'|'last'] 229 | * @return $this 230 | */ 231 | public function add_breadcrumb_item($label, $href = null, $position = 'last') { 232 | $breadcrumb_item = array( 233 | 'label' => $label, 234 | 'href' => $href, 235 | ); 236 | 237 | if ($position === 'last') { 238 | array_push($this->breadcrumb, $breadcrumb_item); 239 | } elseif ($position === 'first') { 240 | array_unshift($this->breadcrumb, $breadcrumb_item); 241 | } else { 242 | show_error('Layout error: Incorrect parameter'); 243 | } 244 | 245 | return $this; 246 | } 247 | 248 | /** 249 | * Remove an item of the breadcrumb 250 | * If $position is 'first' (resp. 'last'), the removed breadcrumb item is the first (resp. last) one. 251 | * 252 | * @access public 253 | * @param $position ['first'|'last'] 254 | * @return $this 255 | */ 256 | public function remove_breadcrumb_item($position = 'last') { 257 | if ($position === 'last') { 258 | array_pop($this->breadcrumb); 259 | } elseif ($position === 'first') { 260 | array_shift($this->breadcrumb); 261 | } else { 262 | show_error('Layout error: Incorrect parameter'); 263 | } 264 | 265 | return $this; 266 | } 267 | 268 | /******************************************************************************/ 269 | 270 | /* 271 | |------------------------------------------------------------------------------- 272 | | The "breadcrumb" section 273 | |------------------------------------------------------------------------------- 274 | */ 275 | 276 | /** 277 | * Return the breadcrumb 278 | * 279 | * @access public 280 | * @return The breadcrumb 281 | */ 282 | public function return_breadcrumb() { 283 | $retour = $this->CI->config->item('layout_breadcrumb_opening_tag'); 284 | 285 | $flat_breadcrumb_items = array(); 286 | foreach ($this->breadcrumb as $breadcrumb_item) { 287 | $flat_breadcrumb_item = $this->CI->config->item('layout_breadcrumb_item_opening_tag'); 288 | 289 | if (is_null($breadcrumb_item['href'])) { 290 | $flat_breadcrumb_item .= $breadcrumb_item['label']; 291 | } else { 292 | $flat_breadcrumb_item .= '' . $breadcrumb_item['label'] . ''; 293 | } 294 | 295 | $flat_breadcrumb_item .= $this->CI->config->item('layout_breadcrumb_item_closing_tag'); 296 | 297 | $flat_breadcrumb_items[] = $flat_breadcrumb_item; 298 | } 299 | 300 | $retour .= implode($this->CI->config->item('layout_breadcrumb_item_separator'), $flat_breadcrumb_items); 301 | 302 | $retour .= $this->CI->config->item('layout_breadcrumb_closing_tag'); 303 | 304 | return $retour; 305 | } 306 | 307 | /******************************************************************************/ 308 | 309 | /* 310 | |------------------------------------------------------------------------------- 311 | | The "asset" section 312 | |------------------------------------------------------------------------------- 313 | */ 314 | 315 | /** 316 | * Get the absolute href of the asset whose uri is $uri and location is $location 317 | * 318 | * @access private 319 | * @param $uri 320 | * @param $location ['local'|'remote'] 321 | * @return The absolute href of the asset whose uri is $uri and location is $location 322 | */ 323 | private function asset_absolute_href($uri, $location) { 324 | switch ($location) { 325 | case 'local': 326 | return base_url() . $this->CI->config->item('layout_web_folder') . '/' . $uri; 327 | case 'remote': 328 | return $uri; 329 | default: 330 | show_error('Layout error: Incorrect parameter'); 331 | } 332 | } 333 | 334 | /** 335 | * Check if the css tags $tags are correctly defined in $this->CI->config->item('layout_css_tags') 336 | * 337 | * @access private 338 | * @param $tags 339 | * @return void 340 | */ 341 | private function check_css_tags(array $tags) { 342 | if ( ! empty(array_diff($tags, $this->CI->config->item('layout_css_tags')))) { 343 | show_error('Layout error: Unknown tag for css asset'); 344 | } 345 | } 346 | 347 | /** 348 | * Check if the javascript tags $tags are correctly defined in $this->CI->config->item('layout_js_tags') 349 | * 350 | * @access private 351 | * @param $tags 352 | * @return void 353 | */ 354 | private function check_js_tags(array $tags) { 355 | if ( ! empty(array_diff($tags, $this->CI->config->item('layout_js_tags')))) { 356 | show_error('Layout error: Unknown tag for javascript asset'); 357 | } 358 | } 359 | 360 | /** 361 | * Complete the css asset data $css with the default values 362 | * 363 | * @access private 364 | * @param $css 365 | * @return void 366 | */ 367 | private function complete_css_data(&$css) { 368 | if (is_string($css)) { 369 | $css = array( 370 | 'type' => 'uri', 371 | 'uri' => $css, 372 | ); 373 | } 374 | 375 | $reflector = new ReflectionClass(__CLASS__); 376 | 377 | switch ($css['type']) { 378 | case 'uri': 379 | $parameters = array('location', 'attributes', 'tags'); 380 | break; 381 | case 'str': 382 | $parameters = array('attributes', 'tags'); 383 | break; 384 | case 'php': 385 | $parameters = array('args', 'attributes', 'tags'); 386 | break; 387 | default: 388 | break; 389 | } 390 | 391 | foreach ($reflector->getMethod('add_css_' . $css['type'])->getParameters() as $item) { 392 | if (in_array($item->getName(), $parameters)) { 393 | if ( ! isset($css[$item->getName()])) { 394 | $css[$item->getName()] = $item->getDefaultValue(); 395 | } 396 | } 397 | } 398 | } 399 | 400 | /** 401 | * Complete the javascript asset data $js with the default values 402 | * 403 | * @access private 404 | * @param $js 405 | * @return void 406 | */ 407 | private function complete_js_data(&$js) { 408 | if (is_string($js)) { 409 | $js = array( 410 | 'type' => 'uri', 411 | 'uri' => $js, 412 | ); 413 | } 414 | 415 | $reflector = new ReflectionClass(__CLASS__); 416 | 417 | switch ($js['type']) { 418 | case 'uri': 419 | $parameters = array('location', 'attributes', 'tags'); 420 | break; 421 | case 'str': 422 | $parameters = array('attributes', 'tags'); 423 | break; 424 | case 'php': 425 | $parameters = array('args', 'attributes', 'tags'); 426 | break; 427 | default: 428 | break; 429 | } 430 | 431 | foreach ($reflector->getMethod('add_js_' . $js['type'])->getParameters() as $item) { 432 | if (in_array($item->getName(), $parameters)) { 433 | if ( ! isset($js[$item->getName()])) { 434 | $js[$item->getName()] = $item->getDefaultValue(); 435 | } 436 | } 437 | } 438 | } 439 | 440 | /** 441 | * Add the css asset $css to the layout 442 | * 443 | * @access private 444 | * @param $css 445 | * @return $this 446 | */ 447 | private function add_css(array $css) { 448 | switch ($css['type']) { 449 | case 'uri': 450 | return $this->add_css_uri($css['uri'], $css['location'], $css['attributes'], $css['tags']); 451 | case 'str': 452 | return $this->add_css_str($css['content'], $css['attributes'], $css['tags']); 453 | case 'php': 454 | return $this->add_css_php($css['callback'], $css['args'], $css['attributes'], $css['tags']); 455 | default: 456 | return; 457 | } 458 | } 459 | 460 | /** 461 | * Add the javascript asset $js to the layout 462 | * 463 | * @access private 464 | * @param $js 465 | * @return $this 466 | */ 467 | private function add_js(array $js) { 468 | switch ($js['type']) { 469 | case 'uri': 470 | return $this->add_js_uri($js['uri'], $js['location'], $js['attributes'], $js['tags']); 471 | case 'str': 472 | return $this->add_js_str($js['content'], $js['attributes'], $js['tags']); 473 | case 'php': 474 | return $this->add_js_php($js['callback'], $js['args'], $js['attributes'], $js['tags']); 475 | default: 476 | return; 477 | } 478 | } 479 | 480 | /** 481 | * Add a css uri asset to the layout 482 | * 483 | * If $location is 'local' then the reference folder is ./{$config['layout_web_folder']}/ 484 | * 485 | * Example 1: 486 | * $CI->layout->add_css_uri('css/controllers/Welcome/actions/hello.css'); 487 | * 488 | * Example 2: 489 | * $CI->layout->add_css_uri( 490 | * 'css/controllers/Welcome/actions/hello.css', 491 | * 'local', 492 | * ['media' => 'screen'], 493 | * ['tag1', 'tag2'] 494 | * ); 495 | * 496 | * Example 3: 497 | * $CI->layout->add_css_uri( 498 | * 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css', 499 | * 'remote', 500 | * array( 501 | * 'integrity' => 'sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u', 502 | * 'crossorigin' => 'anonymous', 503 | * ) 504 | * ); 505 | * 506 | * @access public 507 | * @param $uri 508 | * @param $location ['local'|'remote'] 509 | * @param $attributes 510 | * @param $tags 511 | * @param $return_bool 512 | * @return $this if $return_bool is false, otherwise true if the css asset has been added successfully and false otherwise 513 | */ 514 | public function add_css_uri($uri, $location = 'local', array $attributes = [], array $tags = [], bool $return_bool = false) { 515 | if ( ! in_array($location, array('local', 'remote'))) { 516 | show_error('Layout error: Incorrect location for css uri asset'); 517 | } 518 | 519 | $this->check_css_tags($tags); 520 | 521 | if (($location == 'local') 522 | && ( ! file_exists(FCPATH . $this->CI->config->item('layout_web_folder') . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $uri))) 523 | ) { 524 | if ($return_bool) { 525 | return false; 526 | } else { 527 | show_error('Layout error: Incorrect css uri asset'); 528 | } 529 | } 530 | 531 | $this->css[] = array( 532 | 'type' => 'uri', 533 | 'absolute_href' => $this->asset_absolute_href($uri, $location), 534 | 'attributes' => $attributes, 535 | 'tags' => $tags, 536 | ); 537 | 538 | if ($return_bool) { 539 | return true; 540 | } else { 541 | return $this; 542 | } 543 | } 544 | 545 | /** 546 | * Add a css string asset to the layout 547 | * 548 | * Example: 549 | * $CI->layout->add_css_str('body {font-size: 14px;}'); 550 | * 551 | * @access public 552 | * @param $content Some css code 553 | * @param $attributes 554 | * @param $tags 555 | * @param $return_bool 556 | * @return $this if $return_bool is false, otherwise true if the css asset has been added successfully and false otherwise 557 | */ 558 | public function add_css_str($content, array $attributes = [], array $tags = [], bool $return_bool = false) { 559 | $this->check_css_tags($tags); 560 | 561 | if ( ! is_string($content)) { 562 | if ($return_bool) { 563 | return false; 564 | } else { 565 | show_error('Layout error: Incorrect css string asset'); 566 | } 567 | } 568 | 569 | $this->css[] = array( 570 | 'type' => 'str', 571 | 'content' => $content, 572 | 'attributes' => $attributes, 573 | 'tags' => $tags, 574 | ); 575 | 576 | if ($return_bool) { 577 | return true; 578 | } else { 579 | return $this; 580 | } 581 | } 582 | 583 | /** 584 | * Add a css php asset to the layout 585 | * 586 | * Example 1: 587 | * $CI->layout->add_css_php('my_function'); 588 | * 589 | * Example 2: 590 | * $CI->layout->add_css_php(array('My_class', 'my_method')); 591 | * 592 | * @access public 593 | * @param $callback A php function which returns some css code 594 | * @param $args The arguments used with the php function $callback 595 | * @param $attributes 596 | * @param $tags 597 | * @param $return_bool 598 | * @return $this if $return_bool is false, otherwise true if the css asset has been added successfully and false otherwise 599 | */ 600 | public function add_css_php($callback, array $args = [], array $attributes = [], array $tags = [], bool $return_bool = false) { 601 | $this->check_css_tags($tags); 602 | 603 | if ( ! is_callable($callback)) { 604 | if ($return_bool) { 605 | return false; 606 | } else { 607 | show_error('Layout error: Incorrect css php asset'); 608 | } 609 | } 610 | 611 | $this->css[] = array( 612 | 'type' => 'php', 613 | 'callback' => $callback, 614 | 'args' => $args, 615 | 'attributes' => $attributes, 616 | 'tags' => $tags, 617 | ); 618 | 619 | if ($return_bool) { 620 | return true; 621 | } else { 622 | return $this; 623 | } 624 | } 625 | 626 | /** 627 | * Add a javascript uri asset to the layout 628 | * 629 | * If $location is 'local' then the reference folder is ./{$config['layout_web_folder']}/ 630 | * 631 | * Example 1: 632 | * $CI->layout->add_js_uri('js/controllers/Welcome/actions/hello.js'); 633 | * 634 | * Example 2 (assuming jquery is a 'local' asset): 635 | * $CI->layout->add_js_uri( 636 | * 'third_party/jquery/js/jquery.js', 637 | * 'local', 638 | * ['charset' => 'UTF-8'], 639 | * ['tag1', 'tag2'] 640 | * ); 641 | * 642 | * Example 3 (assuming bootstrap is a 'remote' asset): 643 | * $CI->layout->add_js_uri( 644 | * 'https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js', 645 | * 'remote', 646 | * array( 647 | * 'integrity' => 'sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa', 648 | * 'crossorigin' => 'anonymous', 649 | * 'async' => true, 650 | * 'defer' => false, 651 | * ) 652 | * ); 653 | * 654 | * @access public 655 | * @param $uri 656 | * @param $location ['local'|'remote'] 657 | * @param $attributes 658 | * @param $tags 659 | * @param $return_bool 660 | * @return $this if $return_bool is false, otherwise true if the javascript asset has been added successfully and false otherwise 661 | */ 662 | public function add_js_uri($uri, $location = 'local', array $attributes = [], array $tags = [], bool $return_bool = false) { 663 | if ( ! in_array($location, array('local', 'remote'))) { 664 | show_error('Layout error: Incorrect location for javascript uri asset'); 665 | } 666 | 667 | $this->check_js_tags($tags); 668 | 669 | if (($location == 'local') 670 | && ( ! file_exists(FCPATH . $this->CI->config->item('layout_web_folder') . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $uri))) 671 | ) { 672 | if ($return_bool) { 673 | return false; 674 | } else { 675 | show_error('Layout error: Incorrect javascript uri asset'); 676 | } 677 | } 678 | 679 | $this->js[] = array( 680 | 'type' => 'uri', 681 | 'absolute_href' => $this->asset_absolute_href($uri, $location), 682 | 'attributes' => $attributes, 683 | 'tags' => $tags, 684 | ); 685 | 686 | if ($return_bool) { 687 | return true; 688 | } else { 689 | return $this; 690 | } 691 | } 692 | 693 | /** 694 | * Add a javascript string asset to the layout 695 | * 696 | * Example: 697 | * $CI->layout->add_js_str('var refresh_interval = 30;'); 698 | * 699 | * @access public 700 | * @param $content Some javascript code 701 | * @param $attributes 702 | * @param $tags 703 | * @param $return_bool 704 | * @return $this if $return_bool is false, otherwise true if the javascript asset has been added successfully and false otherwise 705 | */ 706 | public function add_js_str($content, array $attributes = [], array $tags = [], bool $return_bool = false) { 707 | $this->check_js_tags($tags); 708 | 709 | if ( ! is_string($content)) { 710 | if ($return_bool) { 711 | return false; 712 | } else { 713 | show_error('Layout error: Incorrect javascript string asset'); 714 | } 715 | } 716 | 717 | $this->js[] = array( 718 | 'type' => 'str', 719 | 'content' => $content, 720 | 'attributes' => $attributes, 721 | 'tags' => $tags, 722 | ); 723 | 724 | if ($return_bool) { 725 | return true; 726 | } else { 727 | return $this; 728 | } 729 | } 730 | 731 | /** 732 | * Add a javascript php asset to the layout 733 | * 734 | * Example 1: 735 | * $CI->layout->add_js_php('my_function'); 736 | * 737 | * Example 2: 738 | * $CI->layout->add_js_php(array('My_class', 'my_method')); 739 | * 740 | * @access public 741 | * @param $callback A php function which returns some javascript code 742 | * @param $args The arguments used with the php function $callback 743 | * @param $attributes 744 | * @param $tags 745 | * @param $return_bool 746 | * @return $this if $return_bool is false, otherwise true if the javascript asset has been added successfully and false otherwise 747 | */ 748 | public function add_js_php($callback, array $args = [], array $attributes = [], array $tags = [], bool $return_bool = false) { 749 | $this->check_js_tags($tags); 750 | 751 | if ( ! is_callable($callback)) { 752 | if ($return_bool) { 753 | return false; 754 | } else { 755 | show_error('Layout error: Incorrect javascript php asset'); 756 | } 757 | } 758 | 759 | $this->js[] = array( 760 | 'type' => 'php', 761 | 'callback' => $callback, 762 | 'args' => $args, 763 | 'attributes' => $attributes, 764 | 'tags' => $tags, 765 | ); 766 | 767 | if ($return_bool) { 768 | return true; 769 | } else { 770 | return $this; 771 | } 772 | } 773 | 774 | /** 775 | * Add the basic css assets that have a tag in common with the list $tags 776 | * If $tags is null, all the basic css assets are added. 777 | * You may call this method with a single tag instead of an array. 778 | * These assets are defined in the variable $config['layout_basic_css'] of the configuration file ./config/config_layout.php 779 | * 780 | * @access public 781 | * @param $tags 782 | * @return $this 783 | */ 784 | public function add_basic_css($tags = null) { 785 | if (( ! is_null($tags)) 786 | && ( ! is_array($tags)) 787 | ) { 788 | $tags = array($tags); 789 | } 790 | 791 | foreach ($this->CI->config->item('layout_basic_css') as $css) { 792 | $this->complete_css_data($css); 793 | 794 | if (is_null($tags) 795 | || ( ! empty(array_intersect($css['tags'], $tags))) 796 | ) { 797 | $result = $this->add_css($css); 798 | if ($result === false) { 799 | show_error('Layout error: Incorrect css asset in basic assets'); 800 | } 801 | } 802 | } 803 | 804 | return $this; 805 | } 806 | 807 | /** 808 | * Add the basic javascript assets that have a tag in common with the list $tags 809 | * If $tags is null, all the basic javascript assets are added. 810 | * You may call this method with a single tag instead of an array. 811 | * These assets are defined in the variable $config['layout_basic_js'] of the configuration file ./config/config_layout.php 812 | * 813 | * @access public 814 | * @param $tags 815 | * @return $this 816 | */ 817 | public function add_basic_js($tags = null) { 818 | if (( ! is_null($tags)) 819 | && ( ! is_array($tags)) 820 | ) { 821 | $tags = array($tags); 822 | } 823 | 824 | foreach ($this->CI->config->item('layout_basic_js') as $js) { 825 | $this->complete_js_data($js); 826 | 827 | if (is_null($tags) 828 | || ( ! empty(array_intersect($js['tags'], $tags))) 829 | ) { 830 | $result = $this->add_js($js); 831 | if ($result === false) { 832 | show_error('Layout error: Incorrect javascript asset in basic assets'); 833 | } 834 | } 835 | } 836 | 837 | return $this; 838 | } 839 | 840 | /** 841 | * Add the basic css and javascript assets that have a tag in common with the list $tags 842 | * If $tags is null, all the basic css and javascript assets are added. 843 | * You may call this method with a single tag instead of an array. 844 | * These assets are defined in the variables $config['layout_basic_css'] and $config['layout_basic_js'] of the configuration file ./config/config_layout.php 845 | * 846 | * @access public 847 | * @param $tags 848 | * @return $this 849 | */ 850 | public function add_basic_assets($tags = null) { 851 | $this->add_basic_css($tags); 852 | $this->add_basic_js($tags); 853 | 854 | return $this; 855 | } 856 | 857 | /** 858 | * Add the basic css assets except those that have a tag in common with the list $tags 859 | * You may call this method with a single tag instead of an array. 860 | * These assets are defined in the variable $config['layout_basic_css'] of the configuration file ./config/config_layout.php 861 | * 862 | * @access public 863 | * @param $tags 864 | * @return $this 865 | */ 866 | public function add_basic_css_except($tags) { 867 | if ( ! is_array($tags)) { 868 | $tags = array($tags); 869 | } 870 | 871 | foreach ($this->CI->config->item('layout_basic_css') as $css) { 872 | $this->complete_css_data($css); 873 | 874 | if (empty(array_intersect($css['tags'], $tags))) { 875 | $result = $this->add_css($css); 876 | if ($result === false) { 877 | show_error('Layout error: Incorrect css asset in basic assets'); 878 | } 879 | } 880 | } 881 | 882 | return $this; 883 | } 884 | 885 | /** 886 | * Add the basic javascript assets except those that have a tag in common with the list $tags 887 | * You may call this method with a single tag instead of an array. 888 | * These assets are defined in the variable $config['layout_basic_js'] of the configuration file ./config/config_layout.php 889 | * 890 | * @access public 891 | * @param $tags 892 | * @return $this 893 | */ 894 | public function add_basic_js_except($tags) { 895 | if ( ! is_array($tags)) { 896 | $tags = array($tags); 897 | } 898 | 899 | foreach ($this->CI->config->item('layout_basic_js') as $js) { 900 | $this->complete_js_data($js); 901 | 902 | if (empty(array_intersect($js['tags'], $tags))) { 903 | $result = $this->add_js($js); 904 | if ($result === false) { 905 | show_error('Layout error: Incorrect javascript asset in basic assets'); 906 | } 907 | } 908 | } 909 | 910 | return $this; 911 | } 912 | 913 | /** 914 | * Add the basic css and javascript assets except those that have a tag in common with the list $tags 915 | * You may call this method with a single tag instead of an array. 916 | * These assets are defined in the variables $config['layout_basic_css'] and $config['layout_basic_js'] of the configuration file ./config/config_layout.php 917 | * 918 | * @access public 919 | * @param $tags 920 | * @return $this 921 | */ 922 | public function add_basic_assets_except($tags) { 923 | $this->add_basic_css_except($tags); 924 | $this->add_basic_js_except($tags); 925 | 926 | return $this; 927 | } 928 | 929 | /******************************************************************************/ 930 | 931 | /* 932 | |------------------------------------------------------------------------------- 933 | | The triggers 934 | |------------------------------------------------------------------------------- 935 | */ 936 | 937 | /** 938 | * Insert the css asset $css 939 | * 940 | * @access private 941 | * @param $css 942 | * @return void 943 | */ 944 | private function insert_css($css) { 945 | switch ($css['type']) { 946 | case 'uri': 947 | echo ' $attribute_value) { 960 | if ($attribute_value === false) { 961 | continue; 962 | } 963 | 964 | echo ' ' . $attribute_name; 965 | if ($attribute_value !== true) { 966 | echo '="' . $attribute_value . '"'; 967 | } 968 | } 969 | 970 | switch ($css['type']) { 971 | case 'uri': 972 | echo ' />'; 973 | break; 974 | case 'str': 975 | echo '>'; 976 | echo $css['content']; 977 | echo ''; 978 | break; 979 | case 'php': 980 | echo '>'; 981 | echo call_user_func_array($css['callback'], $css['args']); 982 | echo ''; 983 | break; 984 | default: 985 | break; 986 | } 987 | } 988 | 989 | /** 990 | * Insert the javascript asset $js 991 | * 992 | * @access private 993 | * @param $js 994 | * @return void 995 | */ 996 | private function insert_js($js) { 997 | switch ($js['type']) { 998 | case 'uri': 999 | echo ''; 1025 | break; 1026 | case 'str': 1027 | echo '>'; 1028 | echo $js['content']; 1029 | echo ''; 1030 | break; 1031 | case 'php': 1032 | echo '>'; 1033 | echo call_user_func_array($js['callback'], $js['args']); 1034 | echo ''; 1035 | break; 1036 | default: 1037 | break; 1038 | } 1039 | } 1040 | 1041 | /** 1042 | * Trigger the insertion of the title of the page 1043 | * 1044 | * @access public 1045 | * @return void 1046 | */ 1047 | public function trigger_title() { 1048 | echo 'Create - Update
-------------------------------------------------------------------------------- /application/views/controllers/Welcome/actions/hello.php: -------------------------------------------------------------------------------- 1 | 4 | 5 |Hello
-------------------------------------------------------------------------------- /application/views/controllers/Welcome/actions/index.php: -------------------------------------------------------------------------------- 1 | 4 | 5 |The index page
-------------------------------------------------------------------------------- /license.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017-2019 Vincent MOULIN 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /web/css/app.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmoulin78/codeigniter-layout-library/04d14ca74127b8fb88850b2064bab6bc78b7f680/web/css/app.css -------------------------------------------------------------------------------- /web/css/controllers/Welcome/actions/create_update.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmoulin78/codeigniter-layout-library/04d14ca74127b8fb88850b2064bab6bc78b7f680/web/css/controllers/Welcome/actions/create_update.css -------------------------------------------------------------------------------- /web/css/controllers/Welcome/actions/hello.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmoulin78/codeigniter-layout-library/04d14ca74127b8fb88850b2064bab6bc78b7f680/web/css/controllers/Welcome/actions/hello.css -------------------------------------------------------------------------------- /web/css/controllers/Welcome/actions/index.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmoulin78/codeigniter-layout-library/04d14ca74127b8fb88850b2064bab6bc78b7f680/web/css/controllers/Welcome/actions/index.css -------------------------------------------------------------------------------- /web/css/controllers/Welcome/controller.css: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmoulin78/codeigniter-layout-library/04d14ca74127b8fb88850b2064bab6bc78b7f680/web/css/controllers/Welcome/controller.css -------------------------------------------------------------------------------- /web/js/app.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmoulin78/codeigniter-layout-library/04d14ca74127b8fb88850b2064bab6bc78b7f680/web/js/app.js -------------------------------------------------------------------------------- /web/js/controllers/Welcome/actions/create_update.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmoulin78/codeigniter-layout-library/04d14ca74127b8fb88850b2064bab6bc78b7f680/web/js/controllers/Welcome/actions/create_update.js -------------------------------------------------------------------------------- /web/js/controllers/Welcome/actions/hello.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmoulin78/codeigniter-layout-library/04d14ca74127b8fb88850b2064bab6bc78b7f680/web/js/controllers/Welcome/actions/hello.js -------------------------------------------------------------------------------- /web/js/controllers/Welcome/actions/index.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmoulin78/codeigniter-layout-library/04d14ca74127b8fb88850b2064bab6bc78b7f680/web/js/controllers/Welcome/actions/index.js -------------------------------------------------------------------------------- /web/js/controllers/Welcome/controller.js: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vmoulin78/codeigniter-layout-library/04d14ca74127b8fb88850b2064bab6bc78b7f680/web/js/controllers/Welcome/controller.js --------------------------------------------------------------------------------