basteyy/plates-url-toolset

A few functions for url handling in plates

1.0.0 2024-10-14 15:51 UTC

This package is auto-updated.

Last update: 2024-11-14 16:02:36 UTC


README

This plugin provides a few URL functions for use within Plates templates.

Setup

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.

Usage

Load Extension

To make the URL tools available, load the extension into your Plates scope:

/** @var \League\Plates\Engine $engine */
$engine->loadExtension(new \basteyy\PlatesUrlToolset\PlatesUrlToolset());

Load Options

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
    ));

Usages Inside the Templates

For all the examples below, I use example.org as the hypothetical website URL.

Get the Current URL

echo $this->getCurrentUrl(); // Displays the current URL

Result:

http://example.org/foobar

Create an HTML Link

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>

Get a Named URL

// 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

Get a Named HTML Link

// 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>

Get a URL with a Debugging Timestamp Appended

// 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

License

The MIT License (MIT). Please see License File for more information.

Contributing

Feel free to contribute to this project. Just create a pull request with your changes.

Support

ko-fi