Permalink
Browse files

Initial Bonita structure and files.

  • Loading branch information...
1 parent 41e7ddc commit 6962722a11c061f6194fe55527e8dce6093ae268 @benwerd committed Aug 23, 2011
View
@@ -0,0 +1,7 @@
+Bonita requires PHP 5.2+.
+
+To use in its most basic setting, place the whole Bonita folder into
+your web project and include start.php.
+
+More sophisticated instructions - e.g. for inclusion in plugin-based
+systems, or for using multiple templates - coming soon.
View
@@ -1,4 +1,4 @@
-Copyright 2011 Ben Werdmuller
+Copyright 2011 Ben Werdmuller <ben@benwerd.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
View
54 README
@@ -0,0 +1,54 @@
+Bonita: a simple PHP template engine.
+=====================================
+
+Version 0.1: August 22, 2011.
+
+Getting started
+---------------
+
+Check out git://github.com/benwerd/bonita.git
+
+To use in its most basic setting, place the whole Bonita folder into
+your web project and include start.php.
+
+More sophisticated instructions - e.g. for inclusion in plugin-based
+systems, or for using multiple templates - coming soon.
+
+
+Bugs and feature requests
+-------------------------
+
+Bonita uses Github's built-in issue tracker:
+
+https://github.com/benwerd/bonita/issues
+
+
+Why is this here?
+-----------------
+
+I've been using the same patterns for a long time, and have rewritten
+them time and time again. So I decided I'd create a single, canonical
+project, and release it under an open source license so others can
+use it and help it grow.
+
+Bonita's templating engine lets you build templates in PHP, and very
+quickly (for example) provide feeds for
+
+
+Copyright and license
+---------------------
+
+Copyright 2011 Ben Werdmuller <ben@benwerd.com>
+<http://benwerd.com/>
+
+Licensed under the Apache License, Version 2.0 (the "License");
+you may not use this file except in compliance with the License.
+You may obtain a copy of the License at
+
+http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
View
@@ -0,0 +1,30 @@
+<?php
+
+ /**
+ * Hello!
+
+ This is the simplest of all possible examples (ish). It'll draw a
+ page of text in two template types.
+
+ Stick ?t=rss on the end of the URL to load the same page in RSS.
+
+ */
+
+ // Load Bonita
+ require_once dirname(dirname(__FILE__)) . '/start.php';
+
+ // Add this directory as an additional path
+ Bon::additionalPath(dirname(__FILE__));
+
+ // Instantiate template
+ $tmp = new BonTemp();
+
+ // For the purposes of this example, some GET line fun to choose which template
+ // we're using
+ if ($_GET['t'] == 'rss') $tmp->setTemplateType('rss');
+
+ // Page contents
+ $tmp->title = 'Example page'; // Page title
+ $tmp->url = 'https://github.com/benwerd/bonita'; // A link back to git
+ $tmp->body = $tmp->draw('pages/example'); // Page body
+ $tmp->drawPage(); // Draw the page
@@ -0,0 +1,11 @@
+<p>
+ This is a quick example of how you can build pages in Bonita that very
+ quickly become independent of their template types.
+</p>
+<p>
+ (In future, we'll probably go further.)
+</p>
+<?=$t->draw('pages/example/link');?>
+<p>
+ <a href="<?php echo $vars['url'];?>">Here's a link back to the Bonita project on Github.</a>
+</p>
@@ -0,0 +1,3 @@
+<p>
+ <a href="?t=rss">Click here to get the RSS version of this page.</a>
+</p>
@@ -0,0 +1,10 @@
+<!DOCTYPE HTML>
+<html>
+ <head>
+ <title><?=$vars['title']; ?></title>
+ </head>
+ <body>
+ <h1><?=$vars['title'];?></h1>
+ <?=$vars['body'];?>
+ </body>
+</html>
@@ -0,0 +1,3 @@
+<p>
+ This is the RSS version of the page.
+</p>
@@ -0,0 +1,17 @@
+<?php
+
+ header("content-type: text/xml");
+ header('pragma: public', true);
+
+?>
+<rss version="2.0">
+ <channel>
+ <title><![CDATA[<?=$vars['title'];?>]]></title>
+ <link><?php echo htmlentities($vars['url']);?></link>
+ <item>
+ <title><![CDATA[<?=$vars['title'];?>]]></title>
+ <link><?php echo htmlentities($vars['url']);?>#</link>
+ <description><![CDATA[<?=$vars['body'];?>]]></description>
+ </item>
+ </channel>
+</rss>
@@ -0,0 +1,60 @@
+<?php
+
+ /**
+
+ Bonita static management class file
+
+ @package Bonita
+
+ */
+
+
+ class Bon {
+
+ /** Configuration variables **/
+
+ static $path = ''; // The path of the Bonita base
+ static $additionalPaths = array(); // Additional paths to check for templates
+ static $cache = false; // Set depending on the existence of the cache
+
+ /** Private helper vars **/
+
+ private static $templates = array(); // Template overrides
+
+ /** Useful functions **/
+
+ /**
+ * Returns whether or not we're running off the cache
+ * @return true|false
+ */
+
+ static function cached() {
+ return self::$cache;
+ }
+
+ /**
+ * Sets an additional path to check for (eg) templates
+ * Does nothing if we're running off the cache
+ * @param string $path A full path
+ * @return true|false Depending on success
+ */
+
+ static function additionalPath($path) {
+ if (self::cached()) return false;
+ if (!empty($path) && is_dir($path)) {
+ if (!in_array($path,self::$additionalPaths)) {
+ self::$additionalPaths[] = $path;
+ }
+ }
+ }
+
+ /**
+ * Get any saved additional paths (or an empty array if there aren't any)
+ * @return array
+ */
+
+ static function getAdditionalPaths() {
+ return self::$additionalPaths;
+ }
+
+ }
@@ -13,8 +13,18 @@
class BonTemp {
- private $templateType = 'default'; // Which template are we using?
- private $vars = array(); // Template variables
+ public $templateType = 'default'; // Which template are we using?
+ public $vars = array(); // Template variables
+
+ /**
+ * Constructor allows copying; we're shunning use of clone here
+ */
+ function __construct($initial = false) {
+ if ($initial instanceof BonTemp) {
+ $this->vars = $initial->vars;
+ $this->templateType = $initial->templateType;
+ }
+ }
/**
* Magic method to set template variables
@@ -38,5 +48,97 @@ function __get($name) {
}
return null;
}
+
+ /**
+ * Method to draw an actual template element
+ * @param string Name of the template element to draw
+ * @return string Rendered template element
+ */
+ function draw($templateName) {
+ $templateName = preg_replace('/^_[A-Z0-9\/]+/i','',$templateName);
+ if (!empty($templateName)) {
+
+ // Add the Bonita base path to our additional paths list
+ $paths = Bon::getAdditionalPaths();
+ $paths[] = Bon::$path;
+
+ // Add template types to an array; ensure we revert to default
+ $templateTypes = array($this->getTemplateType());
+ if ($this->getTemplateType() != 'default') $templateTypes[] = 'default';
+
+ // Cycle through the additional paths and check for the template file
+ // - if it exists, break out of the foreach
+ foreach($templateTypes as $templateType)
+ foreach($paths as $basepath) {
+ $path = $basepath . '/templates/'.$templateType.'/' . $templateName . '.tpl.php';
+ if (file_exists($path)) {
+
+ // Special vars:
+ $vars = $this->vars;
+ $t = $this;
+
+ ob_start();
+ @include $path;
+ return ob_get_clean();
+
+ // Break out of the foreach path
+ // (although this code should be unreachable)
+ break;
+
+ }
+ }
+ }
+ // If we've got here, just return a blank string; the template doesn't exist
+ return '';
+ }
+
+ /**
+ * Returns the value of a stored variable
+ * @param $name The name of the variable
+ * @return mixed
+ */
+ function vars($name) {
+ if (isset($this->vars[$name]))
+ return $this->vars[$name];
+ else
+ return false;
+ }
+
+ /**
+ * Draws the shell template
+ * @param $echo If set to true (by default), echoes the page; otherwise returns it
+ */
+ function drawPage($echo = true) {
+ if ($echo)
+ echo $this->draw('shell');
+ else
+ return $this->draw('shell');
+ }
+
+ /**
+ * Returns the current template type
+ * @return string Name of the current template ('default' by default)
+ */
+ function getTemplateType() {
+ return $this->templateType;
+ }
+
+ /**
+ * Sets the current template type
+ * @param string $template The name of the template you wish to use
+ */
+ function setTemplateType($templateType) {
+ if (!empty($templateType)) {
+ $this->templateType = $templateType;
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * In case anyone's wondering whether this class is a reference or not.
+ */
+ function sookie() {}
}
View
@@ -13,9 +13,15 @@
*/
-
- // Set Bonita base path to the directory this file is in
- constant BONITA_PATH = dirname(__FILE__);
// Load required files
- require_once(dirname(__FILE__) . '/includes/bonita.class.php');
+ require_once(dirname(__FILE__) . '/includes/classes/bon.class.php');
+ require_once(dirname(__FILE__) . '/includes/classes/bontemp.class.php');
+
+
+ // Set Bonita base path to the directory this file is in
+ Bon::$path = dirname(__FILE__);
+
+ // Check for the existence of a cache file: if it exists, run it
+ // (NB: right now, the cache mechanism is a definite @TODO)
+ if (file_exists(Bon::$path . '/paths.cache.php')) @include_once Bon::$path . '/paths.cache.php';

0 comments on commit 6962722

Please sign in to comment.