Permalink
Browse files

Views for Cling.

- Use the simpletemplate thingie for views
- Document how to use it.
  • Loading branch information...
1 parent 94e8d99 commit 2423a10218e8cf6a6f3a87824be41fa713d10d2f @CBeerta committed Dec 20, 2011
Showing with 169 additions and 2 deletions.
  1. +9 −0 Cling.php
  2. +17 −1 README.md
  3. +136 −0 View.php
  4. +7 −1 example.php
View
@@ -74,6 +74,11 @@ class Cling
private $_userNotFound = null;
/**
+ * Cling View
+ **/
+ public $view = null;
+
+ /**
* Constructor
*
* @param array $options User Options
@@ -87,10 +92,14 @@ public function __construct($options = array())
$this->_options = array_merge(
array(
+ 'template.path' => __DIR__ . '/views/',
'debug' => false,
),
$options
);
+
+ $this->view = new Cling_View();
+ $this->view->setPath($this->_options['template.path']);
}
/**
View
@@ -69,8 +69,24 @@ You can override the default handler, by calling the notFound method with a user
echo "I can't help you...\n";
});
-
The application will `exit` immediatle if the notFound method is being called.
+## Views
+Console Applications should, just like a Website, facilitate Views:
+ $app = new Cling(array('template.path' => 'views/'));
+
+ $app->command('hello-world:', function($name) {
+ $app->view->set('name', $name);
+ $app->view->display('example.txt.php')
+ })->help("Example with a View.");
+
+The view would look then something like:
+
+ Hello <?php echo $name; ?>!
+
+ I Hope You're doing fine.
+
+
+
View
@@ -0,0 +1,136 @@
+<?php
+/**
+* PHP Template Engine.
+*
+* Source: https://github.com/thinkphp/php-template-engine
+*
+* Implements the same interface as Savant3 and Smarty, but is more lightweight.
+* It's is originally created in the Sitepoint article:
+* http://www.sitepoint.com/article/beyond-template-engine
+*
+* Usage:
+* <?php
+* $tpl = new Template('path/to/templates');
+* $tpl->assign('variable','some value');
+* $tpl->display('template');
+* ?>
+*
+*
+* PHP Version 5.3
+*
+* Copyright (C) 2011 by Claus Beerta
+*
+* Permission is hereby granted, free of charge, to any person obtaining a copy
+* of this software and associated documentation files (the "Software"), to deal
+* in the Software without restriction, including without limitation the rights
+* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+* copies of the Software, and to permit persons to whom the Software is
+* furnished to do so, subject to the following conditions:
+*
+* The above copyright notice and this permission notice shall be included in
+* all copies or substantial portions of the Software.
+*
+* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+* THE SOFTWARE.
+*
+* @category CLI
+* @package Cling
+* @author Claus Beerta <claus@beerta.de>
+* @license http://www.opensource.org/licenses/mit-license.php MIT License
+* @link http://claus.beerta.de/
+**/
+
+/**
+* Simple Tempalte Class
+*
+* @category CLI
+* @package Cling
+* @author Claus Beerta <claus@beerta.de>
+* @license http://www.opensource.org/licenses/mit-license.php MIT License
+* @link http://claus.beerta.de/
+**/
+class Cling_View
+{
+ /**
+ * Template Variables
+ **/
+ private $_vars = array();
+
+ /**
+ * Directory to Templates
+ **/
+ private $_template_path = null;
+
+ /**
+ * Sets a template variable
+ *
+ * @param String $name the name of the variable template to set.
+ * @param String $value the value of the variable template.
+ *
+ * @return void
+ */
+ public function set($name, $value)
+ {
+ $this->vars[$name] = $value;
+ }
+
+ /**
+ * Sets the path to the template files.
+ *
+ * @param String $path => path to template files.
+ *
+ * @return void.
+ */
+ public function setPath($path)
+ {
+ $this->path = $path;
+ }
+
+ /**
+ * Open, parse, and return the template file.
+ *
+ * @param String $file the template filename.
+ *
+ * @return String
+ */
+ public function fetch($file)
+ {
+ //extract the vars to local namespace.
+ extract($this->vars);
+
+ //start output buffering.
+ ob_start();
+
+ //include the file
+ include $this->path . $file;
+
+ //get the contents of the buffer.
+ $contents = ob_get_contents();
+
+ //end buffering and discard.
+ ob_end_clean();
+
+ //return output String
+ return($contents);
+ }
+
+ /**
+ * Display the template directly.
+ *
+ * @param String $file the template filename.
+ *
+ * @return (String)
+ */
+ public function display($file)
+ {
+ echo $this->fetch($file);
+ }
+
+
+}
+
View
@@ -6,7 +6,11 @@
require_once 'Cling.php';
-$app = new Cling(array('debug' => true));
+$app = new Cling(
+ array(
+ 'debug' => true,
+ )
+);
/**
* Help Text
@@ -40,12 +44,14 @@ function() use ($app) {
/**
* A route that captures piped data from stdin
**/
+/*
$app->command(':stdin',
function() use ($app) {
while (($data = $app->route()->readStdin()) !== false) {
print_r($data);
}
});
+*/
/**
* Execute The App

0 comments on commit 2423a10

Please sign in to comment.