Page 1 of 1

Solved! Title+Content to 1.php from 2.php in two instances?

Posted: Sun Mar 09, 2008 5:58 am
by JAB Creations
I am interested in creating a dynamic file handler to serve multiple pages on my site. However the issue is that I wish to pull a title and the content separately from 2.php. So the client sees 1.php?page=thisorthat. I'm not sure how to best approach the scenario. I can work with PHP classes if that helps any. Here is what I'd like to do...

1.php - Needs the HTML title initially but should not include other HTML content from 2.php until later in the body.
2.php - Has the title and the content though needs to serve both to 1.php separately.

Re: Title+Content to 1.php from 2.php but in two instances?

Posted: Sun Mar 09, 2008 6:14 am
by Chris Corbyn
You need a "view" object. You set properties into the view (i.e. set the title among other things), and the render it as late as possible. Probably time to start reading up on MVC.

Re: Title+Content to 1.php from 2.php but in two instances?

Posted: Sun Mar 09, 2008 10:15 am
by JAB Creations
Hey Chris thanks though I need a little help: what should I read up more specifically on php.net? I have the feeling in my mind this is what I want though I try to use as minimal code as possible. Thanks!

Re: Title+Content to 1.php from 2.php but in two instances?

Posted: Sun Mar 09, 2008 5:31 pm
by Chris Corbyn
Nothing specifically on php.net, this is more of a general (Web MVC) pattern.

Effectively you want a place to hold some values, and a way to tell that thing to display a template at the end.

Here's a very basic view (the best bare bones of most view containers):

Code: Select all

<?php
 
class SimpleView {
  private $_vars = array();
  
  public function setVar($name, $value) {
    $this->_vars[$name] = $value;
  }
 
  public function render($_template) {
    export($this->_vars);
    include($_template);
  }
}
So somewhere at the start of you code you create a "new SimpleView()".

Code: Select all

$view = new SimpleView();
Then you could set some variables into it if you like:

Code: Select all

$view->setVar('author', 'Me!');
And you pass it to anything else which needs to modify variables in it.

Then when you want to output your page:

Code: Select all

$view->render('some-template.tpl.php');
In this particular case there would be a $author variable exposed inside the template file.

Re: Title+Content to 1.php from 2.php but in two instances?

Posted: Mon Mar 10, 2008 2:12 pm
by JAB Creations
Check it out, I was able to get it to work! :mrgreen:
file1.php

Code: Select all

<?php
include("templates-1.php");
$request = array_keys($_GET);
foreach ($request as $game2) {echo $game2;}
$game1 = 'pc_';
$game3 = '.php';
$game = $game1.$game2.$game3;
include($game);
 
echo '<title>'.$title.' Cheat Codes</title>
<meta name="Description" content="'.$title.' cheat codes for PC." />
<meta name="keywords" content="'.$title.', cheat, codes, pc" />
<meta name="language" content="en" />' . "\n";
include("templates-2.php");
 
//get command from list
echo $page;
include("templates-3.php");
file2.php

Code: Select all

<?php
$title = 'A game name with ImpORTanT casing';
$page = <<<EOD
<div>Lots of HTML stuff that could be a hundred lines long goes here, yay!</div>
EOD;
?>