Assigning HTML value to PHP variable - best practice?
Right now I'm assigning HTML to a variable the usual way:
$var = <<<END
<blah>...开发者_如何学Go</blah>
END;
The big disadvantage is that my IDE won't treat this as HTML, and so it won't highlight the code. Is there a way to do it that will keep the HTML outside of the <?php ?>
tags so that code highlighting will work?
If you're printing, you could always just exit PHP and go back in whenever you like:
<?php
function print_header() {
?>
<html>
<head>
</head>
<body>
<?php
}
function print_footer() {
?>
</body>
</html>
<?php
}
print_header();
print_footer();
Alternatively, you could use buffers to use that technique to assign them to variables:
<?php
function print_header() {
ob_start();
?>
<html>
<head>
</head>
<body>
<?php
return ob_get_clean();
}
function print_footer() {
ob_start();
?>
</body>
</html>
<?php
return ob_get_clean();
}
echo print_header();
echo print_footer();
I try to avoid this as much as possible by using a template engine such as Smarty -> http://www.smarty.net/
精彩评论