PHP Classes

PHP Router Template: Process template PHP scripts and fix file paths

Recommend this page to a friend!
  Info   Documentation   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
Not enough user ratingsTotal: 106 This week: 1All time: 9,707 This week: 57Up
Version License PHP version Categories
php-router-templete 1.0GNU General Publi...5PHP 5, Templates
Description 

Author

This package can process template PHP scripts and fix file paths.

It can take the values of several types settings and stores them in class variables.

The class can process a template by first assigning local variables to the values of the settings variables.

Then the class includes a given PHP script that is the template file that generates the template processing output with PHP code that processes the template variables.

The class can also compute a relative path of the current script relative to the so that path can be used as a parameter variable by the template script.

Innovation Award
PHP Programming Innovation award nominee
June 2022
Number 8
Processing templates is a common task that many PHP applications perform to generate the output, for instance, of each page.

In some cases, HTML templates may contain the paths of the URLs of JavaScript, images, and CSS files.

When these files use relative paths for these files, the correct path depends on the path of the current PHP script page URL.

The correct relative path may vary if the page URLs are inside each server directory.

This package implements an approach to simplify this situation. It can take a parameter that applications may path to specify the relative depth of external files that the current page uses.

Manuel Lemos
Picture of Ujah Chigozie peter
  Performance   Level  
Name: Ujah Chigozie peter <contact>
Classes: 28 packages by
Country: Nigeria Nigeria
Age: 33
All time rank: 198510 in Nigeria Nigeria
Week rank: 32 Up2 in Nigeria Nigeria Up
Innovation award
Innovation award
Nominee: 11x

Documentation

php-router-template

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

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

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

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

<?php 
/Secure template from unwanted access/
$ALLOW_ACCESS or die("Access Denied");
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Router template</title>
<!-- Unbreakable css path -->
<link href="<?php echo $root;?>assets/bootstrap/css/bootstrap.min.css" rel="stylesheet"/>
</head>
<body>
<h1>News - <?php echo $config::NAME;?></h1>
<?php 
  /Call Functions methods/
  $func->doStuff();
  
  /Call User methods/
  $user->doAnything()
  
  /Gets user information/
  $person->name;
  
  /Access options from template/
  echo $self["foo"];
  
   /Project root directory/
  echo $root;
?>
Unbreakable image 
<img src="<?php echo $root;?>assets/image/foo.png"/>

Unbreakable link 
<a href="<?php echo $root;?>newpage">A New Page</a>
</body>
</html>

  Files folder image Files (4)  
File Role Description
Files folder imagesrc (1 file)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Read me

  Files folder image Files (4)  /  src  
File Role Description
  Plain text file RouterTemplate.php Class Class source

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:106
This week:1
All time:9,707
This week:57Up