TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   HTML Inside the class? (http://www.talkphp.com/general/4453-html-inside-class.html)

Sirupsen 05-31-2009 07:35 AM

HTML Inside the class?
 
Hello! I have some trouble figuring out if I should make the HTML inside of my class, of if stuff like that is done outside of the class? For some reason the code looks a lot better when you do it inside the classes, but of course they become heavy too. What do you guys do? And what would you recommend me to do? :o Thanks!

Tanax 05-31-2009 11:05 AM

I recomend only using the class for handling the data. Then you have the HTML seperate(outside of the class), and feed the HTML with the data returned from the class.

allworknoplay 05-31-2009 02:06 PM

Quote:

Originally Posted by Tanax (Post 24766)
I recomend only using the class for handling the data. Then you have the HTML seperate(outside of the class), and feed the HTML with the data returned from the class.

Agreed. I think that's that is confusing about the power of OOP. It can really do almost anything you need it to do so where does one stop? If you think of it from an MVC point of view, you would keep the class for data usage only, while you keep the HTML/design separate.

I hope that makes sense.

Sakakuchi 05-31-2009 02:51 PM

Definetely keep it out. Just had a project some weeks ago, where all of the pagecontent was created in a class. It was such a mess, and it was almost impossible to maintain. It also kills your eyes when you need to find a certain point in that code-mess (No syntax-highlighting).

Enfernikus 05-31-2009 04:14 PM

Objects should ONLY handle business logic whilst content should be kept to files designed to handle View logic.

Sirupsen 05-31-2009 07:52 PM

So this looks good then..?

PHP Code:

        <?php
             
include_once("_class/database.php");
             include_once(
"_class/news.php");

            
$DB = new DB;
            
$NEWS = new news;
            
$DB->setup_db('localhost','root','','db');

          if (
$_POST['title'])
            
$NEWS->admin_write($_POST);

            if (
$_GET ['post'] == 1) {
              echo 
'<form method="post" action="index.php">';
              echo 
'<h1>Post something!</h1>';
              echo 
'<input type="text" name="title"><br>';
              echo 
'<input type="text" name="content"></textarea><br>';
              echo 
'<input type="submit" name="submit">';
              echo 
'</form>';
             } else {
                
$sql "SELECT * FROM slimplCMS_posts";
                
                foreach (
$DB->select($sql) AS $row) {
                    echo
                    
"<div id='post_".$row['id']."'>
                    <p>
                        "
.$row['title']."
                    </p><p>
                        "
.$row['bodytext']."
                    </p>
                        "
.$row['created']."
                    </p></div><br>"
;
                }
                echo 
'<a href="?post=1" title="Write an entry!" />Write an entry!</a>';
            }
        
?>


Tanax 05-31-2009 08:00 PM

Well yes.
Ideally I wouldn't use PHP to parse out layout HTML(such as forms and stuff).

It's better to use some sort of template system and use regular HTML(with PHP conditionals).

But that is good. At least you're not using HTML inside your class.

Sirupsen 05-31-2009 08:13 PM

Quote:

Originally Posted by Tanax (Post 24779)
Well yes.
Ideally I wouldn't use PHP to parse out layout HTML(such as forms and stuff).

It's better to use some sort of template system and use regular HTML(with PHP conditionals).

But that is good. At least you're not using HTML inside your class.

Alright, thanks! Now I know a bit more on how to think OOP, this all made me a bit confused. :)
If anything got anything to add, just shoot! :D

Village Idiot 06-01-2009 12:51 AM

You've struck me with inspiration to write another article. I will be writing about basic program design, your issue will certainly fall within the usage of classes and other logic areas.

I've never been able to properly convey good program design and object uses to someone who doesn't already know it, so this may take a few days to work out.

Sirupsen 06-01-2009 09:08 AM

Can't wait Village Idiot. :D

Sakakuchi 06-01-2009 02:13 PM

I personally preffer most of the time something like this(depending wheter there is more html or php):


PHP Code:

<?php
include_once("_class/database.php");
include_once(
"_class/news.php");

$DB = new DB;
$NEWS = new news;
$DB->setup_db('localhost','root','','db');

if (
$_POST['title'])
   
$NEWS->admin_write($_POST);

if (
$_GET ['post'] == 1) { ?>
    <form method="post" action="index.php">
    <h1>Post something!</h1>
    <input type="text" name="title"><br>
    <input type="text" name="content"></textarea><br>
    <input type="submit" name="submit">
</form>
<?php
} else { 
     
$sql "SELECT * FROM slimplCMS_posts";
                
    foreach (
$DB->select($sql) AS $row) {?>
        <div id="post_"<?=$row['id']?>">
        <p><?=$row['title']?></p>
        <p><?=$row['bodytext']?></p>
        <p><?=$row['created']?></p>
        </div><br>";
<?php ?>

<a href="?post=1" title="Write an entry!" />Write an entry!</a>
<?php ?>

Now, you can't see the advantage since the webeditor now has no syntax-highlighting on html, but when opening in eclipse or any similar IDE - it should show you also syntaxhighlighting on the html part. But as I said - I think i'ts not always the best way to do it - it depends on how much html do you have in comparison to the php code you have ;)

allworknoplay 06-01-2009 02:15 PM

Quote:

Originally Posted by Village Idiot (Post 24786)
You've struck me with inspiration to write another article. I will be writing about basic program design, your issue will certainly fall within the usage of classes and other logic areas.

I've never been able to properly convey good program design and object uses to someone who doesn't already know it, so this may take a few days to work out.

Please do! I can't wait!

Village Idiot 06-04-2009 07:18 PM

This article is taking me longer than I originally thought it would take. I am finding it stuff very difficult to put into words.

Village Idiot 06-05-2009 08:47 PM

I'm beginning to think it might be better to film it as a lecture, what I'm writing just doesn't seem to be getting it though. So we might looking at a lot longer than a few days, this would take weeks. I'm wrote out an incomplete powerpoint to go with it and it is 25 slides already. This could be over an hour of me talking. Doing this would be harder, but probably be the best tutorial I've done.

Wildhoney 06-05-2009 09:19 PM

You know how to create an atmosphere of anticipation :-) !

Village Idiot 06-05-2009 09:23 PM

Quote:

Originally Posted by Wildhoney (Post 24948)
You know how to create an atmosphere of anticipation :-) !

Accidentally, yes. I got my head into more than I first realized. I don't even know how I am going to get this stuff filmed. I still hope to get this completed, but don't expect anything soon.

maZtah 06-06-2009 10:03 AM

We're all looking forward to your writings, Village Idiot!


All times are GMT. The time now is 02:04 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0