PHPackages                             peterujah/php-router-template - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. peterujah/php-router-template

ActiveLibrary[Templating &amp; Views](/categories/templating)

peterujah/php-router-template
=============================

A simple php class to help render template in routers and fix additional slash issue.

1.6(3y ago)114MITPHPPHP &gt;=5.3.0

Since Jun 6Pushed 3y ago2 watchersCompare

[ Source](https://github.com/peterujah/php-router-template)[ Packagist](https://packagist.org/packages/peterujah/php-router-template)[ Docs](https://github.com/peterujah/php-router-template)[ RSS](/packages/peterujah-php-router-template/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (6)DependenciesVersions (7)Used By (0)

php-router-template
===================

[](#php-router-template)

A simple php class to help render template in routers and fix additional slash issue.

### IMPORTANT

[](#important)

You do not have to install, download nor use this class, i wrote the class for my personal needs, the first time i starting using router in my project instead of old ways. The only important the about this class is `deep` method which fixed the issue with extra shlash as posted on stackoverflow question here [style disappear when i add slash / after route](https://stackoverflow.com/questions/64298425/style-disappear-when-i-add-slash-after-route)

And making it easy for me to access any defined global variables within the template since am not using any php framework. So please just ignore the project, even though i have claerly documented the class and showed usage sample, it because i like wasing my time on beautiful usles codes and likes to wrap all my ever writing functions is a class because i might need it again. What about composer installation? Yah i know, it free so i used.

Installation is super-easy via Composer:

```
composer require peterujah/php-router-templete
```

USAGES
======

[](#usages)

Initialize RouterTemplate with the necessary parameters and register your custom classes.

```
$template = new \Peterujah\NanoBlock\RouterTemplate(__DIR__, false);
$template->addUser(new User(User::LIVE))->addFunc(new Functions())->addConfig(new Config());
```

Render template by passing the directory deep method as the first parameter while optional options array will be the second parameter.

```
$template->Render("home")->with($template->deep(1));
```

A shorthand to the above method should be using `withDept` method and only passing the directory dept integer as the first parameter while optional options array will be the second parameter.

```
$template->Render("home")->view(1);
```

Using the class with [Bramus Router](https://github.com/bramus/router) or any other php router as you wish. Initialize your router instance

```
$router = new \Bramus\Router\Router();
```

Render hompage template using `with` and `deep` method.

```
$router->get('/', function() use ($template) {
    $template->Build("home")->with($template->deep(0));
});
```

Render hompage template using `view` method.

```
$router->get('/', function() use ($template) {
    $template->Build("home")->view(0);
});
```

Render update product template with product id as the second url parameter.

```
$router->get('/update/([a-zA-Z0-9]+)', function($id) use ($template) {
    $template->Build("product")->view(1);
    /*
      Using with method below
      $template->Build("product")->with($template->deep(1));
    */
});
```

Render update product template with product id as second url parameter and passing additional options to the template.

```
$router->get('/update/([a-zA-Z0-9]+)', function($id) use ($template) {
    $template->Build("product")->view(1, [
      "foo" => "Our Foo"
      "bar" => "Our Bar id {$id}"
    ]);
});
```

Accessing all global variables within a template file `/router/product.php`.

```

Router template
