Hello all,
I'm creating a template engine to replace placeholder strings in my html template with dynamic content served by a function. To do this I have written the following template processor:
1. function load_template()
2. {
3. $template_file = "template.html";
4. $template = fread(fopen($template_file, 'r'), filesize($template_file));
5. $template_vars = array('{MAIN}' => bodyContent(), '{NAV}' => navContent(), '{TITLE}' => $title);
6.
7. function template_eval (&$template, &$template_vars)
8. {
9. return strtr($template, $template_vars);
10. }
11.
12. echo template_eval($template, $template_vars);
13. }
14.
15. load_template();
which is called by a page like so:
1. <?php
2. $title = "Example Title";
3.
4. function navContent()
5. {
6. ?>
7. <h1>Page Links:</h1>
8. <ul>
9. <li><a href="">Link 1</a></li>
10. <li><a href="">Link 2</a></li>
11. <li><a href="">Link 3</a></li>
12. </ul>
13. <?php
14. }
15.
16. function bodyContent()
17. {
18. ?>
19.
20. <h1>Example Title 1</h1>
21. <p>Example Content 1</p>
22. <h1>Example Title 2</h1>
23. <p>Example Content 2</p>
24. <h1>Example Title 3</h1>
25. <p>Example Content 3</p>
26. <?php
27. }
28.
29. require_once (URL."/test_templateProcessor.php");
30. ?>
Unfortunately, the content fails to be placed into the appropriate place in the template but, rather, ends on top of the template as can be seen here: here.
I'm not quite sure why it does this, so any insight would be greatly appreciated. Thanks in advance for any and all help!
Jordan