Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it bad to use html inside a php class?

Tags:

php

wordpress

is there anything wrong with using html inside a class function? I call it in the DOM so I don't need a string returned.

public function the_contact_table(){
    ?>
    <div>
        some html here
    </div>
    <?php
}

Also when I do need the string I use this method? Is there a better way or is this relatively standard?

public function get_single(){
    ob_start();?>
        <div class='staff-member single'>
            <div class='col left'>
                <div class='thumbnail'>
                    thumbnail
                </div>
                <?php $this->the_contact_table(); ?>
            </div>
            <div class='col right'>

            </div>
        </div>      
    <?php
    $content = ob_get_contents();
    ob_end_clean();
    return $content;
}

UPDATE

I should have explained why i am doing this. I'm making a Wordpress plugin and want to control a post types output. So I am using a filter like below

public function filter_single($content){
     global $post;
     if ($post->post_type == 'staff-member') {

         $sm = new JM_Staff_Member($post);
         $content = $sm->get_single();
     }
     return $content;
}

So as you can see, I must return a string to the wordpress core

like image 618
JackMahoney Avatar asked Jul 29 '12 05:07

JackMahoney


People also ask

Can you have HTML inside PHP?

While HTML and PHP are two separate programming languages, you might want to use both of them on the same page to take advantage of what they both offer. With one or both of these methods, you can easily embed HTML code in your PHP pages to format them better and make them more user-friendly.

What are the pitfalls commonly seen with the practice of inserting HTML inside PHP?

The problem with having HTML and PHP mixed up is that it's hard to reuse the code, and often a lot harder to read and understand it. There are lots of ways to do it - Twig (as in Symfony) and HAML are at the extreme of separation.


1 Answers

You should be using HEREDOC instead of output buffering if you want to store a long string into a variable. It looks like this:

$content = <<<EOD
content here
EOD;

EOD can be anything, but note two important things:

  1. It can't have any whitespace in front of it and it must be on it's own line
  2. It shouldn't be a string that could be found within your content

If you are using PHP >= 5.3, then you should use NOWDOC, which does not parse for variable inside the doc (unless you need this). The only different with the syntax of NOWDOC is that the sentinel is enclosed in quotes:

$content = <<<'EOD'
content here
EOD;

The reason why I'd stray away from output buffering is that it prevents the server from chunking the data sent to the client. This means that requests will seem slower because instead of the content being progressively sent to the client and displayed, it is forced to be sent all at once. Output buffering is a hack for situations when functions carelessly echo data instead of returning it or a tool for certain applications with the specific need for it. I'd also imagine that you'd take a hit on execution time if you used output buffering (because it involves function calls) versus HEREDOCing the string into a variable or including a view.

Now to answer the question about whether it is appropriate, I would say that in an MVC application all HTML and other content should be contained within its own view. Then a controller can call a view to display itself and doesn't have to worry about knowing the code involved in displaying the view. You can still pass information (like titles, authors, arrays of tags, etc.) to views, but the goal here is separating the content from the logic.

That said, Wordpress templates and code looks pretty sloppy to begin with and loosely if not at all implements MVC so if it's too much work to create a view for this, I'd say the sloppiness would fit in with WP's style.

like image 178
Bailey Parker Avatar answered Sep 21 '22 14:09

Bailey Parker