Guest User

Untitled

a guest
Apr 24th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.16 KB | None | 0 0
  1. <?php
  2. Class Page {
  3.  
  4. var $template;
  5. var $partials;
  6.  
  7. #
  8. # this faked data will be dynamically created (by searching through and parsing the /content folder) and stored within the Page class
  9. #
  10. var $data = array(
  11. '$false' => false,
  12. '$true' => true,
  13. '$pages' => array(
  14. array(
  15. '/@name/' => 'About',
  16. '/@url/' => './about-me/'
  17. ),
  18. array(
  19. '/@name/' => 'Contact',
  20. '/@url/' => './contact/'
  21. )
  22. ),
  23. '$category_list' => array(
  24. array(
  25. '/@name/' => 'Projects',
  26. '/@url/' => './projects/',
  27. '$children' => array(
  28. array(
  29. '/@name/' => 'Project 1',
  30. '/@url/' => './projects/project-1'
  31. ),
  32. array(
  33. '/@name/' => 'Project 2',
  34. '/@url/' => './projects/project-2',
  35. '$children' => array(
  36. array(
  37. '/@name/' => 'Project 2 Movies',
  38. '/@url/' => './projects/project-2/movies/',
  39. '$children' => array(
  40. array(
  41. '/@name/' => 'Distant child',
  42. '/@url/' => './projects/project-2/movies/long-lost-child'
  43. ),
  44. array(
  45. '/@name/' => 'Help, I am alive too!',
  46. '/@url/' => './projects/project-2/movies/im-alive/'
  47. )
  48. )
  49. ),
  50. array(
  51. '/@name/' => 'Project 2 Flash Animations',
  52. '/@url/' => './projects/project-2/swfs/',
  53. )
  54. )
  55. )
  56. )
  57. ),
  58. array(
  59. '/@name/' => 'Blog',
  60. '/@url/' => './blog/',
  61. '$children' => array(
  62. array(
  63. '/@name/' => 'Entry 1',
  64. '/@url/' => './blog/entry-1'
  65. )
  66. )
  67. )
  68. )
  69. );
  70.  
  71. function __construct() {
  72. # store fake templates
  73. $this->template = $GLOBALS['template'];
  74. # store fake partials
  75. $this->partials = $GLOBALS['partials'];
  76. $this->render();
  77. }
  78.  
  79. static function content_vars($content) {
  80. $replacement_pairs = array();
  81. # extract key/values from content file
  82. preg_match_all('/[\w\d_-]+?:[\S\s]*?\n\n/', $content, $matches);
  83. foreach($matches[0] as $match) {
  84. $colon_split = explode(':', $match);
  85. $replacement_pairs['/@'.$colon_split[0].'/'] = trim($colon_split[1]);
  86. }
  87. return $replacement_pairs;
  88. }
  89.  
  90. function render() {
  91. #
  92. # This section fakes stacey's content file parser
  93. #
  94.  
  95. # pull out each key/value pair from the content file and combine with the Faked page data
  96. $data = array_merge($this->data, $this->content_vars($GLOBALS['content_file']."\n\n"));
  97.  
  98. # replace standard content variables
  99. $parsed_content = Partial::parse($data, $this->template);
  100.  
  101. # render final output
  102. echo $parsed_content;
  103. }
  104.  
  105. }
  106.  
  107. Class Partial {
  108.  
  109. static function get_partial_template($name) {
  110. return $GLOBALS['partials'][$name];
  111. // return file_get_contents('../templates/partials'.$name.'.html');
  112. }
  113.  
  114. static function parse($data, $template) {
  115. $template = Partial::parse_if($data, $template);
  116. $template = Partial::parse_foreach($data, $template);
  117. $template = Partial::parse_includes($data, $template);
  118. $template = Partial::parse_vars($data, $template);
  119. return $template;
  120. }
  121.  
  122. static function parse_if($data, $template) {
  123.  
  124. # match any inner if statements
  125. preg_match('/([\S\s]*?)if\s+?(\$.+?)\?\s+?do([\S\s]+?)endif([\S\s]*)$/', $template, $template_parts);
  126.  
  127. if(!empty($template_parts)) {
  128. # Run the replacements on the pre-"if" part of the partial
  129. $template = Partial::parse($data, $template_parts[1]);
  130.  
  131. # If the condition is true
  132. if(isset($data[$template_parts[2]]) && ($data[$template_parts[2]])) {
  133. # Parse the block inside the if statement
  134. $template .= Partial::parse($data, $template_parts[3]);
  135. }
  136.  
  137. # Run the replacements on the post-"if" part of the partial
  138. $template .= Partial::parse($data, $template_parts[4]);
  139.  
  140. }
  141. return $template;
  142. }
  143.  
  144. static function parse_foreach($data, $template) {
  145. # Split out the partial into the parts Before, Inside, and After the foreach loop
  146. preg_match('/([\S\s]*?)foreach[\s]+?(\$.+?)[\s]+?do([\S\s]+)end([\S\s]*)$/', $template, $template_parts);
  147. if(!empty($template_parts)) {
  148. # Run the replacements on the pre-"foreach" part of the partial
  149. $template = Partial::parse($data, $template_parts[1]);
  150.  
  151. # Traverse one level deeper into the data hierachy
  152. $pages = $data[$template_parts[2]];
  153. if ($pages) {
  154. foreach ($pages as $data_item) {
  155. # Recursively parse the inside part of the foreach loop
  156. $template .= Partial::parse($data_item, $template_parts[3]);
  157. }
  158. }
  159.  
  160. # Run the replacements on the post-"foreach" part of the partial
  161. $template .= Partial::parse($data, $template_parts[4]);
  162. }
  163. return $template;
  164. }
  165.  
  166. static function parse_includes($data, $template) {
  167. # Split out the partial into the parts Before, Inside, and After the :include
  168. preg_match('/([\S\s]*?):([\w\d-_]+?)(\.html)?\b([\S\s]*)$/', $template, $template_parts);
  169.  
  170. ###### TODO: There is no protection against endless loops due to circular inclusions
  171. if (!empty($template_parts)) {
  172. # Run the replacements on the pre-":include" part of the partial
  173. $template = Partial::parse($data, $template_parts[1]);
  174.  
  175. # Parse the included template
  176. $inner_template = Partial::get_partial_template($template_parts[2]);
  177. $template .= Partial::parse($data, $inner_template);
  178.  
  179. # Run the replacements on the post-":include" part of the partial
  180. $template .= Partial::parse($data, $template_parts[4]);
  181. }
  182. return $template;
  183. }
  184.  
  185. static function parse_vars($data, $template) {
  186. # Split out the partial into the parts Before, Inside, and After the @var
  187. preg_match('/([\S\s]*?)(\@[\w\d-_]+?)\b([\S\s]*)$/', $template, $template_parts);
  188.  
  189. if (!empty($template_parts)) {
  190. # Run the replacements on the pre-"@var" part of the partial
  191. $template = Partial::parse($data, $template_parts[1]);
  192.  
  193. # Replace the @var
  194. $var = $data['/'.$template_parts[2].'/'];
  195. if ($var && is_string($var)) {
  196. $template .= $var;
  197. }
  198.  
  199. # Run the replacements on the post-"@var" part of the partial
  200. $template .= Partial::parse($data, $template_parts[3]);
  201. }
  202. return $template;
  203. }
  204.  
  205.  
  206. }
  207.  
  208. # ----------------------------------------------------------
  209.  
  210. #
  211. #
  212. # Faked Content file
  213. #
  214. #
  215.  
  216. $content_file = <<<EOS
  217.  
  218. title: Stacey, new partials (proof of concept)
  219.  
  220. EOS;
  221.  
  222. #
  223. #
  224. # Faked Template
  225. #
  226. #
  227.  
  228. $template = <<<EOS
  229. <html>
  230. <head>
  231. <title>@title</title>
  232. </head>
  233. <body>
  234. <h1>@title</h1>
  235. <div id="container">
  236. :pages
  237. <hr>
  238. :category_list
  239. <hr>
  240. if \$true? do
  241. :footer.html
  242. endif
  243. </div>
  244. </body>
  245. </html>
  246. EOS;
  247.  
  248. #
  249. #
  250. # Faked Partials
  251. #
  252. #
  253.  
  254. $pages_partial_html = <<<EOS
  255. <ol id="wrapper">
  256. foreach \$pages do
  257. <li>:link</li>
  258. end
  259. </ol>
  260. EOS;
  261.  
  262. $categories_partial_html = <<<EOS
  263. <ol id="wrapper">
  264. foreach \$category_list do
  265. :link
  266. :children
  267. end
  268. </ol>
  269. EOS;
  270.  
  271. $footer_partial_html = <<<EOS
  272. <div id="footer">
  273. <p>Footer.</p>
  274. </div>
  275. EOS;
  276.  
  277. $link_partial_html = <<<EOS
  278. <a class="page-link" href="@url">@name</a>
  279. EOS;
  280.  
  281. $children_partial_html = <<<EOS
  282. if \$children? do
  283. <ol class="children" title="Children of @url">
  284. foreach \$children do
  285. <li>
  286. :link
  287. :children
  288. </li>
  289. end
  290. </ol>
  291. endif
  292. EOS;
  293.  
  294. #
  295. # this variable will be created by looping through each file within /templates/partials
  296. #
  297. $partials = array(
  298. 'pages' => $pages_partial_html,
  299. 'category_list' => $categories_partial_html,
  300. 'footer' => $footer_partial_html,
  301. 'children' => $children_partial_html,
  302. 'link' => $link_partial_html
  303.  
  304. );
  305.  
  306. #
  307. #
  308. # Init
  309. #
  310. #
  311.  
  312. $p = new Page;
  313.  
  314. ?>
Add Comment
Please, Sign In to add comment