Skip to content

Commit

Permalink
Add very simple templating
Browse files Browse the repository at this point in the history
  • Loading branch information
cmelbye committed Dec 8, 2008
1 parent b563b9d commit 4d8d2af
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 14 deletions.
16 changes: 2 additions & 14 deletions controllers/GameController.php
Expand Up @@ -2,22 +2,10 @@

class GameController extends WebApp_Controller {
public function get() {
?>
<h2>Form</h2>
<form action="/step4" method="post">
<input type="hidden" name="testvalue" value="Hello from the get() method!" />
<p>
Submit:
<input type="submit" value="test" />
</p>
</form>
<?php
WebApp_Template::render( 'game/form' );
}

public function post() {
echo "Hello from the post() action!<br />";
echo "<pre>";
var_dump( $_POST );
echo "</pre>";
WebApp_Template::render( 'game/result' );
}
}
37 changes: 37 additions & 0 deletions library/WebApp/Template.php
@@ -0,0 +1,37 @@
<?php

class WebApp_Template {
protected static function _render( $template_name ) {
if( file_exists( 'templates/' . $template_name . '.phtml' ) ) {
ob_start();
require_once "templates/" . $template_name . ".phtml";
return ob_get_clean();
} else {
// fix me
return false;
}
}

public static function render( $template_name, $use_layout = true, $layout = 'default' ) {
if( $use_layout ) {

$layout_path = 'templates/layouts/' . $layout . '.phtml';

if( !file_exists( $layout_path ) ) {
return false;
}

$data = self::_render( $template_name );
$content_for_layout = $data;

# clean up some things before rendering the view
unset( $data );

include( $layout_path);

} else {
$data = self::_render( $template_name );
echo $data;
}
}
}

0 comments on commit 4d8d2af

Please sign in to comment.