Skip to content

Commit 39b297c

Browse files
committed
Merge with upstream
2 parents 148f3cd + 946a89c commit 39b297c

File tree

8 files changed

+185
-15
lines changed

8 files changed

+185
-15
lines changed

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ To learn how to write your own custom View class, visit the [Slim Framework docu
3636
require_once 'SmartyView.php';
3737

3838
//Init Slim app with the custom View
39-
Slim::init(array(
39+
$app = new Slim(array(
4040
'view' => new SmartyView()
4141
));
4242

Views/Extension/README.markdown

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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+
If you need to specify the appname for the getInstance method in the urlFor functions, set it as the third parameter of the function
23+
in your template:
24+
25+
<a href="{{ urlFor('hello', {"name": person.name, "age": person.age}, 'admin') }}">Hello {{ name }}</a>
26+
27+
The $twigExtensions take an array of extension class name which need to follow the naming convention starting with __Extension_Twig__,
28+
this might seem like a overkill to add Slim's urlFor but it makes organising your project easier as your project becomes larger.
29+
30+
## Smarty
31+
### How to use
32+
To use this in Smarty just include the code below at the top of your Slim index.php after including SmartyView.
33+
34+
SmartyView::$smartyExtensions = array(
35+
dirname(__FILE__) . '/Views/Extension/Smarty',
36+
);
37+
38+
Inside your Smarty template you would write:
39+
40+
{urlFor name="hello" options="name.Josh|age.26"}
41+
42+
You can easily pass variables that are arrays using the (.) or object using the (->) by doing:
43+
44+
<a href="{urlFor name="hello" options="name.{$person.name}|age.{$person.age}"}">Hello {$name}</a>
45+
46+
If you need to specify the appname for the getInstance method in the urlFor functions, set the appname parameter in your function:
47+
48+
<a href="{urlFor name="hello" appname="admin" options="name.{$person.name}|age.{$person.age}"}">Hello {$name}</a>
49+
50+
The $smartyExtensions take an array of extension directories, this follows the Smarty naming convention provided in the Smarty docs.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
$appName = isset($params['appname']) ? $params['appname'] : 'default';
15+
16+
$url = Slim::getInstance($appName)->urlFor($name);
17+
18+
if (isset($params['options']))
19+
{
20+
$options = explode('|', $params['options']);
21+
foreach ($options as $option) {
22+
list($key, $value) = explode('.', $option);
23+
$opts[$key] = $value;
24+
}
25+
26+
$url = Slim::getInstance($appName)->urlFor($name, $opts);
27+
}
28+
29+
return $url;
30+
}

Views/Extension/Twig/Slim.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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_Method($this, 'urlFor'),
14+
);
15+
}
16+
17+
public function urlFor($name, $params = array(), $appName = 'default')
18+
{
19+
return Slim::getInstance($appName)->urlFor($name, $params);
20+
}
21+
}

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/README.markdown

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@ The Slim Framework provides a default View class that uses PHP template files by
44

55
## TwigView
66

7-
The `TwigView` custom View class provides support for the [Twig](http://www.twig-project.org/) template library. You can use the TwigView custom View in your Slim application like this:
7+
The `TwigView` custom View class provides support for the [Twig](http://twig.sensiolabs.org/) template library. You can use the TwigView custom View in your Slim application like this:
88

99
<?php
1010
require 'slim/Slim.php';
1111
require 'views/TwigView.php';
12-
Slim::init('TwigView');
12+
$app = new Slim(array(
13+
'view' => 'TwigView'
14+
));
1315
//Insert your application routes here
14-
Slim::run();
16+
$app->run();
1517
?>
1618

1719
You will need to configure the `TwigView::$twigOptions` and `TwigView::$twigDirectory` class variables before using the TwigView class in your application. These variables can be found at the top of the `views/TwigView.php` class definition.
@@ -24,9 +26,11 @@ The `MustacheView` custom View class provides support for the [Mustache template
2426
require 'slim/Slim.php';
2527
require 'views/MustacheView.php';
2628
MustacheView::$mustacheDirectory = 'path/to/mustacheDirectory/';
27-
Slim::init('MustacheView');
29+
$app = new Slim(array(
30+
'view' => 'MustacheView'
31+
));
2832
//Insert your application routes here
29-
Slim::run();
33+
$app->run();
3034
?>
3135

3236
Before you can use the MustacheView class, you will need to set `MustacheView::$mustacheDirectory`. This property should be the relative or absolute path to the directory containing the `Mustache.php` library.
@@ -38,9 +42,11 @@ The `SmartyView` custom View class provides support for the [Smarty](http://www.
3842
<?php
3943
require 'slim/Slim.php';
4044
require 'views/SmartyView.php';
41-
Slim::init('SmartyView');
45+
$app = new Slim(array(
46+
'view' => 'SmartyView'
47+
));
4248
//Insert your application routes here
43-
Slim::run();
49+
$app->run();
4450
?>
4551

4652
You will need to configure the `SmartyView::$smartyDirectory`, `SmartyView::$smartyCompileDirectory` , `SmartyView::$smartyCacheDirectory` and optionally `SmartyView::$smartyTemplatesDirectory`, class variables before using the SmartyView class in your application. These variables can be found at the top of the `views/SmartyView.php` class definition.
@@ -52,11 +58,13 @@ The `BlitzView` custom View class provides support for the Blitz templating syst
5258
<?php
5359
require 'slim/Slim.php';
5460
require 'views/BlitzView.php';
55-
Slim::init('BlitzView');
61+
$app = new Slim(array(
62+
'view' => 'BlitzView'
63+
));
5664
//Insert your application routes here
57-
Slim::run();
65+
$app->run();
5866
?>
59-
67+
6068
Place your Blitz template files in the designated templates directory.
6169

6270
## HaangaView
@@ -66,11 +74,11 @@ The `HaangaView` custom View class provides support for the Haanga templating sy
6674
<?php
6775
require 'slim/Slim.php';
6876
require_once 'views/HaangaView.php';
69-
Slim::init(array(
77+
$app = new Slim(array(
7078
'view' => new HaangaView('/path/to/Haanga/dir', '/path/to/templates/dir', '/path/to/compiled/dir')
7179
));
7280
//Insert your application routes here
73-
Slim::run();
81+
$app->run();
7482
?>
7583

7684
## H2oView

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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function render( $template ) {
7373
/**
7474
* Creates new TwigEnvironment if it doesn't already exist, and returns it.
7575
*
76-
* @return TwigEnvironment
76+
* @return Twig_Environment
7777
*/
7878
public function getEnvironment() {
7979
if ( !$this->twigEnvironment ) {
@@ -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)