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

    [man]strtr/man does character translation. You probably want to use [man]str_replace/man instead.

    By the way, instead of fread(fopen($template_file, 'r'), filesize($template_file)), use [man]file_get_contents/man.

      Thanks for the reply, I have changed my code to use str_replace() rather than strtr() as suggested:

      <?php
      function load_template()
      {
          $template_file = "template.html";
          $template = fread(fopen($template_file, 'r'), filesize($template_file));
          $template_vars = array('{MAIN}', '{NAV}', '{TITLE}');
          $template_vals = array(bodyContent(), navContent(), $title);
      
      function template_eval (&$template, &$template_vars, &$template_vals)
      {
          return str_replace($template_vars, $template_vals, $template);
      }
      
      echo template_eval($template, $template_vars, $template_vals);
      }
      
      load_template();
      ?>

      But get the same result as can be seen here.

      Thanks for your suggestion.

      ~Jordan

        Sorry, I got a little over zealous and didn't read your post in its entirety. I updated the code to use file_get_contents() rather than fread() but have the same result. (Although I did notice that the page loaded faster)

        <?php
        function load_template()
        {
            $template_file = "template.html";
            $template = file_get_contents($template_file);
            $template_vars = array('{MAIN}', '{NAV}', '{TITLE}');
            $template_vals = array(bodyContent(), navContent(), $title);
        
        function template_eval (&$template, &$template_vars, &$template_vals)
        {
            return str_replace($template_vars, $template_vals, $template);
        }
        
        echo template_eval($template, $template_vars, $template_vals);
        }
        
        load_template();
        ?>

        Thanks for the suggestions

        ~Jordan

          The problem is that navContent() and bodyContent() don't return anything, they output it instead. So as soon as you call them in load_template(), that text is sent out to the browser (and the functions return nothing). That happens long before the return value of template_eval() is echoed.

          What you're after here is heredoc syntax:

          return <<<EOF
          blah blah blah and another "blah".
          EOF;
          

          PS: strtr() would do the job, here.

            Write a Reply...