diff --git a/README.md b/README.md index cb3934e..2f1e82d 100644 --- a/README.md +++ b/README.md @@ -14,20 +14,10 @@ Slim Views only officially support the following views listed below. #### using [Composer](http://getcomposer.org/) -Create a composer.json file in your project root: - -```json -{ - "require": { - "slim/views": "0.1.*" - } -} -``` - -Then run the following composer command: +Install in your project by running the following composer command: ```bash -$ php composer.phar install +$ php composer require slim/views ``` ## Smarty diff --git a/Smarty.php b/Smarty.php index fcf5101..1f5f9da 100644 --- a/Smarty.php +++ b/Smarty.php @@ -1,124 +1,124 @@ - - */ -class Smarty extends \Slim\View -{ - /** - * @var string The path to the Smarty code directory WITHOUT the trailing slash - */ - public $parserDirectory = null; - - /** - * @var string The path to the Smarty compiled templates folder WITHOUT the trailing slash - */ - public $parserCompileDirectory = null; - - /** - * @var string The path to the Smarty cache folder WITHOUT the trailing slash - */ - public $parserCacheDirectory = null; - - /** - * @var SmartyExtensions The Smarty extensions directory you want to load plugins from - */ - public $parserExtensions = array(); - - /** - * @var parserInstance persistent instance of the Parser object. - */ - private $parserInstance = null; - - /** - * Render Template - * - * This method will output the rendered template content - * - * @param string $template The path to the template, relative to the templates directory. - * @param null $data - * @return string - */ - public function render($template, $data = null) - { - $parser = $this->getInstance(); - $parser->assign($this->all()); - - return $parser->fetch($template, $data); - } - - /** - * Creates new Smarty object instance if it doesn't already exist, and returns it. - * - * @throws \RuntimeException If Smarty lib directory does not exist - * @return \Smarty Instance - */ - public function getInstance() - { - if (! ($this->parserInstance instanceof \Smarty)) { - if (!class_exists('\Smarty')) { - if (!is_dir($this->parserDirectory)) { - throw new \RuntimeException('Cannot set the Smarty lib directory : ' . $this->parserDirectory . '. Directory does not exist.'); - } - require_once $this->parserDirectory . '/Smarty.class.php'; - } - - $this->parserInstance = new \Smarty(); - $this->parserInstance->template_dir = $this->getTemplatesDirectory(); - if ($this->parserExtensions) { - $this->parserInstance->addPluginsDir($this->parserExtensions); - } - if ($this->parserCompileDirectory) { - $this->parserInstance->compile_dir = $this->parserCompileDirectory; - } - if ($this->parserCacheDirectory) { - $this->parserInstance->cache_dir = $this->parserCacheDirectory; - } - } - - return $this->parserInstance; - } -} + + */ +class Smarty extends \Slim\View +{ + /** + * @var string The path to the Smarty code directory WITHOUT the trailing slash + */ + public $parserDirectory = null; + + /** + * @var string The path to the Smarty compiled templates folder WITHOUT the trailing slash + */ + public $parserCompileDirectory = null; + + /** + * @var string The path to the Smarty cache folder WITHOUT the trailing slash + */ + public $parserCacheDirectory = null; + + /** + * @var SmartyExtensions The Smarty extensions directory you want to load plugins from + */ + public $parserExtensions = array(); + + /** + * @var parserInstance persistent instance of the Parser object. + */ + private $parserInstance = null; + + /** + * Render Template + * + * This method will output the rendered template content + * + * @param string $template The path to the template, relative to the templates directory. + * @param null $data + * @return string + */ + public function render($template, $data = null) + { + $parser = $this->getInstance(); + $parser->assign($this->all()); + + return $parser->fetch($template, $data); + } + + /** + * Creates new Smarty object instance if it doesn't already exist, and returns it. + * + * @throws \RuntimeException If Smarty lib directory does not exist + * @return \Smarty Instance + */ + public function getInstance() + { + if (!($this->parserInstance instanceof \Smarty)) { + if (!class_exists('\Smarty')) { + if (!is_dir($this->parserDirectory)) { + throw new \RuntimeException('Cannot set the Smarty lib directory : ' . $this->parserDirectory . '. Directory does not exist.'); + } + require_once $this->parserDirectory . '/Smarty.class.php'; + } + + $this->parserInstance = new \Smarty(); + $this->parserInstance->template_dir = $this->getTemplatesDirectory(); + if ($this->parserExtensions) { + $this->parserInstance->addPluginsDir($this->parserExtensions); + } + if ($this->parserCompileDirectory) { + $this->parserInstance->compile_dir = $this->parserCompileDirectory; + } + if ($this->parserCacheDirectory) { + $this->parserInstance->cache_dir = $this->parserCacheDirectory; + } + } + + return $this->parserInstance; + } +} diff --git a/SmartyPlugins/function.baseUrl.php b/SmartyPlugins/function.baseUrl.php index 1ed33fd..ce800b6 100644 --- a/SmartyPlugins/function.baseUrl.php +++ b/SmartyPlugins/function.baseUrl.php @@ -1,26 +1,26 @@ -request(); - $uri = $req->getUrl(); - - if ($withUri) { - $uri .= $req->getRootUri(); - } - - return $uri; -} +request(); + $uri = $req->getUrl(); + + if ($withUri) { + $uri .= $req->getRootUri(); + } + + return $uri; +} diff --git a/SmartyPlugins/function.siteUrl.php b/SmartyPlugins/function.siteUrl.php index 83dc37b..f08baa5 100644 --- a/SmartyPlugins/function.siteUrl.php +++ b/SmartyPlugins/function.siteUrl.php @@ -1,27 +1,27 @@ -request(); - $uri = $req->getUrl(); - - if ($withUri) { - $uri .= $req->getRootUri(); - } - - return $uri . '/' . ltrim($url, '/'); -} +request(); + $uri = $req->getUrl(); + + if ($withUri) { + $uri .= $req->getRootUri(); + } + + return $uri . '/' . ltrim($url, '/'); +} diff --git a/SmartyPlugins/function.urlFor.php b/SmartyPlugins/function.urlFor.php index b0d3ea7..0a4150f 100644 --- a/SmartyPlugins/function.urlFor.php +++ b/SmartyPlugins/function.urlFor.php @@ -1,43 +1,42 @@ -urlFor($name); - - if (isset($params['options'])) - { - switch (gettype($params['options'])) { - case 'array': - $opts = $params['options']; - break; - - case 'string': - $options = explode('|', $params['options']); - foreach ($options as $option) { - list($key, $value) = explode('.', $option); - $opts[$key] = $value; - } - break; - - default: - throw new \Exception('Options parameter is of unknown type, provide either string or array'); - } - - $url = \Slim\Slim::getInstance($appName)->urlFor($name, $opts); - } - - return $url; -} +urlFor($name); + + if (isset($params['options'])) { + switch (gettype($params['options'])) { + case 'array': + $opts = $params['options']; + break; + + case 'string': + $options = explode('|', $params['options']); + foreach ($options as $option) { + list($key, $value) = explode('.', $option); + $opts[$key] = $value; + } + break; + + default: + throw new \Exception('Options parameter is of unknown type, provide either string or array'); + } + + $url = \Slim\Slim::getInstance($appName)->urlFor($name, $opts); + } + + return $url; +} diff --git a/Twig.php b/Twig.php index 2a42ca0..a76ddd2 100644 --- a/Twig.php +++ b/Twig.php @@ -6,7 +6,7 @@ * @author Andrew Smith * @link http://www.slimframework.com * @copyright 2013 Josh Lockhart - * @version 0.1.2 + * @version 0.1.3 * @package SlimViews * * MIT LICENSE diff --git a/TwigExtension.php b/TwigExtension.php index e5b13ed..f5e7bb5 100644 --- a/TwigExtension.php +++ b/TwigExtension.php @@ -6,7 +6,7 @@ * @author Andrew Smith * @link http://www.slimframework.com * @copyright 2013 Josh Lockhart - * @version 0.1.2 + * @version 0.1.3 * @package SlimViews * * MIT LICENSE @@ -47,6 +47,7 @@ public function getFunctions() new \Twig_SimpleFunction('urlFor', array($this, 'urlFor')), new \Twig_SimpleFunction('baseUrl', array($this, 'base')), new \Twig_SimpleFunction('siteUrl', array($this, 'site')), + new \Twig_SimpleFunction('currentUrl', array($this, 'currentUrl')), ); } @@ -70,4 +71,21 @@ public function base($withUri = true, $appName = 'default') } return $uri; } + + public function currentUrl($withQueryString = true, $appName = 'default') + { + $app = Slim::getInstance($appName); + $req = $app->request(); + $uri = $req->getUrl() . $req->getPath(); + + if ($withQueryString) { + $env = $app->environment(); + + if ($env['QUERY_STRING']) { + $uri .= '?' . $env['QUERY_STRING']; + } + } + + return $uri; + } } diff --git a/composer.json b/composer.json index be06b74..2a1bf52 100644 --- a/composer.json +++ b/composer.json @@ -26,9 +26,8 @@ "twig/twig": "Twig templating system" }, "autoload": { - "psr-0": { - "Slim\\Views": "" + "psr-4": { + "Slim\\Views\\": "./" } - }, - "target-dir": "Slim/Views" + } }