The code below is a sample code of the top oh my php web page. There are php variables being outputted in specific places.
I'd to implement an HTML to PDF converter, but it requires me to put all of my code into a single variable that the PDF converter will use in its class. How can I put my existing into into a single variable say: $html
without having to open up all my PHP variables, escpaing everything and concatenating the whole place? I was thinking of using heredoc
syntax but it doesn't like the <?php ?>
and I'm sort of confused as I've never used it in the past. Any ideas on how to achieve this?
Ideally, this is what I'd like to do:
$html = <<<EOD
<div id="topHeaderView"><?php echo configuration::getValue(6); ?></div>
<table>
<tr>
<td><?php echo $lang["FAI_R"]["PRT"]["TITLE"]["HEADER"]; ?></td>
</tr>
EOD;
The above doesn't capture any values outputted by $lang["FAI_R"]["PRT"]["TITLE"]["HEADER"]
or by configuration::getValue(6)
.
Insead of:
$html = "";
$html .= "<div id=\"topHeaderView\">".configuration::getValue(6)."</div>";
$html .= "<table>";
$html .= "<tr>";
$html .= "<td>".$lang["FAI_R"]["PRT"]["TITLE"]["HEADER"]."</td>";
$html .= "</tr>";
This is something I want to avoid...
This is a good use of output buffering
ob_start();
?><div id="topHeaderView"><?php echo configuration::getValue(6); ?></div>
<table>
<tr>
<td><?php echo $lang["FAI_R"]["PRT"]["TITLE"]["HEADER"]; ?></td>
</tr>
<?php
$html = ob_get_clean();
As far as I can see in the manual, it is not possible to call functions inside HEREDOC. A less cumbersome solution is:
$config_print = configuration::getValue(6);
$lang_print = $lang["FAI_R"]["PRT"]["TITLE"]["HEADER"];
$html = <<<EOD
<div id="topHeaderView">$config_print</div>
<table>
<tr>
<td>$lang_print</td>
</tr>
EOD;
Edit: Or you could use:
$html = <<<EOD
<div id="topHeaderView"><?= _( configuration::getValue(6) ); ?></div>
<table>
<tr>
<td><?= _( $lang["FAI_R"]["PRT"]["TITLE"]["HEADER"] ); ?></td>
</tr>
EOD;
heredoc
is php syntax therefore it needs to be inside the php tags. The php documentation, here, explains the behavior of variables within heredoc
strings:
Heredoc text behaves just like a double-quoted string, without the double quotes. This means that quotes in a heredoc do not need to be escaped... Variables are expanded, but the same care must be taken when expressing complex variables inside a heredoc as with strings.
There are also some examples in the documentation.
<?php
$value = configuration::getValue(6);
$header = $lang["FAI_R"]["PRT"]["TITLE"]["HEADER"];
$html = <<<EOD
<div id="topHeaderView">$value</div>
<table>
<tr>
<td>$header</td>
</tr>
EOD;
?>
The manual has a whole chapter devoted to the assorted string syntaxes that PHP provides (4 to date). You're basically missing string interpolation:
$html = <<<EOD
<div id="topHeaderView">$value</div>
<table>
<tr>
<td>{$lang["FAI_R"]["PRT"]["TITLE"]["HEADER"]}</td>
</tr>
EOD;
However, it isn't as simple as that. You are using PHP to generate code in another language (HTML) and you need to ensure that the resulting code is valid. Thus you cannot inject random stuff. In order to insert literal text inside HTML you need to use htmspecialchars(). And variable interpolation expects, well, variables, not functions. So the heredoc syntax offers little advantage here. Concatenation would be a simpler alternative:
$html = '<div id="topHeaderView">' . htmlspecialchars($value) . '</div>
<table>
<tr>
<td>' . htmlspecialchars($lang["FAI_R"]["PRT"]["TITLE"]["HEADER"]) . '</td>
</tr>';
You said you don't to escape and concatenate. I understand you. That's why complex HTML generation normally relies on template engines. Find one or build your own.