PageRenderTime 23ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/modules/user_guide/views/libraries/menu.php

https://github.com/Manuite/FUEL-CMS
PHP | 438 lines | 295 code | 44 blank | 99 comment | 14 complexity | 2a5e8857be5ac4d95ad26ca2e4d548d7 MD5 | raw file
  1. <h1>Menu Class</h1>
  2. <p>The Menu class is used to create hierchical html structures ideal for menu structures.</p>
  3. <h2>Initializing the Class</h2>
  4. <p>Like most other classes in CodeIgniter, the Menu class is initialized in your controller using the <dfn>$this->load->library</dfn> function:</p>
  5. <pre class="brush: php">$this->load->library('menu');</pre>
  6. <p>Alternatively, you can pass initialization parameters as the second parameter:</p>
  7. <pre class="brush: php">$this->load->library('menu', array('active_class'=>'on', 'render_type' => 'collapsible'));</pre>
  8. <h2>Configuring Validator Information</h2>
  9. <p>There are several public properties you can use to configure the Menu Class:</p>
  10. <table border="0" cellspacing="1" cellpadding="0" class="tableborder">
  11. <tbody>
  12. <tr>
  13. <th>Preference</th>
  14. <th>Default Value</th>
  15. <th>Options</th>
  16. <th>Description</th>
  17. </tr>
  18. <tr>
  19. <td><strong>active_class</strong></td>
  20. <td>active</td>
  21. <td>None</td>
  22. <td>The active css class</td>
  23. </tr>
  24. <tr>
  25. <td><strong>active</strong></td>
  26. <td>NULL</td>
  27. <td>None</td>
  28. <td>The active menu item</td>
  29. </tr>
  30. <tr>
  31. <td><strong>styles</strong></td>
  32. <td></td>
  33. <td></td>
  34. <td>CSS class styles to apply to menu items... can be a nested array</td>
  35. </tr>
  36. <tr>
  37. <td><strong>first_class</strong></td>
  38. <td>first</td>
  39. <td>None</td>
  40. <td>The css class for the first menu item</td>
  41. </tr>
  42. <tr>
  43. <td><strong>last_class</strong></td>
  44. <td>last</td>
  45. <td>None</td>
  46. <td>The css class for the last menu item</td>
  47. </tr>
  48. <tr>
  49. <td><strong>depth</strong></td>
  50. <td>0</td>
  51. <td>None</td>
  52. <td>The depth of the menu to render at</td>
  53. </tr>
  54. <tr>
  55. <td><strong>use_titles</strong></td>
  56. <td>FALSE</td>
  57. <td>TRUE/FALSE (boolean)</td>
  58. <td>Use the title attribute in the links</td>
  59. </tr>
  60. <tr>
  61. <td><strong>root_value</strong></td>
  62. <td>NULL</td>
  63. <td>NULL or 0</td>
  64. <td>The root parent value</td>
  65. </tr>
  66. <tr>
  67. <td><strong>container_tag</strong></td>
  68. <td>NULL</td>
  69. <td>For menus with a render_type property of basic, collapsible, or breadcrumb, the default is 'ul'.
  70. For menus with a render_type property of delimited is 'div'.
  71. For render_types of page_title or array, the container_tag property doesn't apply
  72. </td>
  73. <td>The html tag for the container of a set of menu items.</td>
  74. </tr>
  75. <tr>
  76. <td><strong>container_tag_attrs</strong></td>
  77. <td>None</td>
  78. <td>None>
  79. <td>HTML attributes for the container tag</td>
  80. </tr>
  81. <tr>
  82. <td><strong>container_tag_id</strong></td>
  83. <td>None</td>
  84. <td>None</td>
  85. <td>HTML container id</td>
  86. </tr>
  87. <tr>
  88. <td><strong>container_tag_class</strong></td>
  89. <td>None</td>
  90. <td>None</td>
  91. <td>HTML container class</td>
  92. </tr>
  93. <tr>
  94. <td><strong>cascade_selected</strong></td>
  95. <td>TRUE</td>
  96. <td>TRUE/FALSE (boolean)</td>
  97. <td>Cascade the selected items</td>
  98. </tr>
  99. <tr>
  100. <td><strong>include_hidden</strong></td>
  101. <td>FALSE</td>
  102. <td>TRUE/FALSE (boolean)</td>
  103. <td>Include menu items with the hidden attribute</td>
  104. </tr>
  105. <tr>
  106. <td><strong>item_tag</strong></td>
  107. <td>li</td>
  108. <td>None</td>
  109. <td>The html list item element</td>
  110. </tr>
  111. <tr>
  112. <td><strong>item_id_prefix</strong></td>
  113. <td>None</td>
  114. <td>None</td>
  115. <td>The prefix to the item id</td>
  116. </tr>
  117. <tr>
  118. <td><strong>item_id_key</strong></td>
  119. <td>id</td>
  120. <td>either 'id' or 'location'</td>
  121. <td>The key value to use for creating an id for an item</td>
  122. </tr>
  123. <tr>
  124. <td><strong>use_nav_key</strong></td>
  125. <td>FALSE</td>
  126. <td>TRUE/FALSE (boolean)</td>
  127. <td>Use the nav_key value to match active state instead of the id value</td>
  128. </tr>
  129. <tr>
  130. <td><strong>render_type</strong></td>
  131. <td>basic</td>
  132. <td>basic, breadcrumb, collapsible, page_title, delimited, array</td>
  133. <td>The type of menu to render</td>
  134. </tr>
  135. <tr>
  136. <td><strong>pre_render_func</strong></td>
  137. <td>NULL</td>
  138. <td>None</td>
  139. <td>function to apply to menu labels before rendering</td>
  140. </tr>
  141. <tr>
  142. <td colspan="4" class="hdr"><h4>Specific to Breadcrumb AND/OR Page Title Menus</h4></td>
  143. </tr>
  144. <tr>
  145. <td>delimiter</td>
  146. <td>For menus with a render_type property of page_title, or breadcrumb, the default is ' &amp;gt; '.
  147. For menus with a render_type property of delimited is ' &amp;nbsp;|&amp;nbsp; '.
  148. For render_types of basic, collapsible and array, the delimiter property doesn't apply</td>
  149. <td>None</td>
  150. <td>The HTML element between the links</td>
  151. </tr>
  152. <tr>
  153. <td>display_current</td>
  154. <td>TRUE</td>
  155. <td>TRUE/FALSE (boolean)</td>
  156. <td>Display the current active breadcrumb item?</td>
  157. </tr>
  158. <tr>
  159. <td>home_link</td>
  160. <td>Home</td>
  161. <td>None</td>
  162. <td>The root home link. If value is an array, the key of array is the link and the value is the label</td>
  163. </tr>
  164. <tr>
  165. <td colspan="4" class="hdr"><h4>Specific to Breadcrumb Menus</h4></td>
  166. </tr>
  167. <tr>
  168. <td>arrow_class</td>
  169. <td>arrow</td>
  170. <td>None</td>
  171. <td>The class for the arrows</td>
  172. </tr>
  173. <tr>
  174. <td colspan="4" class="hdr"><h4>Specific to Page TItle Display</h4></td>
  175. </tr>
  176. <tr>
  177. <td>order</td>
  178. <td>'asc'</td>
  179. <td>asc,desc</td>
  180. <td>The direction the page title should build</td>
  181. </tr>
  182. </tbody>
  183. </table>
  184. <h2>The Menu Array Structure</h2>
  185. <p>The menu array can hav the following key/value pair:</p>
  186. <ul>
  187. <li><strong>id</strong> - the id value of the menu item. If none is specified, then the array key value will be used</li>
  188. <li><strong>parent_id</strong> - the parent's id value that the menu item should reside under. Top level (also called root items), should have a root value of NULL or 0 depending on what is specified for the <dfn>root_value</dfn></li>
  189. <li><strong>label</strong> - the text label to display for the menu item</li>
  190. <li><strong>location</strong> - the link location of the menu item. If none is specified, the array key value will be used</li>
  191. <li><strong>attributes</strong> - additional HTML link attributes (e.g. target="_blank") to assign to the menu item</li>
  192. <li><strong>hidden</strong> - whether to display the menu item or not.</li>
  193. <li><strong>active</strong> - a regular expression that determines whether it should be given active status. If none is specified, it will be the same as the location value (e.g. about/history$|about/contact$).
  194. You can also use the <dfn>:children</dfn> active magic selector, which will highlight a menu item if it or any of it's sub pages are active.
  195. For example, if you have a news page, it allows you to easily highlight the news in the menu for all URI locations of news and news/{literal}{any}{/literal} (translates to news$|news/.+ in regular expression).
  196. </li>
  197. </ul>
  198. <p>Below is an common example:</p>
  199. <pre class="brush: php">
  200. $nav = array();
  201. $nav['about'] = 'About';
  202. $nav['about/history'] = array('label' => 'History', 'parent_id' => 'about');
  203. $nav['about/contact'] = array('label' => 'Contact', 'parent_id' => 'about');
  204. $nav['products'] = 'Products';
  205. $nav['products/X3000'] = array('label' => 'X3000', 'parent_id' => 'products');
  206. </pre>
  207. <h1>Function Reference</h1>
  208. <h2>$this->menu->render(<var>items</var>, <var>['active']</var>, <var>['parent_id']</var>, <var>'render_type'</var>)</h2>
  209. <p>Will create the HTML for the menu.
  210. The <dfn>$items</dfn> the array of items to display. See above for format of the array.
  211. The <dfn>$active</dfn> parameter specifies which menu item is active.
  212. The <dfn>$parent_id</dfn> parameter specifies where in the $items to start the heirarchy.
  213. The <dfn>$render_type</dfn> parameter is the type of menu you want to render (<dfn>basic</dfn>, <dfn>breadcrumb</dfn>, <dfn>collapsible</dfn>).
  214. </p>
  215. <pre class="brush: php">
  216. $nav = array();
  217. $nav['about'] = 'About';
  218. $nav['about/history'] = array('label' => 'History', 'parent_id' => 'about');
  219. $nav['about/contact'] = array('label' => 'Contact', 'parent_id' => 'about');
  220. $nav['products'] = 'Products';
  221. $nav['products/X3000'] = array('label' => 'X3000', 'parent_id' => 'products');
  222. $active = 'about/history';
  223. $menu = $this->menu->render($nav, $active, NULL, 'basic');
  224. echo $menu;
  225. /* echoed output looks something like this
  226. <ul>
  227. <li class="first active"><a href="/about" title="About">About</a>
  228. <ul>
  229. <li class="first"><a href="/about/history" title="History">History</a></li>
  230. <li class="last active"><a href="/about/contact" title="Contact">Contact</a></li>
  231. </ul>
  232. </li>
  233. <li class="last"><a href="/products" title="Products">Products</a></li>
  234. </ul>
  235. */
  236. </pre>
  237. <h2>$this->menu->render_collapsible(<var>items</var>, <var>['active']</var>, <var>['parent_id']</var>)</h2>
  238. <p>Will create the HTML for a collapsible menu.
  239. The <dfn>$items</dfn> the array of items to display. See above for format of the array.
  240. The <dfn>$active</dfn> parameter specifies which menu item is active.
  241. The <dfn>$parent_id</dfn> parameter specifies where in the $items to start the heirarchy
  242. </p>
  243. <pre class="brush: php">
  244. $nav = array();
  245. $nav['about'] = 'About';
  246. $nav['about/history'] = array('label' => 'History', 'parent_id' => 'about');
  247. $nav['about/contact'] = array('label' => 'Contact', 'parent_id' => 'about');
  248. $nav['products'] = 'Products';
  249. $active = 'about/history';
  250. $menu = $this->menu->render($nav, $active, NULL, 'collapsible');
  251. echo $menu;
  252. /* echoed output looks something like this. Note the difference in the product items not have a sub ul
  253. <ul>
  254. <li class="first active"><a href="/about" title="About">About</a>
  255. <ul>
  256. <li class="first"><a href="/about/history" title="History">History</a></li>
  257. <li class="last active"><a href="/about/contact" title="Contact">Contact</a></li>
  258. </ul>
  259. </li>
  260. <li class="last"><a href="/products" title="Products">Products</a></li>
  261. </ul>
  262. */
  263. </pre>
  264. <h2>$this->menu->render_breadcrumb(<var>items</var>, <var>['active']</var>, <var>['parent_id']</var>)</h2>
  265. <p>Will create the HTML for a breadcrumb menu.
  266. The <dfn>$items</dfn> the array of items to display. See above for format of the array.
  267. The <dfn>$active</dfn> parameter specifies which menu item is active.
  268. The <dfn>$parent_id</dfn> parameter specifies where in the $items to start the heirarchy
  269. </p>
  270. <pre class="brush: php">
  271. $nav = array();
  272. $nav['about'] = 'About';
  273. $nav['about/history'] = array('label' => 'History', 'parent_id' => 'about');
  274. $nav['about/contact'] = array('label' => 'Contact', 'parent_id' => 'about');
  275. $nav['products'] = 'Products';
  276. $active = 'about/history';
  277. $menu = $this->menu->render($nav, $active, NULL, 'breadcrumb');
  278. echo $menu;
  279. /* echoed output looks something like this.
  280. <ul>
  281. <li><a href="/">Home</a> <span class="arrow"> &gt; </span> </li>
  282. <li><a href="/about">About</a> <span class="arrow"> &gt; </span> </li>
  283. <li>History</li>
  284. </ul>
  285. */
  286. </pre>
  287. <h2>$this->menu->render_page_title(<var>items</var>, <var>['active']</var>, <var>['parent_id']</var>)</h2>
  288. <p>Will create a page title based on the nav structure.
  289. The <dfn>$items</dfn> the array of items to display. See above for format of the array.
  290. The <dfn>$active</dfn> parameter specifies which menu item is active.
  291. The <dfn>$parent_id</dfn> parameter specifies where in the $items to start the heirarchy
  292. </p>
  293. <pre class="brush: php">
  294. $nav = array();
  295. $nav['about'] = 'About';
  296. $nav['about/history'] = array('label' => 'History', 'parent_id' => 'about');
  297. $nav['about/contact'] = array('label' => 'Contact', 'parent_id' => 'about');
  298. $nav['products'] = 'Products';
  299. $active = 'about/history';
  300. $menu = $this->menu->render($nav, $active, NULL, 'page_title');
  301. echo $menu;
  302. /* echoed output looks something like this.
  303. Home &gt; About &gt; History
  304. */
  305. </pre>
  306. <h2>$this->menu->render_array(<var>items</var>, <var>['active']</var>, <var>['parent_id']</var>)</h2>
  307. <p>Will output a nested array. The key of <dfn>children</dfn> will contain any child menu items.
  308. The <dfn>$items</dfn> the array of items to display. See above for format of the array.
  309. The <dfn>$active</dfn> parameter specifies which menu item is active.
  310. The <dfn>$parent_id</dfn> parameter specifies where in the $items to start the heirarchy
  311. </p>
  312. <pre class="brush: php">
  313. $nav = array();
  314. $nav['about'] = 'About';
  315. $nav['about/history'] = array('label' => 'History', 'parent_id' => 'about');
  316. $nav['about/contact'] = array('label' => 'Contact', 'parent_id' => 'about');
  317. $nav['products'] = 'Products';
  318. $active = 'about/history';
  319. $menu = $this->menu->render($nav, $active, NULL, 'array');
  320. print_r($menu);
  321. /* echoed output looks something like this.
  322. Array
  323. (
  324. [about] => Array
  325. (
  326. [id] => about
  327. [label] => About
  328. [location] => about
  329. [attributes] => Array
  330. (
  331. )
  332. [active] =>
  333. [parent_id] =>
  334. [hidden] =>
  335. [children] => Array
  336. (
  337. [about/history] => Array
  338. (
  339. [id] => about/history
  340. [label] => History
  341. [location] => about/history
  342. [attributes] => Array
  343. (
  344. )
  345. [active] =>
  346. [parent_id] => about
  347. [hidden] =>
  348. )
  349. [about/wcontact] => Array
  350. (
  351. [id] => about/contact
  352. [label] => Contact
  353. [location] => about/contact
  354. [attributes] => Array
  355. (
  356. )
  357. [active] =>
  358. [parent_id] => about
  359. [hidden] =>
  360. )
  361. )
  362. )
  363. [products] => Array
  364. (
  365. [id] => products
  366. [label] => Products
  367. [location] => products
  368. [attributes] => Array
  369. (
  370. )
  371. [active] =>
  372. [parent_id] =>
  373. [hidden] =>
  374. )
  375. )
  376. */
  377. </pre>
  378. <h2>$this->menu->reset()</h2>
  379. <p>Will reset the parameters back to their default values. Good to use when multiple menus are generated.</p>
  380. <pre class="brush: php">
  381. $menu = $this->menu->clear();
  382. </pre>