PageRenderTime 26ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/system/libraries/Template.php

https://bitbucket.org/flyingdog/kennel
PHP | 200 lines | 131 code | 36 blank | 33 comment | 22 complexity | fc86f69b1d84394f00a5d83f10f7f7e4 MD5 | raw file
  1. <?php
  2. /**
  3. * TODO i18n
  4. */
  5. class Template extends View
  6. {
  7. // Static variables
  8. const DOCTYPE_HTML5 = '<!doctype html>';
  9. const MODERNIZR = 'modermozr-1.5.min.js';
  10. const PNGFIX = 'dd_belatedpng-0.0.8a.min.js';
  11. const RAPHAEL = 'raphael-min.js';
  12. const JQUERY = 'jquery-1.9.1.min.js';
  13. const BOOTSTRAP = 'bootstrap.min.css';
  14. static $INSTANCE = null;
  15. // Variables
  16. var $lang = '';
  17. var $dir = 'ltr';
  18. var $favicon = null;
  19. var $title = null;
  20. var $titleSeparator = '-';
  21. var $bodyClass = null;
  22. var $bodyId = null;
  23. var $ngApp = null;
  24. var $base = null;
  25. // Structure
  26. private $_html;
  27. private $_head;
  28. private $_body;
  29. // Meta
  30. private $_meta = array();
  31. // Links
  32. private $_links = array();
  33. // Resources
  34. private $_stylesheets = array();
  35. private $_scripts = array();
  36. private $_footScripts = array();
  37. // Template View
  38. private $_template;
  39. function __construct($template)
  40. {
  41. $this->_template = new View($template, $this);
  42. self::$INSTANCE = $this;
  43. }
  44. static function getInstance($view_name=null)
  45. {
  46. if (!self::$INSTANCE)
  47. {
  48. if ($view_name) self::$INSTANCE = new self($view_name);
  49. else return debug::error('Template::getInstance(null): no instance created yet');
  50. }
  51. return self::$INSTANCE;
  52. }
  53. // Ovewriting normal View behavior
  54. //////////////////////////////////
  55. private function _getOutput()
  56. {
  57. // Make $template and $title accessible in the descendant views
  58. $this->__set('template', $this);
  59. $this->__set('title', $this->title);
  60. // Template View (must run first since child views may call functions
  61. // from $template)
  62. $templateView = XML::text($this->_template->__toString());
  63. // TODO: review this
  64. // <html>
  65. $this->_html = XML::element('html');
  66. $this->_html->dir =$this->dir;
  67. if (Kennel::getSetting('i18n', 'enabled'))
  68. $this->_html->lang = i18n::getLang();
  69. // <head>
  70. $this->_head = XML::element('head', $this->_html);
  71. // <title>
  72. $title = XML::element('title', $this->_head);
  73. $title->setValue($this->getTitle());
  74. // favicon
  75. if ($this->favicon)
  76. $this->_head->adopt(html::favicon($this->favicon));
  77. // Content Type
  78. $this->_head->adopt(html::meta(array( 'charset'=>'utf-8' )));
  79. // Base
  80. if ($this->base) {
  81. $base = XML::element('base', $this->_head, $this->base);
  82. $base->self_closing = XML::SELF_CLOSING_HTML;
  83. }
  84. // <meta>
  85. foreach ($this->_meta as $meta)
  86. $this->_head->adopt(html::meta($meta));
  87. // <link>
  88. foreach ($this->_links as $link)
  89. $this->_head->adopt(html::link($link['rel'], $link['href'], $link['type'], $link['title']));
  90. // Header <style>
  91. $this->_head->adopt(html::css($this->_stylesheets));
  92. // HTML5 Shiv
  93. XML::text('<!--[if lt IE 9]><script src="system/assets/js/html5shiv.js"></script><![endif]-->', $this->_head);
  94. // Header <script>
  95. $this->_head->adopt(html::js($this->_scripts));
  96. // <body>
  97. $this->_body = XML::element('body', $this->_html);
  98. $this->_body->class = browser::css();
  99. if (Kennel::getSetting('i18n', 'enabled'))
  100. $this->_body->class .= ' ' . i18n::getLang();
  101. if ($this->bodyClass) $this->_body->class .= ' ' . $this->bodyClass;
  102. if ($this->bodyId) $this->_body->id = $this->bodyId;
  103. if ($this->ngApp) $this->_body->set('ng-app', $this->ngApp);
  104. // Inject the Template View
  105. $this->_body->adopt($templateView);
  106. // Foot <script>
  107. $this->_body->adopt(html::js($this->_footScripts));
  108. // Return the whole shebang
  109. return self::DOCTYPE_HTML5 . $this->_html->output(true);
  110. }
  111. function render()
  112. {
  113. header('content-type: text/html; charset:UTF-8');
  114. print $this->_getOutput();
  115. }
  116. // Aditional utility methods
  117. ////////////////////////////
  118. function getTitle()
  119. {
  120. if ($this->title && Kennel::getSetting('application', 'title'))
  121. return $this->title . " {$this->titleSeparator} " . Kennel::getSetting('application', 'title');
  122. elseif (!$this->title && Kennel::getSetting('application', 'title'))
  123. return Kennel::getSetting('application', 'title');
  124. elseif ($this->title && !Kennel::getSetting('application', 'title'))
  125. return $this->title;
  126. else
  127. return 'Untitled';
  128. }
  129. function meta($name_or_properties, $content=null)
  130. {
  131. if ( is_array($name_or_properties) ) {
  132. $this->_meta[] = $name_or_properties;
  133. } else {
  134. $this->_meta[] = array('name'=>$name_or_properties, 'content'=>esc::attr($content));
  135. }
  136. }
  137. function link($rel, $type, $href, $title=null)
  138. {
  139. $this->_links[] = array('rel'=>$rel, 'type'=>$type, 'href'=>$href, 'title'=>$title);
  140. }
  141. function css()
  142. {
  143. $arguments = func_get_args();
  144. foreach ($arguments as $stylesheet)
  145. if (!in_array($stylesheet, $this->_stylesheets))
  146. $this->_stylesheets[] = $stylesheet;
  147. }
  148. function js()
  149. {
  150. $arguments = func_get_args();
  151. $lastArg = $arguments[count($arguments)-1];
  152. if (is_bool($lastArg) && $lastArg === true) {
  153. array_pop($arguments);
  154. foreach ($arguments as $script)
  155. if (!in_array($script, $this->_scripts))
  156. $this->_footScripts[] = $script;
  157. } else {
  158. foreach ($arguments as $script)
  159. if (!in_array($script, $this->_scripts))
  160. $this->_scripts[] = $script;
  161. }
  162. }
  163. }
  164. ?>