This plugin provides a few URL functions for use within Plates templates.
First, download the library via Composer:
composer require basteyy/plates-url-toolset
Of course, you can download the library manually and include it in your project. But I recommend using Composer.
To make the URL tools available, load the extension into your Plates scope:
/** @var \League\Plates\Engine $engine */
$engine->loadExtension(new \basteyy\PlatesUrlToolset\PlatesUrlToolset());
Customize the extension by setting options when loading it:
/** @var \League\Plates\Engine $engine */
$engine->loadExtension(
new \basteyy\PlatesUrlToolset\PlatesUrlToolset(
null, // The base URL for all URLs. Should be something like https://example.org
true, // Change the default value for using absolute URLs
[] // Register named URLs for later use
));
For all the examples below, I use example.org
as the hypothetical website URL.
echo $this->getCurrentUrl(); // Displays the current URL
Result:
http://example.org/foobar
echo $this->getLink('/foo', 'Click it!!', 'Yes, click me', 'btn big', false);
Result:
<a href="/foo" title="Yes, click me" class="btn big">Click it!!</a>
// Register the link in the constructor or via
$this->addNamedUrl('linkname', 'foobar');
echo $this->getNamedUrl('linkname');
echo $this->getNamedUrl('linkname', true);
Result:
/foobar
https://example.org/foobar
// Register the link in the constructor or via
$this->addNamedUrl('linkname', 'foobar-123456789');
echo $this->getNamedLink('linkname', 'Click it!!', 'Yes, click me', 'btn big', false);
Result:
<a href="/foobar-123456789" title="Yes, click me" class="btn big">Click it!!</a>
// Register the link in the constructor or via
echo $this->getDebugUrl('/foobar/file/something.css');
echo $this->getDebugUrl('/foobar/file/bar.jpg', true, 'blabla', 'aaaa');
Result:
/foobar/file/something.css?request_time=1234567
(current timestamp)
https://example.org/foobar/file/bar.jpg?blabla=aaaa
The MIT License (MIT). Please see License File for more information.
Feel free to contribute to this project. Just create a pull request with your changes.