Skip to content

Commit 78386c0

Browse files
committed
Merge branch 'pedrobrazao-feature/current-path'
Closes #95
2 parents 383bea0 + c87f064 commit 78386c0

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ $app->run();
5858

5959
## Custom template functions
6060

61-
This component exposes a custom `path_for()` function to your Twig templates. You can use this function to generate complete URLs to any Slim application named route. This is an example Twig template:
61+
`TwigExtension` provides these functions to your Twig templates:
62+
63+
* `path_for()` - returns the URL for a given route.
64+
* `base_url()` - returns the `Uri` object's base URL.
65+
* `is_current_path()` - returns true is the provided route name and parameters are valid for the current path.
66+
* `current_path()` - renders the current path, with or without the query string.
67+
68+
69+
You can use `path_for` to generate complete URLs to any Slim application named route and use `is_current_path` to determine if you need to mark a link as active as shown in this example Twig template:
6270

6371
{% extends "layout.html" %}
6472

@@ -70,6 +78,7 @@ This component exposes a custom `path_for()` function to your Twig templates. Yo
7078
</ul>
7179
{% endblock %}
7280

81+
7382
## Testing
7483

7584
```bash

src/TwigExtension.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function getFunctions()
3737
new \Twig_SimpleFunction('path_for', array($this, 'pathFor')),
3838
new \Twig_SimpleFunction('base_url', array($this, 'baseUrl')),
3939
new \Twig_SimpleFunction('is_current_path', array($this, 'isCurrentPath')),
40+
new \Twig_SimpleFunction('current_path', array($this, 'currentPath')),
4041
];
4142
}
4243

@@ -60,6 +61,27 @@ public function isCurrentPath($name, $data = [])
6061
return $this->router->pathFor($name, $data) === $this->uri->getBasePath() . '/' . ltrim($this->uri->getPath(), '/');
6162
}
6263

64+
/**
65+
* Returns current path on given URI.
66+
*
67+
* @param bool $withQueryString
68+
* @return string
69+
*/
70+
public function currentPath($withQueryString = false)
71+
{
72+
if (is_string($this->uri)) {
73+
return $this->uri;
74+
}
75+
76+
$path = $this->uri->getBasePath() . '/' . ltrim($this->uri->getPath(), '/');
77+
78+
if ($withQueryString && '' !== $query = $this->uri->getQuery()) {
79+
$path .= '?' . $query;
80+
}
81+
82+
return $path;
83+
}
84+
6385
/**
6486
* Set the base url
6587
*

tests/TwigExtensionTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,35 @@ public function testIsCurrentPath($router, $uri, $name, $data, $expected)
4343

4444
$this->assertEquals($expected, $result);
4545
}
46+
47+
public function currentPathProvider()
48+
{
49+
$router = new Router();
50+
51+
$router->map(['GET'], '/hello/{name}', null)->setName('foo');
52+
$uri = Uri::createFromString('http://example.com/hello/world?a=b');
53+
54+
$uri2 = $uri->withBasePath('bar');
55+
$router->map(['GET'], '/bar/hello/{name}', null)->setName('bar');
56+
57+
return [
58+
[$router, '/foo', false, '/foo'],
59+
[$router, '/foo', true, '/foo'], // string based URI doesn't care about $withQueryString
60+
[$router, $uri, false, '/hello/world'],
61+
[$router, $uri, true, '/hello/world?a=b'],
62+
[$router, $uri2, false, '/bar/hello/world'],
63+
[$router, $uri2, true, '/bar/hello/world?a=b'],
64+
];
65+
}
66+
67+
/**
68+
* @dataProvider currentPathProvider
69+
*/
70+
public function testCurrentPath($router, $uri, $withQueryString, $expected)
71+
{
72+
$extension = new TwigExtension($router, $uri);
73+
$result = $extension->currentPath($withQueryString);
74+
75+
$this->assertEquals($expected, $result);
76+
}
4677
}

0 commit comments

Comments
 (0)