Skip to content

Commit

Permalink
Merge branch 'pedrobrazao-feature/current-path'
Browse files Browse the repository at this point in the history
Closes #95
  • Loading branch information
akrabat committed May 7, 2018
2 parents 383bea0 + c87f064 commit 78386c0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
11 changes: 10 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ $app->run();

## Custom template functions

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:
`TwigExtension` provides these functions to your Twig templates:

* `path_for()` - returns the URL for a given route.
* `base_url()` - returns the `Uri` object's base URL.
* `is_current_path()` - returns true is the provided route name and parameters are valid for the current path.
* `current_path()` - renders the current path, with or without the query string.


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:

{% extends "layout.html" %}

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


## Testing

```bash
Expand Down
22 changes: 22 additions & 0 deletions src/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public function getFunctions()
new \Twig_SimpleFunction('path_for', array($this, 'pathFor')),
new \Twig_SimpleFunction('base_url', array($this, 'baseUrl')),
new \Twig_SimpleFunction('is_current_path', array($this, 'isCurrentPath')),
new \Twig_SimpleFunction('current_path', array($this, 'currentPath')),
];
}

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

/**
* Returns current path on given URI.
*
* @param bool $withQueryString
* @return string
*/
public function currentPath($withQueryString = false)
{
if (is_string($this->uri)) {
return $this->uri;
}

$path = $this->uri->getBasePath() . '/' . ltrim($this->uri->getPath(), '/');

if ($withQueryString && '' !== $query = $this->uri->getQuery()) {
$path .= '?' . $query;
}

return $path;
}

/**
* Set the base url
*
Expand Down
31 changes: 31 additions & 0 deletions tests/TwigExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,35 @@ public function testIsCurrentPath($router, $uri, $name, $data, $expected)

$this->assertEquals($expected, $result);
}

public function currentPathProvider()
{
$router = new Router();

$router->map(['GET'], '/hello/{name}', null)->setName('foo');
$uri = Uri::createFromString('http://example.com/hello/world?a=b');

$uri2 = $uri->withBasePath('bar');
$router->map(['GET'], '/bar/hello/{name}', null)->setName('bar');

return [
[$router, '/foo', false, '/foo'],
[$router, '/foo', true, '/foo'], // string based URI doesn't care about $withQueryString
[$router, $uri, false, '/hello/world'],
[$router, $uri, true, '/hello/world?a=b'],
[$router, $uri2, false, '/bar/hello/world'],
[$router, $uri2, true, '/bar/hello/world?a=b'],
];
}

/**
* @dataProvider currentPathProvider
*/
public function testCurrentPath($router, $uri, $withQueryString, $expected)
{
$extension = new TwigExtension($router, $uri);
$result = $extension->currentPath($withQueryString);

$this->assertEquals($expected, $result);
}
}

0 comments on commit 78386c0

Please sign in to comment.