ICHI PHP TEMPLATE is the fast and secure Pure PHP Template Library
This package is Open Source According to MIT license
- Acknowledgement
- Installation
- Set Up Template Base Directory Path
- Showing Views
- Showing Error Messages
- Showing Success Messages
- Section And Content
- Sharing Data In All Views
- Preventing XSS Attack
- Components
- Show Old Request Data
I really thanks to my mother for everything. She is the best for me.
composer require jijihohococo/ichi-template
You can set up the base directory path for your all template view files.
It is highly recommend to set up template base directory path before using functions of ICHI TEMPLATE
The base directory path will be used in calling all views
use JiJiHoHoCoCo\IchiTemplate\Template\View;
// example //
View::setPath(__DIR__.'/../views/');
It doesn't matter if you want to use another base directory path name.
After setting template base path for views, you can directly call the files under this base path directly.
For example,you have 'show_data.php' under your template base path.
You can directly call this file in showing views directly without including directory path if you had set template base path.
You can show the views in your function like that
You must include full directory path if you don't set up the template base path
Using 'view' function can only apply section and content template style in that called view php file.
Without Data
public function showData(){
//
return view('show_data.php');
}
With Data
public function showData(){
return view('show_data.php',[
'data' => 'Hello World'
]);
}
You can also call your php file without '.php' file extension
public function showData(){
return view('show_data');
}
You can use called data in your view file which is calling from "view" function
In your_view_php_file_path
echo $data; // Hello World
If you don't want to use template system but want to show only the views. You can do as shown as below
You can also use following functions within your view php file which is called from 'view' function
Without Data
includeView('include_file.php');
With Data
includeView('include_file.php',[
'data' => 'Hello World'
]);
You can also call your php file without '.php' file extension
includeView('include_file');
'includeView' function will show the file by using 'include' function 'includeOnceView' function will show the by using 'include_once' function 'requireView' function will show the file by using 'require' function 'requireOnceView' function will show the file by using 'require_once' function
You can add error messages
setErrors([
'name_error' => 'name is required',
'email_error' => 'email is required'
]);
You can get error messages with 'errors' array variable in your view php files
if(isset($errors['name_error'])){
echo $errors['name_error']. '<br>';
}
if(isset($errors['email_error'])){
echo $errors['email_error'] . '<br>';
}
You can add success messages
setSuccess([
'message' => 'registeration is completed'
]);
You can get sucess messages with 'success' array variable in your view php views
if(isset($success['message'])){
echo $success['message'];
}
You can apply template system as shown as below
Firstly, you must show your view php file
return view('show_data.php');
In your template main php file
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<?php y('content'); ?>
<?php includeView('admin/layouts/footer.php'); ?>
</body>
</html>
In your view php file
<?php
extend('main.php');
section('content');
?>
<p>Content</p>
<?php endSection(); ?>
You can also call your php file without '.php' file extension
extend('main');
In your view php file, You must call your template main php file with 'extend' function firstly. You must add 'content' name in your 'section' function. After using 'section' function and write your frontend stuffs you must use 'endSection' function
In your template main php file, you must call 'y' function with the content name that you want to show.
Both view php file and main layout file will be shown.
If you want to change page title dynamicatlly according to view php file
In your view php file
section('content','Content Page');
In your template main php file
<head>
<title><?php echo title(); ?></title>
</head>
You can share the data (variables) in all your views
use JiJiHoHoCoCo\IchiTemplate\Template\View;
View::share([
'writer' => 'John',
'book' => 'New Book'
]);
And calling 'view' function
return view('show_data.php');
You can use the share data as variables in your view php file
echo $writer . '<br>';
echo $book;
You can prevent your string data output from xss attack
echo e($data);
You can use class as your component to show view php files
In your component class
namespace App\Components;
use JiJiHoHoCoCo\IchiTemplate\Component\Component;
class TestComponent extends Component{
public function render(){
return view('componet_view.php');
}
}
You can create the component class with the commandline.
Firstly you need to created the file named "ichi" under your project folder and use the below code in this file
#!/usr/bin/env php
<?php
require __DIR__.'/vendor/autoload.php';
use JiJiHoHoCoCo\IchiTemplate\Command\TemplateCommand;
$templateCommand=new TemplateCommand;
$templateCommand->run(__DIR__,$argv);
And then you can create the component in your commandline
php ichi make:component TestComponent
The default file folder is "app/Components". So after making command, the component you created will be in the this default file folder. If you want to change the default folder path, you can change it in your "ichi" file.
$templateCommand=new TemplateCommand;
$templateCommand->setPath('new_app/Components');
$templateCommand->run(__DIR__,$argv);
In your view php file, you can now call your component
component('App\Components\TestComponent');
You can use constructor in your component class to pass the data
In your component class
namespace App\Components;
use JiJiHoHoCoCo\IchiTemplate\Component\Component;
class TestComponent extends Component{
private $name;
public function __construct(string $name){
$this->name=$name;
}
public function render(){
return view('componet_view.php',[
'name' => $this->name
]);
}
}
In your view php file
component('App\Components\TestComponent',[
'name' => 'Test Data'
]);
You can set the base directory path for your component classes.
use JiJiHoHoCoCo\IchiTemplate\Component\ComponentSetting;
ComponentSetting::setPath('App\Components');
So you can call only component class name when you use 'component' function
component('TestComponent',[
'name' => 'Test Data'
]);
You can show old request data after submitting in your php file
<input type="text" name="test" value="<?php old('test'); ?>">
If you want to set default data if the request is not isset
old('test','Default Test');
This function is aimed to use in create and update data forms.