0

I am using (or at least tying to) PHP HEREDOC function as a templating engine. I have implemented external caller string that can directly process external functions in HEREDOC, and that works successfully.

The problem I am facing now is that the order of certain functions appear to take precedence and execute first, regardless of other functions and/or code inside the specific HEREDOC.

How to fix that?

(Please note I am a PHP beginner. I have done my homework, but couldn't find a solution. Thanks.)

FUNCTION PROCESOR:

function heredoc($input)
    {
    return $input;
    }
    $heredoc = "heredoc";

HEREDOC TEMPLATE:

function splicemaster_return_full_page()
    {
    global $heredoc;
    $title ="This is document title";
    echo <<<HEREDOC
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
{$heredoc(splice_html_title($title))}
</head>
<body>
{$heredoc(splicemaster_return_message())}
{$heredoc(splice_quick_add_article_form())}
{$heredoc(display_all_articles_in_a_html_table())}
</body>
</html>
HEREDOC;
    }

The issue at hand is with "{$heredoc(display_all_articles_in_a_html_table())}" call, which outputs before everything else, resulting in a broken HTML.

Any help appreciated, I am banging my head with this for quite a while now.

UPDATE:

using stuff posted in comments i tried to do something else, but this is ugly as hell, and I would have issues editing this at later date.

function testout()
    {
    $title = "This is document title";

echo "<!DOCTYPE html>";
echo '<html lang="en">';
echo     "<head>";
echo       '<meta charset="utf-8">';
echo         "<title>". $title . "</title>";
echo     "</head>";
echo     "<body>";
echo splicemaster_return_message();
echo splice_quick_add_article_form();
echo display_all_articles_in_a_html_table();
echo     "</body>";
echo "</html>";

    }

(How it looks like is not important - I have a HTML processor function.)

UPDATE 2

OK, so I found "dirty" fix, tho that doesn't explain why the engine works as it does. (I also tested on another machine, with diff. php):

function splicemaster_return_full_page()
    {
    global $heredoc;
    $title ="This is document title";

    echo <<<HEREDOC
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
{$heredoc(splice_html_title($title))}
</head>
<body>
{$heredoc(splicemaster_return_message())}
{$heredoc(splice_quick_add_article_form())}
HEREDOC;
    echo <<<HEREDOC
{$heredoc(display_all_articles_in_a_html_table())}
</body>
</html>
HEREDOC;
    }
mrmut
  • 484
  • 5
  • 18
  • I would suggest not using HEREDOC for templating entire pages to be honest.. use OB and load templates... to be honest, why not look at using a framework for your programming as all these issues will have already been resolved. – RaggaMuffin-420 Oct 02 '14 at 17:03
  • Thanks for a re; What is "OB"? Apart from that, frameworks are not my stuff, at least not for this project. (And yes, you can call functions in HEREDOC, you just need to "rename" the functions a s a string.) – mrmut Oct 02 '14 at 17:15

2 Answers2

0

You shouldn't be using heredoc here. Or really be trying to render an entire html document within a function. This is how html should be rendered with php. Note: I'm also pretty sure you can't call functions in a heredoc statement.

<?php $title = "This is document title"; ?>
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <?php echo splice_html_title($title); ?>
    </head>
    <body>
        <?php
            echo splicemaster_return_message()
               . splice_quick_add_article_form()
               . display_all_articles_in_a_html_table();
        ?>
    </body>
</html>

You can see how much cleaner this is, which makes it much easier to edit, when needed. You just put this in a file 'page.php' for example.

include_once('page.php');

And include it where ever you would call that function splicemaster_return_full_page.

Evadecaptcha
  • 1,403
  • 11
  • 17
  • I am trying to build "html snippets" that I would just output as my flow control processes the requests/page. I am building a one-file small CMS. Now, this works, but the breaking point seems to be how to output stuff. I guess I do have an option of calling external files, but I hoped I could do it this way. -- Working in a way you suggest would break my PHP, I think, as the page is not processed linearly, but as a program. (Tho I will try this, don't have a clue will it work or not.) – mrmut Oct 02 '14 at 17:12
  • OK, so I did try this. - I don't like this constant going in and out of a program. Makes code hard to manage! :-( Other thing is that the concatenated echo outputted the LAST function FIRST?? -- It worked only if I output it echo by echo (echo for each function call). – mrmut Oct 02 '14 at 17:21
  • I don't think echoing the last function first is possible. – Evadecaptcha Oct 02 '14 at 17:23
  • Those should be include() too anyway. You should only be handling things programatically if they require variables of some kind. – Evadecaptcha Oct 02 '14 at 17:24
  • Evade Captcha: that Is why I thought, but it does do that. -- Can you explain in more detail what you wanted to say in your second comment? I don't understand. – mrmut Oct 02 '14 at 17:29
  • Basically, you would have a file that generates the splicemaster_return_message and output it. Then here you would just include that file: include_once('return_message.php'); keeping your main html display page clean, and allowing you to implement these factors on any page with include_once('return_message.php'); – Evadecaptcha Oct 02 '14 at 17:43
  • Ah, I understand. Essentially I would be creating external template files. Yeah, that would work, too, tho I wanted to skip all that with HEREDOC templating. I think that is what I'll do in the end, but not if I could figure this out. – mrmut Oct 02 '14 at 17:51
0

I asked this (similar) question on other site while seeking why this happens, and found the culprit.

The problem was in called functions that echo (or print) output, instead returning it. When I switched to return, the code outputs appropriately.

mrmut
  • 484
  • 5
  • 18