Skip to content

Commit

Permalink
some example usage of this class
Browse files Browse the repository at this point in the history
  • Loading branch information
Arin Sarkissian authored and Arin Sarkissian committed May 10, 2009
1 parent 6aa6e67 commit a316175
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 1 deletion.
3 changes: 2 additions & 1 deletion ExampleTemplate.php
Expand Up @@ -156,7 +156,8 @@ public function __toString()
*/
public function inc($includeTemplatePath, $header = null, $footer = null)
{
echo new Template($includeTemplatePath, $this->templateData, $header, $footer);
$class = get_class($this); // in case someone renames this and actually uses it
echo new $class($includeTemplatePath, $this->templateData, $header, $footer);
}
}

Expand Down
7 changes: 7 additions & 0 deletions examples/carsTemplate.php
@@ -0,0 +1,7 @@
<?php foreach($cars as $car) : ?>
<p>
make: <?php echo $car['make']; ?> <br />
model: <?php echo $car['model']; ?> <br />
year: <?php echo $car['year']; ?>
</p>
<?php endforeach; ?>
36 changes: 36 additions & 0 deletions examples/example1.php
@@ -0,0 +1,36 @@
<?php
// using the template with a header & footer


// rig up the include_path
set_include_path(
realpath('../') .
PATH_SEPARATOR .
realpath('./')
);

require_once('ExampleTemplate.php');

// make an array for cars for the template1.php file to spit put
$cars = array(
array('make' => 'vw', 'model' => 'bug', 'year' => '1966'),
array('make' => 'audi', 'model' => 'a4', 'year' => '2009')
);

// the template files ('template1.php', 'header1.php', 'footer1.php')
// will all have access to variables called $foo and $cars
$templateData = array(
'foo' => 'howdy',
'cars' => $cars
);

// render the template
$t = new ExampleTemplate('template1.php', $templateData, 'header1.php', 'footer1.php');
$t->render();

// get the output of the template as a string
$stringValueOfTemplate = $t->render(true);
// var_dump $stringValueOfTemplate to see we captured the output as a string
var_dump($stringValueOfTemplate);

?>
32 changes: 32 additions & 0 deletions examples/example2.php
@@ -0,0 +1,32 @@
<?php
// using the template with a header & footer AND including another template

// rig up the include_path
set_include_path(
realpath('../') .
PATH_SEPARATOR .
realpath('./')
);

require_once('ExampleTemplate.php');

// make an array for cars for the template1.php file to spit put
$cars = array(
array('make' => 'vw', 'model' => 'bug', 'year' => '1966'),
array('make' => 'audi', 'model' => 'a4', 'year' => '2009')
);

// the template files ('template2.php', 'header1.php', 'footer1.php')
// will all have access to variables called $foo and $cars
$templateData = array(
'foo' => 'howdy',
'cars' => $cars
);

// get the template's output but dont render yet
$t = new ExampleTemplate('template2.php', $templateData, 'header1.php', 'footer1.php');
$output = $t->render(true);

echo $output; // or echo $t would have worked...

?>
2 changes: 2 additions & 0 deletions examples/footer1.php
@@ -0,0 +1,2 @@
<hr />
<div style="text-align: center">i am a footer</div>
1 change: 1 addition & 0 deletions examples/footer2.php
@@ -0,0 +1 @@
</div>
3 changes: 3 additions & 0 deletions examples/header1.php
@@ -0,0 +1,3 @@
<h1>I am a header file.</h1>
<p>the value of $foo is <?php echo $foo ?></p>
<hr />
1 change: 1 addition & 0 deletions examples/header2.php
@@ -0,0 +1 @@
<div style="border: 2px solid #666; padding: 10px">
11 changes: 11 additions & 0 deletions examples/template1.php
@@ -0,0 +1,11 @@
<p>
I am the template file. wuddup???
</p>

<?php foreach($cars as $car) : ?>
<p>
make: <?php echo $car['make']; ?> <br />
model: <?php echo $car['model']; ?> <br />
year: <?php echo $car['year']; ?>
</p>
<?php endforeach; ?>
8 changes: 8 additions & 0 deletions examples/template2.php
@@ -0,0 +1,8 @@
<p>
I am the template file. wuddup???
</p>

<?php
// all the variables available to this template will get passed to the new one
$this->inc('carsTemplate.php', 'header2.php', 'footer2.php');
?>

0 comments on commit a316175

Please sign in to comment.