Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 308 Vote(s) - 3.56 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Is it bad to use html inside a php class?

#1
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
Reply

#2
I think it is good practice to use PHP only for logic of application and transmission some data to view layer (template engine). In accordance with this there are some patterns like MVC.
Reply

#3
You should be using [HEREDOC][1] 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.

[1]:

[To see links please register here]

Reply

#4
It's not a good practice in regard to the fact that you alienate front-end developers by placing what are actually "Views" inside of PHP class files. This was one of my biggest issues when I first started using PHP in general, is that I wanted to dynamically create content within classes. It's a great idea, but you want to do it in a way that allows many members of your team to work together as smoothly as possible ;].

You should probably have the content inside of a separate file called "staff-member-single.php", which you then call in your function

public function get_single(){
ob_start();
require_once('views/staff-member-single.php');
$content = ob_get_contents();
ob_end_clean();
return $content;
}

You'd refactor that into a reusable method typically though, so it'd look a little bit like..

public function get_single()
{
$string = $this->render_view_as_string('satff-member-single');
return $string;
}

public function render_view($view)
{
require('views/'.$view.'.php');
}

public function render_view_as_string($view)
{
ob_start();
$this->render_view($view);
$content = ob_get_contents();
ob_end_clean();
return $content;
}
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through