Nicolas Racine
Nicolas Racine

Reputation: 1061

Echoing un-escaped HTML

Very unusual question.

I came across some code some years in the pass that was including conditions and normal PHP syntax while echoing all content.

My question is how is that Technic/syntax called. I have been googling with some very broad terms and can't find what im looking for.

If my memory is correct, The code I viewed long time ago had un-escaped HTML and it was not required to start and stop PHP processing with <?php ?>

I Have a method within a class called Template\Labels::User() the only purpose of that method is to echo the proper html to create a label within my webapp so that the pages are lighten of code and clear to anyone viewing the code.

Id like to avoid, having to <?php ?> for very simple boolean if

Any one know what I am looking for ?

static function User($UserObj,$isLink = true){
    ?>
    <div class="image label bg-purple" style="margin: 4px;">
        <?php if($isLink){
            ?><a href=""><?php
        } ?>
        <img src="<?php echo $UserObj -> ProfilePicture; ?>" style="height: 2em;" class="img-circle" alt="User Image">
        <label style="font-size: 90%"><?php echo $UserObj->FirstName{0}.$UserObj->LastName{0}; ?></label>
        <?php if($isLink){
            ?></a><?php
        } ?>
    </div>
    <?php
}

Edited

After some more research by going through PHP documentation on Operator I found Nowdoc string quoting

Can someone shed some light onto Nowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML

http://php.net/manual/en/language.types.string.php#language.types.string.syntax.nowdoc

Upvotes: 2

Views: 159

Answers (2)

Jack Siro
Jack Siro

Reputation: 797

Its good that you added code to your question so that we can all see what you are dealing with here. Now to me what I understand with your question is that you want to avoid using php tags to echo some html code based on if condition.

<?php 
    static function User($UserObj,$isLink = true){
        $html = '<div class="image label bg-purple" style="margin: 4px;">';
        if($isLink) $html .= '<a href="">';
        $html .= '<img src="'.@$UserObj->ProfilePicture.'" style="height: 2em;" class="img-circle" alt="User Image">';
        $html .= '<label style="font-size: 90%">'.@$UserObj->FirstName[0].@$UserObj->LastName[0].'</label>';
        if($isLink) $html .= '</a>';
        echo $html;
    }
?>

In my thinking I thought you should just have to run php tags once and use a simple variable to add your html code to so that you can print at the end of the function.

Upvotes: 1

Jack Siro
Jack Siro

Reputation: 797

I didn't understand some of your images but all the same your issue is printing unescaped html in PHP. In other words you want to have raw html.

There are two functions am thinking of right now which you can use depending on your desired output: html_entity_decode() and htmlentities().

html_entity_decode() is the opposite of htmlentities() in that it converts all HTML entities in the string to their applicable characters.

<?php     $orig = "I'll \"walk\" the <b>d
$a = htmlentities($orig);
$b = html_entity_decode($a);
echo $a; // I'll &quot;walk&quot
echo $b; // I'll "walk" the <b>
?>

Ref: http://www.php.net/html_entity_decode

I hope this helps solve your issue of unescaped html.

Upvotes: 0

Related Questions