Skip to content

Commit 946a89c

Browse files
committed
Updated to allow multiple app use with urlFor function
1 parent 7760127 commit 946a89c

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

Views/Extension/README.markdown

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ You can easily pass variables that are objects or arrays by doing:
1919

2020
<a href="{{ urlFor('hello', {"name": person.name, "age": person.age}) }}">Hello {{ name }}</a>
2121

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+
2227
The $twigExtensions take an array of extension class name which need to follow the naming convention starting with __Extension_Twig__,
2328
this might seem like a overkill to add Slim's urlFor but it makes organising your project easier as your project becomes larger.
2429

@@ -38,4 +43,8 @@ You can easily pass variables that are arrays using the (.) or object using the
3843

3944
<a href="{urlFor name="hello" options="name.{$person.name}|age.{$person.age}"}">Hello {$name}</a>
4045

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+
4150
The $smartyExtensions take an array of extension directories, this follows the Smarty naming convention provided in the Smarty docs.

Views/Extension/Smarty/function.urlFor.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@
1111
function smarty_function_urlFor($params, $template)
1212
{
1313
$name = isset($params['name']) ? $params['name'] : '';
14-
$url = Slim::getInstance()->urlFor($name);
14+
$appName = isset($params['appname']) ? $params['appname'] : 'default';
15+
16+
$url = Slim::getInstance($appName)->urlFor($name);
1517

1618
if (isset($params['options']))
1719
{
@@ -21,7 +23,7 @@ function smarty_function_urlFor($params, $template)
2123
$opts[$key] = $value;
2224
}
2325

24-
$url = Slim::getInstance()->urlFor($name, $opts);
26+
$url = Slim::getInstance($appName)->urlFor($name, $opts);
2527
}
2628

2729
return $url;

Views/Extension/Twig/Slim.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ public function getName()
1010
public function getFunctions()
1111
{
1212
return array(
13-
'urlFor' => new Twig_Function_Function('Slim::getInstance()->urlFor'),
13+
'urlFor' => new Twig_Function_Method($this, 'urlFor'),
1414
);
1515
}
16+
17+
public function urlFor($name, $params = array(), $appName = 'default')
18+
{
19+
return Slim::getInstance($appName)->urlFor($name, $params);
20+
}
1621
}

0 commit comments

Comments
 (0)