Skip to content

Commit 7760127

Browse files
committed
Updated Smarty and Twig view to allow newly added Extension directory with urlFor extensions
1 parent 2d08661 commit 7760127

File tree

6 files changed

+147
-1
lines changed

6 files changed

+147
-1
lines changed

Views/Extension/README.markdown

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# View Extensions
2+
This is where you can define all you custom functions in the template parser method of choice, I have currently included
3+
Twig and Smarty to allow Slim's urlFor() funtion in the template(view). You can add additional functions, filters, plugins to
4+
the Extension directory under the template parser directory of choice.
5+
6+
## Twig
7+
### How to use
8+
To use this in Twig just include the code below at the top of your Slim index.php file after including TwigView.
9+
10+
TwigView::$twigExtensions = array(
11+
'Extension_Twig_Slim',
12+
);
13+
14+
Inside your Smarty template you would write:
15+
16+
{{ urlFor('hello', {"name": "Josh", "age": "19"}) }}
17+
18+
You can easily pass variables that are objects or arrays by doing:
19+
20+
<a href="{{ urlFor('hello', {"name": person.name, "age": person.age}) }}">Hello {{ name }}</a>
21+
22+
The $twigExtensions take an array of extension class name which need to follow the naming convention starting with __Extension_Twig__,
23+
this might seem like a overkill to add Slim's urlFor but it makes organising your project easier as your project becomes larger.
24+
25+
## Smarty
26+
### How to use
27+
To use this in Smarty just include the code below at the top of your Slim index.php after including SmartyView.
28+
29+
SmartyView::$smartyExtensions = array(
30+
dirname(__FILE__) . '/Views/Extension/Smarty',
31+
);
32+
33+
Inside your Smarty template you would write:
34+
35+
{urlFor name="hello" options="name.Josh|age.26"}
36+
37+
You can easily pass variables that are arrays using the (.) or object using the (->) by doing:
38+
39+
<a href="{urlFor name="hello" options="name.{$person.name}|age.{$person.age}"}">Hello {$name}</a>
40+
41+
The $smartyExtensions take an array of extension directories, this follows the Smarty naming convention provided in the Smarty docs.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/*
3+
* Smarty plugin
4+
* -------------------------------------------------------------
5+
* File: function.urlFor.php
6+
* Type: function
7+
* Name: urlFor
8+
* Purpose: outputs url for a function with the defined name method
9+
* -------------------------------------------------------------
10+
*/
11+
function smarty_function_urlFor($params, $template)
12+
{
13+
$name = isset($params['name']) ? $params['name'] : '';
14+
$url = Slim::getInstance()->urlFor($name);
15+
16+
if (isset($params['options']))
17+
{
18+
$options = explode('|', $params['options']);
19+
foreach ($options as $option) {
20+
list($key, $value) = explode('.', $option);
21+
$opts[$key] = $value;
22+
}
23+
24+
$url = Slim::getInstance()->urlFor($name, $opts);
25+
}
26+
27+
return $url;
28+
}

Views/Extension/Twig/Slim.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
class Extension_Twig_Slim extends Twig_Extension
4+
{
5+
public function getName()
6+
{
7+
return 'slim';
8+
}
9+
10+
public function getFunctions()
11+
{
12+
return array(
13+
'urlFor' => new Twig_Function_Function('Slim::getInstance()->urlFor'),
14+
);
15+
}
16+
}

Views/Extension/TwigAutoloader.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Twig.
5+
*
6+
* (c) 2009 Fabien Potencier
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
/**
13+
* Autoloads Twig Extensions classes.
14+
*
15+
* @package twig
16+
* @author Fabien Potencier <[email protected]>
17+
*/
18+
class Twig_Extensions_Autoloader
19+
{
20+
/**
21+
* Registers Twig_Extensions_Autoloader as an SPL autoloader.
22+
*/
23+
static public function register()
24+
{
25+
ini_set('unserialize_callback_func', 'spl_autoload_call');
26+
spl_autoload_register(array(new self, 'autoload'));
27+
}
28+
29+
/**
30+
* Handles autoloading of classes.
31+
*
32+
* @param string $class A class name.
33+
*
34+
* @return boolean Returns true if the class has been loaded
35+
*/
36+
static public function autoload($class)
37+
{
38+
if (0 !== strpos($class, 'Extension_Twig')) {
39+
return;
40+
}
41+
42+
if (file_exists($file = dirname(dirname(__FILE__)) . '/' . str_replace('_', '/', $class).'.php')) {
43+
require $file;
44+
}
45+
}
46+
}

Views/SmartyView.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,11 @@ class SmartyView extends Slim_View {
6565
*/
6666
public static $smartyTemplatesDirectory = 'templates';
6767

68+
/**
69+
* @var SmartyExtensions The Smarty extensions directory you want to load plugins from
70+
*/
71+
public static $smartyExtensions = array();
72+
6873
/**
6974
* @var persistent instance of the Smarty object
7075
*/
@@ -99,6 +104,9 @@ public static function getInstance() {
99104
require_once self::$smartyDirectory . '/Smarty.class.php';
100105
self::$smartyInstance = new Smarty();
101106
self::$smartyInstance->template_dir = is_null(self::$smartyTemplatesDirectory) ? $this->getTemplatesDirectory() : self::$smartyTemplatesDirectory;
107+
if ( self::$smartyExtensions ) {
108+
self::$smartyInstance->setPluginsDir(self::$smartyExtensions);
109+
}
102110
if ( self::$smartyCompileDirectory ) {
103111
self::$smartyInstance->compile_dir = self::$smartyCompileDirectory;
104112
}
@@ -110,4 +118,4 @@ public static function getInstance() {
110118
}
111119
}
112120

113-
?>
121+
?>

Views/TwigView.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ public function getEnvironment() {
8484
$loader,
8585
self::$twigOptions
8686
);
87+
88+
require_once dirname(__FILE__) . '/Extension/TwigAutoloader.php';
89+
Twig_Extensions_Autoloader::register();
90+
91+
foreach (self::$twigExtensions as $ext) {
92+
$this->twigEnvironment->addExtension(new $ext);
93+
}
8794
}
8895
return $this->twigEnvironment;
8996
}

0 commit comments

Comments
 (0)