1

Right now I'm assigning HTML to a variable the usual way:

$var = <<<END
<blah>...</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?

2 Answers 2

4

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();
Sign up to request clarification or add additional context in comments.

1 Comment

+1 PHP is a templating langugage. Use it, don't fight it! When you say $var= 'a load of HTML'; or echo '<a>some html with insecure $interpolations</a>'; you are doing it wrong!
1

I try to avoid this as much as possible by using a template engine such as Smarty -> http://www.smarty.net/

2 Comments

Yes, a template engine is definitely how you want to handle HTML contents in general.
PHP is already a templating engine in itself. Smarty is overrated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.