├── .gitattributes
├── themes
└── mytheme.php
├── config
└── theme.php
├── README.md
└── libraries
└── Theme.php
/.gitattributes:
--------------------------------------------------------------------------------
1 | # Auto detect text files and perform LF normalization
2 | * text=auto
3 |
4 | # Custom for Visual Studio
5 | *.cs diff=csharp
6 | *.sln merge=union
7 | *.csproj merge=union
8 | *.vbproj merge=union
9 | *.fsproj merge=union
10 | *.dbproj merge=union
11 |
12 | # Standard to msysgit
13 | *.doc diff=astextplain
14 | *.DOC diff=astextplain
15 | *.docx diff=astextplain
16 | *.DOCX diff=astextplain
17 | *.dot diff=astextplain
18 | *.DOT diff=astextplain
19 | *.pdf diff=astextplain
20 | *.PDF diff=astextplain
21 | *.rtf diff=astextplain
22 | *.RTF diff=astextplain
23 |
--------------------------------------------------------------------------------
/themes/mytheme.php:
--------------------------------------------------------------------------------
1 |
2 |
3 |
31 |
32 |
33 |
34 |
35 |
36 | Sample Theme for CI-Theme
37 |
38 |
39 |
40 |
41 |
42 |
43 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/config/theme.php:
--------------------------------------------------------------------------------
1 |
54 |
55 |
56 |
57 | Sample Theme for CI-Theme
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 | ```
66 |
67 | ### Calling a themed page
68 |
69 | It's very simple. Have a look:
70 |
71 | ```
72 |
73 | $ci =& get_instance();
74 | $ci->load->library('theme');
75 | $ci->theme->load('mytheme', 'path/to/view');
76 |
77 | ```
78 |
79 | In the same way you can pass other parameters when loading a default codeigniter view file, you can pass another 2 additional params here.
80 |
81 | ```
82 |
83 | $ci->theme->load('mytheme', 'path/to/view', $vars, FALSE);
84 |
85 | ```
86 |
87 | The third parameter should be an Array with all variables that you want to forward to view.
88 |
89 | The fourth parameter, if TRUE, ensure that return won't be outputed for browser, but will be returned as string instead.
90 |
91 | ## Methods
92 |
93 | There some methods that can be used:
94 |
95 | ### set( name : string, value : array ) : void
96 |
97 | Sets a new variable data to be passed for theme output.
98 |
99 | ### load( theme : string, view : string, view_data : array, return : boolean ) : void|string
100 |
101 | Loads a view with a given theme.
102 |
103 | ### theme::set_css( css : string|array ) : void
104 |
105 | It's a static method.
106 |
107 | Sets CSS files to be outputed into theme files.
108 |
109 | ### theme::set_js( js : string|array ) : void
110 |
111 | It's a static method.
112 |
113 | Sets JS files to be outputed into theme files.
114 |
115 | ### theme::get_css( inline : boolean ) : array|string
116 |
117 | It's a static method.
118 |
119 | Get a list of CSS files.
120 |
121 | When inline param is TRUE, returns a string with all file names concatenated with comma (,).
122 |
123 | I.e:
124 |
125 | ```
126 | path/to/file1,path/to/file2, ...
127 | ```
128 |
129 | ### theme::get_js( inline : boolean ) : array|string
130 |
131 | It's a static method.
132 |
133 | Get a list of JS files.
134 |
135 | When inline param is TRUE, returns a string with all file names concatenated with comma (,).
136 |
137 | I.e:
138 |
139 | ```
140 | path/to/file1,path/to/file2, ...
141 | ```
142 |
143 | ### theme::set_metatag( element : array ) : void
144 |
145 | It's a static method.
146 |
147 | The CI-Theme allows you to manage the metatags that are placed into your HTML file.
148 |
149 | There are already a bunch of predefined metatags that you can use.
150 |
151 | This method set those metatags.
152 |
153 | The format of given element should be array( tag => '', content => '' ) and supported tags are:
154 |
155 | * name
156 | * property
157 | * http-equiv
158 |
159 | I.e:
160 |
161 | ```
162 |
163 | Theme::set_metatag( array('name' => 'robots', 'content' => 'all') ); // or
164 | Theme::set_metatag( array('property' => 'og:title', 'content' => 'My Title') ); // or
165 | Theme::set_metatag( array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8') );
166 |
167 | ```
168 |
169 | ### theme::set_title( str : string ) : void
170 |
171 | It's a static method.
172 |
173 | Set the metatag title.
174 |
175 | ### theme::set_description( str : string ) : void
176 |
177 | It's a static method.
178 |
179 | Set the metatag description.
180 |
181 | ### theme::set_keywords( piece : string|array ) : void
182 |
183 | It's a static method.
184 |
185 | Set the metatag keywords.
186 |
187 | ### theme::set_author( str : string ) : void
188 |
189 | It's a static method.
190 |
191 | Set the metatag author.
192 |
193 | ### theme::set_publisher( str : string ) : void
194 |
195 | It's a static method.
196 |
197 | Set the metatag publisher.
198 |
199 |
200 | ### theme::set_generator( str : string ) : void
201 |
202 | It's a static method.
203 |
204 | Set the metatag generator.
205 |
206 |
207 | ### theme::set_robots( piece : string|array ) : void
208 |
209 | It's a static method.
210 |
211 | Set the metatag robots.
212 |
213 |
214 | ### theme::set_opengraph( property : string, content : string) : void
215 |
216 | It's a static method.
217 |
218 | Set metatags for Open Graph.
219 |
220 |
221 | ### theme::set_fb_opengraph( property : string, content : string) : void
222 |
223 | It's a static method.
224 |
225 | Set metatags for Facebook Open Graph.
226 |
227 |
228 | ### theme::metatags( void ) : string
229 |
230 | It's a static method.
231 |
232 | Retrieve a string with all set metatags.
233 |
234 |
235 | ```
236 |
237 |
238 |
239 | ```
240 |
241 | And the result should be something like:
242 |
243 | ```
244 | Your title here
245 |
246 |
247 |
248 |
249 |
250 |
251 |
252 |
253 |
254 |
255 |
256 |
257 |
258 |
259 |
260 |
261 | ```
262 |
263 |
264 | ## Get involved
265 |
266 | Report bugs, make suggestions and get involved on contributions.
267 |
268 | Feel free to get in touch. ;)
269 |
270 | [](https://bitdeli.com/free "Bitdeli Badge")
--------------------------------------------------------------------------------
/libraries/Theme.php:
--------------------------------------------------------------------------------
1 | 'value') )
42 |
43 | function __construct()
44 | {
45 | $this->ci =& get_instance();
46 | $this->ci->config->load('theme', FALSE, TRUE);
47 |
48 | if ( $this->ci->config->item('output_var_name') )
49 | {
50 | $this->var_name = $this->ci->config->item('output_var_name');
51 | }
52 |
53 | if ( $this->ci->config->item('theme_path') )
54 | {
55 | $this->theme_path = $this->ci->config->item('theme_path');
56 | }
57 |
58 | $this->metatags = array(
59 |
60 | // facebook api
61 | 'property-og-type' => array('property' => 'fb:app_id', 'content' => ''),
62 | 'property-og-type' => array('property' => 'fb:profile_id', 'content' => ''),
63 |
64 | // open-graph
65 | 'property-og-type' => array('property' => 'og:type', 'content' => 'website'),
66 | 'property-og-title' => array('property' => 'og:title', 'content' => ''),
67 | 'property-og-description' => array('property' => 'og:description', 'content' => ''),
68 | 'property-og-image' => array('property' => 'og:image', 'content' => ''),
69 | 'property-og-site-name' => array('property' => 'og:site_name', 'content' => ''),
70 | 'property-og-url' => array('property' => 'og:url', 'content' => ''),
71 |
72 | // pre defined
73 | 'name-viewport' => array('name' => 'viewport', 'content' => 'width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no'),
74 | 'name-robots' => array('name' => 'robots', 'content' => 'index,follow,all'),
75 | 'http-equiv-content-type' => array('http-equiv' => 'Content-Type', 'content' => 'text/html; charset=utf-8'),
76 | 'http-equiv-content-style-type' => array('http-equiv' => 'Content-Style-Type', 'content' => 'text/css'),
77 |
78 | // basic html tags
79 | 'name-title' => array('name' => 'title', 'content' => ''),
80 | 'name-description' => array('name' => 'description', 'content' => ''),
81 | 'name-keywords' => array('name' => 'keywords', 'content' => ''),
82 | 'name-author' => array('name' => 'author', 'content' => ''),
83 | 'name-publisher' => array('name' => 'publisher', 'content' => ''),
84 | 'name-generator' => array('name' => 'generator', 'content' => ''),
85 |
86 | );
87 |
88 | }
89 |
90 | /**
91 | * Sets a new variable data to be passed for theme output
92 | * @param $name
93 | * @param $value
94 | * @return void
95 | */
96 | function set($name, $value)
97 | {
98 | $this->theme_data[$name] = $value;
99 | }
100 |
101 | /**
102 | * Loads a view with a given theme.
103 | * @param string $theme
104 | * @param string $view
105 | * @param array $view_data
106 | * @param boolean $return
107 | * @return void
108 | */
109 | function load($theme = '', $view = '' , $view_data = array(), $return = FALSE)
110 | {
111 | $this->set($this->var_name, $this->ci->load->view($view, $view_data, TRUE));
112 | return $this->ci->load->view($this->theme_path . $theme, $this->theme_data, $return);
113 | }
114 |
115 | /**
116 | * A private function to set assets for theme.
117 | * @return void
118 | */
119 | private static function set_asset( $asset = '', $group = 'css' )
120 | {
121 | $ci =& get_instance();
122 | $assets =& $ci->theme->assets;
123 |
124 | if (!isset( $assets[$group] ))
125 | {
126 | $assets[$group] = array();
127 | }
128 |
129 | if ( is_array($asset) )
130 | {
131 | foreach ($asset as $a)
132 | {
133 | if ( !in_array($a, $assets[$group]) )
134 | {
135 |
136 | $assets[$group][] = $a;
137 | }
138 | }
139 | }
140 | else
141 | {
142 | $assets[$group][] = $asset;
143 | }
144 |
145 | } // set_asset
146 |
147 | /**
148 | * A private function to get assets for theme.
149 | * @return void
150 | */
151 | private static function get_asset( $group = 'css' )
152 | {
153 | $ci =& get_instance();
154 | $assets = $ci->theme->assets;
155 |
156 | if ( !isset($assets[$group]) )
157 | {
158 | return FALSE;
159 | }
160 |
161 | return count($assets[$group]) ? $assets[$group] : FALSE;
162 |
163 | } // set_asset
164 |
165 | /**
166 | * Sets CSS files to be outputed into theme files.
167 | * @param variant $css - String or array(string)
168 | * @return void
169 | */
170 | public static function set_css( $css )
171 | {
172 | self::set_asset($css, 'css');
173 | }
174 |
175 | /**
176 | * Sets JS files to be outputed into theme files.
177 | * @param variant $js - String or array(string)
178 | * @return void
179 | */
180 | public static function set_js( $js )
181 | {
182 | self::set_asset($js, 'js');
183 | }
184 |
185 | /**
186 | * Get a list of CSS files
187 | * @return array(string)
188 | */
189 | public static function get_css( $inline = FALSE )
190 | {
191 | if ( $inline === TRUE )
192 | {
193 | $assets = self::get_asset('css');
194 | return count($assets) ? implode(',', $assets) : '';
195 | }
196 |
197 | return self::get_asset('css');
198 | }
199 |
200 | /**
201 | * Get a list of JS files
202 | * @return array(string)
203 | */
204 | public static function get_js( $inline = FALSE )
205 | {
206 | if ( $inline === TRUE )
207 | {
208 | $assets = self::get_asset('js');
209 | return count($assets) ? implode(',', $assets) : '';
210 | }
211 |
212 | return self::get_asset('js');
213 | }
214 |
215 | /**
216 | * Set metatags.
217 | * @param array $element - Should be array( [name|property|http-equiv] => '', content => '' )
218 | * @return void
219 | */
220 | public static function set_metatag( $element )
221 | {
222 | $ci =& get_instance();
223 |
224 | if ( is_array($element) )
225 | {
226 | krsort($element);
227 | $key = '';
228 |
229 | foreach ($element as $pk => $pv)
230 | {
231 | $pk = preg_replace(array('/\s/','/\:/','/\_/','/\./'), '-', $pk);
232 | $pv = preg_replace(array('/\s/','/\:/','/\_/','/\./'), '-', $pv);
233 | $key = strtolower("{$pk}-{$pv}");
234 | break;
235 | }
236 |
237 | $ci->theme->metatags[ $key ] = $element;
238 | }
239 |
240 | } // set_metatag
241 |
242 | /**
243 | * Retrieve the HTML portion with all metatags set.
244 | * @return string
245 | */
246 | public static function metatags()
247 | {
248 | $ci =& get_instance();
249 |
250 | $html = '';
251 | $ignore = array('name-title'); // what will be ignored in main loop
252 |
253 | // add title (the first meta inside head)
254 | // further add a tab before the tag
255 | $html .= PHP_EOL . "\t" . "{$ci->theme->metatags['name-title']['content']}" . PHP_EOL;
256 |
257 | foreach ($ci->theme->metatags as $mk => $mt)
258 | {
259 | // skip title, coz it uses a specific tag
260 | if ( in_array( $mk, $ignore ) ) continue;
261 |
262 | // skip metatags with empty content
263 | if ( !isset($mt['content']) || empty($mt['content']) ) continue;
264 |
265 | // create the tag, adding a tab in the begining of line.
266 | $html .= "\t" . ' $v)
269 | {
270 | $html .= "{$k}=\"{$v}\" ";
271 | }
272 |
273 | $html .= '>'.PHP_EOL;
274 | }
275 |
276 | return $html . PHP_EOL;
277 |
278 | } // metatags
279 |
280 | /**
281 | * Set the metatag title.
282 | * @param string $str
283 | * @return void
284 | */
285 | public static function set_title( $str )
286 | {
287 | self::set_metatag( array('name' => 'title', 'content' => $str) );
288 | self::set_opengraph('title', $str); // improve eficiency setting the opengraph tag
289 |
290 | } // set_title
291 |
292 | /**
293 | * Set the metatag description.
294 | * @param string $str
295 | * @return void
296 | */
297 | public static function set_description( $str )
298 | {
299 | self::set_metatag( array('name' => 'description', 'content' => $str) );
300 | self::set_opengraph('description', $str); // improve eficiency setting the opengraph tag
301 |
302 | } // set_description
303 |
304 | /**
305 | * Set the metatag keywords.
306 | * @param variant (string/array) $str
307 | * @return void
308 | */
309 | public static function set_keywords( $piece )
310 | {
311 | if ( is_array($piece) )
312 | {
313 | $piece = implode(',', $piece);
314 | }
315 |
316 | self::set_metatag( array('name' => 'keywords', 'content' => $piece) );
317 |
318 | } // set_keywords
319 |
320 | /**
321 | * Set the metatag author.
322 | * @param string $str
323 | * @return void
324 | */
325 | public static function set_author( $str )
326 | {
327 | self::set_metatag( array('name' => 'author', 'content' => $str) );
328 |
329 | } // set_author
330 |
331 | /**
332 | * Set the metatag publisher.
333 | * @param string $str
334 | * @return void
335 | */
336 | public static function set_publisher( $str )
337 | {
338 | self::set_metatag( array('name' => 'publisher', 'content' => $str) );
339 |
340 | } // set_publisher
341 |
342 | /**
343 | * Set the metatag generator.
344 | * @param string $str
345 | * @return void
346 | */
347 | public static function set_generator( $str )
348 | {
349 | self::set_metatag( array('name' => 'generator', 'content' => $str) );
350 |
351 | } // set_generator
352 |
353 | /**
354 | * Set the metatag robots.
355 | * @param variant (string/array) $str
356 | * @return void
357 | */
358 | public static function set_robots( $piece )
359 | {
360 | if ( is_array($piece) )
361 | {
362 | $piece = implode(',', $piece);
363 | }
364 |
365 | self::set_metatag( array('name' => 'robots', 'content' => $piece) );
366 |
367 | } // set_robots
368 |
369 | /**
370 | * Set metatags for Open Graph.
371 | * @param string $property
372 | * @param string $content
373 | * @return void
374 | */
375 | public static function set_opengraph( $property, $content = '' )
376 | {
377 | $property = preg_replace('/og\:/', '', $property);
378 | self::set_metatag( array('property' => "og:{$property}", 'content' => $content) );
379 |
380 | } // set_opengraph
381 |
382 | /**
383 | * Set metatags for Facebook Open Graph.
384 | * @param string $property
385 | * @param string $content
386 | * @return void
387 | */
388 | public static function set_fb_opengraph( $property, $content = '' )
389 | {
390 | $property = preg_replace('/fb\:/', '', $property);
391 | self::set_metatag( array('property' => "fb:{$property}", 'content' => $content) );
392 |
393 | } // set_fb_opengraph
394 |
395 | } // Theme
396 |
397 | /* End of file Theme.php */
398 | /* Location: ./application/libraries/Theme.php */
--------------------------------------------------------------------------------