├── web
├── js
│ ├── app.js
│ └── controllers
│ │ └── Welcome
│ │ ├── actions
│ │ ├── hello.js
│ │ ├── index.js
│ │ └── create_update.js
│ │ └── controller.js
└── css
│ ├── app.css
│ └── controllers
│ └── Welcome
│ ├── controller.css
│ └── actions
│ ├── hello.css
│ ├── index.css
│ └── create_update.css
├── application
├── views
│ └── controllers
│ │ └── Welcome
│ │ └── actions
│ │ ├── index.php
│ │ ├── create_update.php
│ │ └── hello.php
├── templates
│ ├── footer_partial
│ │ ├── footer_partial.php
│ │ └── config.php
│ ├── main_template
│ │ ├── menu_block.php
│ │ └── config.php
│ ├── common_template
│ │ ├── config.php
│ │ └── common_template.php
│ └── alternative_template
│ │ ├── config.php
│ │ └── menu_block.php
├── controllers
│ └── Welcome.php
├── config
│ └── config_layout.php
└── libraries
│ └── Layout.php
├── license.txt
└── README.md
/web/js/app.js:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/web/css/app.css:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/web/css/controllers/Welcome/controller.css:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/web/js/controllers/Welcome/actions/hello.js:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/web/js/controllers/Welcome/actions/index.js:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/web/js/controllers/Welcome/controller.js:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/web/css/controllers/Welcome/actions/hello.css:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/web/css/controllers/Welcome/actions/index.css:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/web/css/controllers/Welcome/actions/create_update.css:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/web/js/controllers/Welcome/actions/create_update.js:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/application/views/controllers/Welcome/actions/index.php:
--------------------------------------------------------------------------------
1 |
4 |
5 |
The index page
--------------------------------------------------------------------------------
/application/views/controllers/Welcome/actions/create_update.php:
--------------------------------------------------------------------------------
1 |
4 |
5 | Create - Update
--------------------------------------------------------------------------------
/application/views/controllers/Welcome/actions/hello.php:
--------------------------------------------------------------------------------
1 |
4 |
5 | Hello
--------------------------------------------------------------------------------
/application/templates/footer_partial/footer_partial.php:
--------------------------------------------------------------------------------
1 |
4 |
5 |
--------------------------------------------------------------------------------
/application/templates/main_template/menu_block.php:
--------------------------------------------------------------------------------
1 |
4 |
5 |
--------------------------------------------------------------------------------
/application/templates/common_template/config.php:
--------------------------------------------------------------------------------
1 |
4 |
5 |
--------------------------------------------------------------------------------
/application/templates/common_template/common_template.php:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 | layout->trigger_title(); ?>
7 | layout->trigger_charset(); ?>
8 | layout->trigger_metadata(); ?>
9 | layout->trigger_http_equiv(); ?>
10 | layout->trigger_css(); ?>
11 |
12 |
13 |
14 |
15 |
16 | layout->block('menu_block'); ?>
17 |
18 |
19 |
20 |
21 | layout->trigger_breadcrumb(); ?>
22 |
23 |
24 |
25 |
26 | layout->trigger_section('main'); ?>
27 |
28 |
29 |
30 |
31 | layout->include_template('footer_partial'); ?>
32 |
33 |
34 | layout->trigger_js(); ?>
35 |
36 |
37 |
38 |
--------------------------------------------------------------------------------
/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.
--------------------------------------------------------------------------------
/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/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'] = '';
42 | $config['layout_breadcrumb_closing_tag'] = '
';
43 | $config['layout_breadcrumb_item_opening_tag'] = '';
44 | $config['layout_breadcrumb_item_closing_tag'] = ' ';
45 | $config['layout_breadcrumb_item_separator'] = ' > ';
46 |
--------------------------------------------------------------------------------
/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/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 '' . $this->title . ' ';
1049 | }
1050 |
1051 | /**
1052 | * Trigger the insertion of the charset of the page
1053 | *
1054 | * @access public
1055 | * @return void
1056 | */
1057 | public function trigger_charset() {
1058 | echo ' ';
1059 | }
1060 |
1061 | /**
1062 | * Trigger the insertion of all the "name" metadata
1063 | *
1064 | * @access public
1065 | * @return void
1066 | */
1067 | public function trigger_metadata() {
1068 | foreach ($this->metadata as $key => $value) {
1069 | echo ' ';
1070 | }
1071 | }
1072 |
1073 | /**
1074 | * Trigger the insertion of all the "http-equiv" metadata
1075 | *
1076 | * @access public
1077 | * @return void
1078 | */
1079 | public function trigger_http_equiv() {
1080 | foreach ($this->http_equiv as $key => $value) {
1081 | echo ' ';
1082 | }
1083 | }
1084 |
1085 | /**
1086 | * Trigger the insertion of the breadcrumb
1087 | *
1088 | * @access public
1089 | * @return void
1090 | */
1091 | public function trigger_breadcrumb() {
1092 | echo $this->return_breadcrumb();
1093 | }
1094 |
1095 | /**
1096 | * Trigger the insertion of the section $section
1097 | *
1098 | * @access public
1099 | * @param $section
1100 | * @return void
1101 | */
1102 | public function trigger_section($section) {
1103 | echo $this->sections[$section];
1104 | }
1105 |
1106 | /**
1107 | * Trigger the insertion of the css assets that have a tag in common with the list $tags
1108 | * If $tags is null, then all the css assets are inserted.
1109 | * You may call this method with a single tag instead of an array.
1110 | *
1111 | * @access public
1112 | * @param $tags
1113 | * @return void
1114 | */
1115 | public function trigger_css($tags = null) {
1116 | if (( ! is_null($tags))
1117 | && ( ! is_array($tags))
1118 | ) {
1119 | $tags = array($tags);
1120 | }
1121 |
1122 | foreach ($this->css as $css) {
1123 | if (is_null($tags)
1124 | || ( ! empty(array_intersect($css['tags'], $tags)))
1125 | ) {
1126 | $this->insert_css($css);
1127 | }
1128 | }
1129 | }
1130 |
1131 | /**
1132 | * Trigger the insertion of the javascript assets that have a tag in common with the list $tags
1133 | * If $tags is null, then all the javascript assets are inserted.
1134 | * You may call this method with a single tag instead of an array.
1135 | *
1136 | * @access public
1137 | * @param $tags
1138 | * @return void
1139 | */
1140 | public function trigger_js($tags = null) {
1141 | if (( ! is_null($tags))
1142 | && ( ! is_array($tags))
1143 | ) {
1144 | $tags = array($tags);
1145 | }
1146 |
1147 | foreach ($this->js as $js) {
1148 | if (is_null($tags)
1149 | || ( ! empty(array_intersect($js['tags'], $tags)))
1150 | ) {
1151 | $this->insert_js($js);
1152 | }
1153 | }
1154 | }
1155 |
1156 | /**
1157 | * Trigger the insertion of the css assets except those that have a tag in common with the list $tags
1158 | * You may call this method with a single tag instead of an array.
1159 | *
1160 | * @access public
1161 | * @param $tags
1162 | * @return void
1163 | */
1164 | public function trigger_css_except($tags) {
1165 | if ( ! is_array($tags)) {
1166 | $tags = array($tags);
1167 | }
1168 |
1169 | foreach ($this->css as $css) {
1170 | if (empty(array_intersect($css['tags'], $tags))) {
1171 | $this->insert_css($css);
1172 | }
1173 | }
1174 | }
1175 |
1176 | /**
1177 | * Trigger the insertion of the javascript assets except those that have a tag in common with the list $tags
1178 | * You may call this method with a single tag instead of an array.
1179 | *
1180 | * @access public
1181 | * @param $tags
1182 | * @return void
1183 | */
1184 | public function trigger_js_except($tags) {
1185 | if ( ! is_array($tags)) {
1186 | $tags = array($tags);
1187 | }
1188 |
1189 | foreach ($this->js as $js) {
1190 | if (empty(array_intersect($js['tags'], $tags))) {
1191 | $this->insert_js($js);
1192 | }
1193 | }
1194 | }
1195 |
1196 | /******************************************************************************/
1197 |
1198 | /*
1199 | |-------------------------------------------------------------------------------
1200 | | The "template" section
1201 | |-------------------------------------------------------------------------------
1202 | */
1203 |
1204 | /**
1205 | * Check if the template $template is a template
1206 | *
1207 | * @access private
1208 | * @param $template
1209 | * @return true if the template $template is a template and false otherwise
1210 | */
1211 | private function is_template($template) {
1212 | if (is_string($template)
1213 | && ( ! empty($template))
1214 | && (file_exists(APPPATH . 'templates' . DIRECTORY_SEPARATOR . $template))
1215 | ) {
1216 | return true;
1217 | } else {
1218 | return false;
1219 | }
1220 | }
1221 |
1222 | /**
1223 | * Return the configuration of the template $template
1224 | *
1225 | * @access private
1226 | * @param $template
1227 | * @return The configuration of the template $template
1228 | */
1229 | private function template_config($template) {
1230 | $template_path = APPPATH . 'templates' . DIRECTORY_SEPARATOR . $template . DIRECTORY_SEPARATOR;
1231 |
1232 | if (file_exists($template_path . 'config.yml')) {
1233 | return yaml_parse_file($template_path . 'config.yml');
1234 | } elseif (file_exists($template_path . 'config.php')) {
1235 | include($template_path . 'config.php');
1236 | return $config;
1237 | } else {
1238 | set_status_header(500);
1239 | exit('Layout error: Missing template configuration file');
1240 | }
1241 | }
1242 |
1243 | /**
1244 | * Include the snippet $snippet
1245 | *
1246 | * @access private
1247 | * @param $snippet A filepath corresponding to a root template or a block
1248 | * @return void
1249 | */
1250 | private function include_snippet($snippet) {
1251 | $CI =& get_instance();
1252 |
1253 | include($snippet);
1254 | }
1255 |
1256 | /**
1257 | * Get the current templates chain
1258 | *
1259 | * @access private
1260 | * @return The current templates chain
1261 | */
1262 | private function get_current_templates_chain() {
1263 | if (count($this->templates_chains_stack) == 0) {
1264 | return false;
1265 | }
1266 |
1267 | $retour = array_slice($this->templates_chains_stack, -1);
1268 | $retour = array_pop($retour);
1269 |
1270 | return $retour;
1271 | }
1272 |
1273 | /**
1274 | * Get the current root template
1275 | *
1276 | * @access private
1277 | * @return The current root template
1278 | */
1279 | private function get_current_root_template() {
1280 | $current_templates_chain = $this->get_current_templates_chain();
1281 |
1282 | if (($current_templates_chain === false)
1283 | || (count($current_templates_chain) == 0)
1284 | ) {
1285 | return false;
1286 | }
1287 |
1288 | return array_pop($current_templates_chain);
1289 | }
1290 |
1291 |
1292 |
1293 | /**
1294 | * Push the template $template in the current templates chain
1295 | *
1296 | * @access private
1297 | * @param $template
1298 | * @return void
1299 | */
1300 | private function push_templates_chain_item($template) {
1301 | if ( ! $this->is_template($template)) {
1302 | set_status_header(500);
1303 | exit('Layout error: Incorrect templates structure');
1304 | }
1305 |
1306 | $templates_chain = array_pop($this->templates_chains_stack);
1307 | array_push($templates_chain, $template);
1308 | array_push($this->templates_chains_stack, $templates_chain);
1309 |
1310 | $template_config = $this->template_config($template);
1311 | if ( ! is_null($template_config['parent_template'])) {
1312 | $this->push_templates_chain_item($template_config['parent_template']);
1313 | }
1314 | }
1315 |
1316 | /**
1317 | * Push the templates chain of the template $template in the templates chains stack $this->templates_chains_stack
1318 | *
1319 | * @access private
1320 | * @param $template
1321 | * @return void
1322 | */
1323 | private function push_templates_chain($template) {
1324 | array_push($this->templates_chains_stack, array());
1325 | $this->push_templates_chain_item($template);
1326 | }
1327 |
1328 | /**
1329 | * Include the template $template
1330 | *
1331 | * @access public
1332 | * @param $template
1333 | * @return void
1334 | */
1335 | public function include_template($template) {
1336 | $this->push_templates_chain($template);
1337 | $current_root_template = $this->get_current_root_template();
1338 | $this->include_snippet(APPPATH . 'templates' . DIRECTORY_SEPARATOR . $current_root_template . DIRECTORY_SEPARATOR . $current_root_template . '.php');
1339 | array_pop($this->templates_chains_stack);
1340 | }
1341 |
1342 | /**
1343 | * Define a block $block which should be implemented in a sub-template (but may also be implemented in the same template or inherited from an ancestor template)
1344 | *
1345 | * @access public
1346 | * @param $block
1347 | * @return void
1348 | */
1349 | public function block($block) {
1350 | foreach ($this->get_current_templates_chain() as $template) {
1351 | $file = APPPATH . 'templates' . DIRECTORY_SEPARATOR . $template . DIRECTORY_SEPARATOR . $block . '.php';
1352 |
1353 | if (file_exists($file)) {
1354 | $this->include_snippet($file);
1355 | return;
1356 | }
1357 | }
1358 | }
1359 |
1360 | /******************************************************************************/
1361 |
1362 | /*
1363 | |-------------------------------------------------------------------------------
1364 | | The "view" section
1365 | |-------------------------------------------------------------------------------
1366 | */
1367 |
1368 | /**
1369 | * Process the view $view
1370 | *
1371 | * @access private
1372 | * @param $view
1373 | * @param $data An associative array of data used in the view $view
1374 | * @param $autoloaded_assets An array defining the assets which have to be autoloaded (this array may only accept the values: 'css', 'js')
1375 | * @param $is_returned
1376 | * if true, the output of the view $view is returned
1377 | * if false, the output of the view $view is loaded in the section $section
1378 | * @param $section
1379 | * @return The output of the view $view if $is_returned is true and void otherwise
1380 | */
1381 | private function process_view($view, $data, $autoloaded_assets, $is_returned = true, $section = null) {
1382 | if (( ! is_string($view))
1383 | || empty($view)
1384 | ) {
1385 | show_error('Layout error: Incorrect view');
1386 | }
1387 |
1388 | if ( ! empty(array_diff($autoloaded_assets, array('css', 'js')))) {
1389 | show_error('Layout error: Incorrect parameter');
1390 | }
1391 |
1392 | if (in_array('css', $autoloaded_assets)) {
1393 | $this->add_css_uri('css/' . $view . '.css', 'local', [], [], true);
1394 | }
1395 |
1396 | if (in_array('js', $autoloaded_assets)) {
1397 | $this->add_js_uri('js/' . $view . '.js', 'local', [], [], true);
1398 | }
1399 |
1400 | if ($is_returned) {
1401 | return $this->CI->load->view($view, $data, true);
1402 | } else {
1403 | if ( ! isset($this->sections[$section])) {
1404 | $this->sections[$section] = '';
1405 | }
1406 | $this->sections[$section] .= $this->CI->load->view($view, $data, true);
1407 | }
1408 | }
1409 |
1410 | /**
1411 | * Load the output of the view $view in the section $section
1412 | *
1413 | * @access public
1414 | * @param $view
1415 | * @param $data An associative array of data used in the view $view
1416 | * @param $section
1417 | * @param $autoloaded_assets An array defining the assets which have to be autoloaded (this array may only accept the values: 'css', 'js')
1418 | * @return $this
1419 | */
1420 | public function load_view($view, $data = array(), $section = null, $autoloaded_assets = array()) {
1421 | if (is_null($section)) {
1422 | $section = $this->CI->config->item('layout_default_section');
1423 | }
1424 |
1425 | $this->process_view($view, $data, $autoloaded_assets, false, $section);
1426 | return $this;
1427 | }
1428 |
1429 | /**
1430 | * Return the output of the view $view
1431 | *
1432 | * @access public
1433 | * @param $view
1434 | * @param $data An associative array of data used in the view $view
1435 | * @param $autoloaded_assets An array defining the assets which have to be autoloaded (this array may only accept the values: 'css', 'js')
1436 | * @return The output of the view $view
1437 | */
1438 | public function return_view($view, $data = array(), $autoloaded_assets = array()) {
1439 | return $this->process_view($view, $data, $autoloaded_assets);
1440 | }
1441 |
1442 | /**
1443 | * Render the view $view with the defined template
1444 | *
1445 | * @access public
1446 | * @param $view
1447 | * @param $data An associative array of data used in the view $view
1448 | * @param $autoloaded_assets An array defining the assets which have to be autoloaded (this array may only accept the values: 'css', 'js')
1449 | * @return void
1450 | */
1451 | public function render_view($view, $data = array(), $autoloaded_assets = array()) {
1452 | $this->load_view($view, $data, $this->CI->config->item('layout_default_section'), $autoloaded_assets);
1453 |
1454 | $this->push_templates_chain($this->template);
1455 |
1456 | $current_root_template = $this->get_current_root_template();
1457 |
1458 | $this->CI->load->view('../templates/' . $current_root_template . '/' . $current_root_template, array('CI' => $this->CI));
1459 | }
1460 |
1461 | /**
1462 | * Render the view corresponding to the controller virtual action with the defined template
1463 | * 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)
1464 | *
1465 | * @access public
1466 | * @param $virtual_action The name of the virtual action
1467 | * @param $data An associative array of data used in the rendered view
1468 | * @return void
1469 | */
1470 | public function render_virtual_action_view($virtual_action, $data = array()) {
1471 | $directory = $this->CI->router->fetch_directory();
1472 | $controller = $this->CI->router->fetch_class();
1473 |
1474 | $this->add_css_uri('css/app.css', 'local', [], [], true);
1475 | $this->add_css_uri('css/controllers/' . $directory . $controller . '/controller.css', 'local', [], [], true);
1476 | $this->add_css_uri('css/controllers/' . $directory . $controller . '/actions/' . $virtual_action . '.css', 'local', [], [], true);
1477 |
1478 | $this->add_js_uri('js/app.js', 'local', [], [], true);
1479 | $this->add_js_uri('js/controllers/' . $directory . $controller . '/controller.js', 'local', [], [], true);
1480 | $this->add_js_uri('js/controllers/' . $directory . $controller . '/actions/' . $virtual_action . '.js', 'local', [], [], true);
1481 |
1482 | $this->render_view('controllers/' . $directory . $controller . '/actions/' . $virtual_action, $data);
1483 | }
1484 |
1485 | /**
1486 | * Note: This method should be called only in a controller action and if:
1487 | * - the name of this action matches the name of the view
1488 | * - the structure of the folder ./application/views is compliant with the requirements
1489 | * Render the view corresponding to the controller action with the defined template
1490 | * 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)
1491 | *
1492 | * @access public
1493 | * @param $data An associative array of data used in the rendered view
1494 | * @return void
1495 | */
1496 | public function render_action_view($data = array()) {
1497 | $action = $this->CI->router->fetch_method();
1498 |
1499 | $this->render_virtual_action_view($action, $data);
1500 | }
1501 |
1502 | /******************************************************************************/
1503 | }
1504 |
--------------------------------------------------------------------------------