Page generation from PHP class


Page generation from PHP class



I'm actually creating websites for fun and some of my friends told me that I could be more efficient if I could create the output of the page with a php class that would represent the whole page.

I was wondering how you people would do it.

Thanks




Java AOT + loading java classes dynamically

1:



PHP include class functions with variables
I'd suggest looking into some PHP frameworks, such as Cake which is good for beginners, or Symfony or Zend Framework if you're more skilled.


Members vs method arguments access in C++
Those will streamline your PHP development a lot..
Have you ever used a “class instance variable” in any of your Ruby code?


Best Way of Implementing these 3 classes in C#: Vector, Direction (unit vector), Point


call function in superclass from subclass

2:



php static function
I am not a OO programmer ..


C# method that is executed after assembly is loaded
but, as a proof of concept, and as per your question, you can do/try something like this..
class Page {     public $meta_title;     public $meta_keywords;     public $html_body;      public function displayPage() {     	$page ='     	<html>     	<head>     		<title>'.$this->meta_title.'</title>     		<meta name="keywords" content="'.$this->meta_keywords.'" />     	</head>     	<body>     		'.$this->html_body.'     	</body>     	</html> ';     	echo $page;     } } 
Then you just use this Page class as ..

.
$page = new Page(); $page->meta_title ="Hello world!"; $page->meta_keywords = "some text keywords"; $page->body = '<h1>Contact Us </h1> <p>you can contact us at blah ... 

blah ..

etc.</p> <address>Dummy Address </address> '; $page->displayPage();
Please note that you can add so many things into to it like ..

class variables (type array) to define stylesheets, javascripts files ..

then you just loop over it to define these files dynamically for individual pages.. You can amend the display page function so that it accomodate left, right, or top navigation bar.

Then you can also have variables like $show_right_bar, $show_left_bar to control which pages display which side navigation bar.

So you can amend and extend to whatever your requirements are.. Alternatively, you can try some php frameworks, which are much more evolved solutions, but that really depends on your programming skills and your requirements.. Hope this helps..


3:


What you could do is have all the tags in the page stored as a multi-dimensional array.

PHP is mushy enough to do this.

As such, every tag would be stored in an element of the array.

Then, you can use control statements to print different parts of the page at different times depending on certain conditions.

It would kinda look like what you get when you convert an XML file to an array.

Here's what I'm thinking..
$page['html']['title'] = "Page's Title"; $page['body']['p'][1] = "1st Paragraph"; $page['body']['p'][2] = "2nd Paragraph"; 
And then you could use your own functions to print these out as HTML fields..


4:


I would suggest using some of the MVC (Model, View, Controller) frameworks out there like KohanaPHP.

It is essentially this.

You're working in a strictly Object-Oriented environment.

A simple page in Kohana, build entirely from a class would look like this:.
class Home_Controller extends Controller {   public function index()   {     echo "Hello World";   } } 
You would then access that page by visiting youur url, the class name, and the method name:. http://www.mysite.com/home/ (index() can be called after home/, but it's implicit). When you start wanting to bring in database-activity, you'll start working with another Class called a Model.

This will contain methods to interact with your database, like the following:.
class Users_Model extends Model {   public function count_users()   {     return $this->db->count_records('users');   } } 
Note here that I didn't write my own query.

Kohana comes with an intuitive Query Builder.. That method would be called from within your Controller, the first class that we mentioned at the beginning of this solution.

That would look like this:.
class Home_Controller extends Controller {   public function index()   {     $usersModel = new Users_Model;     $userCount = $usersModel->count_users();      echo "We have " . 

$userCount .

" users!"; } }
Eventually, you'll want more complicated layouts, which will involve HTML/CSS/Javascript.

At this point, you would introduce the "Views," which are just presentation layers.

Rather than calling echo or print from within the Controller, you would load up a view (an HTML page, essentially) and pass it some variables:.
class Home_Controller extends Controller {   public function index()   {     $myView = new View("index");     $usersModel = new Users_Model;     $userCount = $usersModel->count_users();      $myView->userCount = $userCount;     $myView->render(TRUE);   } } 
Which would load the following "View".
<p>We have <?php print $userCount; ?> users!</p> 
That should be enough to get you started.

Using the MVC-style is really clean, and very fun to work with..


5:


So many ways to do this, indeed.. When I make a simple personal site, I typically have a banner at the top and a menu on one side.

To prevent myself from having to copy'n'paste everything, my index.php checks a get variable, and based on that get variable loads the appropriate content in the middle.

By way of example, my index.php.

My menu actually loads the content with a function, but this is not necessary/recommended:.
<?php  $page = strtolower($_GET['page']); if(!isset($page) || $page === '') {     $page="home"; }  switch($page) {     case "loomis":     	require_once("loomis.php");     	break;     case "home":     default:     	$page = "home";     	require_once("home.php");     	break; }  include "menu.php"; ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Nebulous - <?php getTitle($page) ?></TITLE> <link href="style.css" rel="stylesheet" type="text/css" /> </HEAD>  <body> <?php displayMenu() ?> </body> </html> 
Classes are really only useful as representations of data or functionality, not really to render pages..


6:


OOP can be useful in page rendering if you have something that is static throughout your pages.

(e.g.

Header, Footer) .
$index = new Page(); $index->displayHeader(); // php/html per needs $index->displayFooter(); 
This isn't all that intuitive but as the others said, the possibilities are vast.

I would suggest picking up a book on the subject..


7:


The best way to achieve your aim is to start with an html class.

Its purpose is to write any HTML tags and indent them properly.

It is not a simple task as I have hardly ever found one such html class on the web written properly.

Hint: you will need a stack for managing the automatic closure of HTML tags and a working buffer for the current tag that rolls into a bigger holding buffer.. Using the services of the html class, you can build a webpage class that will create pages from settings.

The trick here is to remain flexible and not hard-code too much inside your class.

You need a setter for basic settings such as title, author, keywords etc.

holding them into an array.

Then webpage->header() should output the top of your page automatically.

Then you should use functions such as webpage->heading($string,$size) to create a heading, webpage->paragraph($text), webpage->image() and so on.

At the end you just need webpage->footer().. To make the webpage class worthwhile, make it create the css as well.

For example if you want to use bootstrap, you can have the class include the css and style your code.

Note that you can also make the webpage class create advanced objects such as webpage->breadcrumbs() or webspage->pagination() .



85 out of 100 based on 85 user ratings 835 reviews