PHP Classes

PHP Style Template Engine: Process templates compiling them into PHP scripts

Recommend this page to a friend!
  Info   View files Example   View files View files (17)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2023-08-21 (9 months ago) RSS 2.0 feedNot enough user ratingsTotal: 45 All time: 10,773 This week: 74Up
Version License PHP version Categories
style 1.0MIT/X Consortium ...7Templates, Code Generation, PHP 7
Description 

Author

This package can process templates compiling them into PHP scripts.

It can process a template file to replace template marks into PHP code to output the processed template data.

The package can also execute the processed template PHP scripts to generate the processed template output result.

Currently, the template engine supports marks that can:

- Custom expressions that allow defining callback functions to handle custom marks

- Sections that define content to be loaded from separate template sections

- Hard-compiling by injecting PHP code in a template that executes when the template is processed

- Including templates and passing specific parameter values using PHP code

- Iterate templates using object variable values

- Functions to output specific HTML tags like, for instance, forms

- Display PHP variable values

- Display template parameters encoded as HTML

- Etc...

Picture of Mohamed Nabil
Name: Mohamed Nabil <contact>
Classes: 1 package by
Country: Egypt Egypt
Age: 28
All time rank: 449652 in Egypt Egypt
Week rank: 234 Up4 in Egypt Egypt Up

Example

<?php


use Style\Style;



//view you want to compile must has .stl at the end of it is name

$style = new Style('template/','template/temp/');

// add new template role using the below callback function that has the full experssion in $capt var

$style->addTempRole('test','\~ob',function($capt){
      return
$capt[0].' ppppppppppppppppppoboobobo';
});

//render view

$style->render('page_sections',[]);


Details

Style

By Mohamed Nabil (https://github.com/PHPMohamedNabil/)!

[Style] is lightweight a tiny PHP Template Engine you can use for small projects or educational purposes.

Feel the power of the template engines of big libraries in your code with simple and flexible usage and little code.

Features

  • Simple compiling tags {$variable}, {#constant}, @include(), {%loop $data%}, {%if%}, [ comment ],, {%func echo str_len($string)%}
  • Very Easy template Compiling as one class called Style just loads the full template and compiling it.
  • new feature to send data to other template file so that be injected into other template in every page load.
  • Easy to inject a new experissions fell free to add as many as you want.
  • Secure when printing variables , as its filtered html content against some xss attacks.

Table of contents

<!--ts--> * Installation * Usage

  * Custom Expressions
  * Sections
  * Hard Compiling
  * Including View
  * Foreach loop
  * Html Creation
  * Printing Vars
  * Terminate the code
  * Printing html Content
  * Table of expressions

* Licence <!--te-->

Installation

  1. Install composer https://github.com/composer/composer
  2. Create a composer.json inside your application folder:

    
    

Usage

Create a Style instance by passing it the folder where your view files are located, and a cache folder. Render a template by calling the render method.

use Style\Style;

$style = new Style('template/','template/temp/');

$style->render('page_sections',[]);

Custom-Expressions

You can also add custom expressions using the addTempRole() function:

$style->addTempRole('test','\~ob',function($capt){
	  return $capt[0].'  ppppppppppppppppppoboobobo';
});
$style->render('page_sections',[]);

Which allows you to use the following in your template:

 here the ppppppppp : ~ob

Sections

You can also use extend views and using @spread(parent_view_name)

@spread('layout')

using also @sections @addsection to send data from child to parent view

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>layout page</title>
</head>
<body>

<p class="yield">
    <!-- this will yield data and print it here form child view -->
	@addsection('content')
</p>

</html>
@spread('layout')

<!-- add data to the main view and render show it -->
@section('content')
  My first paragraph in parent view 
@endsection

Hard-compiling-Feature

you can now send data from one view to another one as it will be compiled and hardcoded example :

in the view main you will write the below expression that when view main.stl.php page loaded or compilled

The view test will be injected by random number in every main.stl.php page load within h1 tag that has class title


@hardcompile(test[] within h1:title data:"echo mt_rand(1,1000)")

results in test.stl.php


<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Test</title>
</head>
<body>

<h1 class="title">
681 <!-- this a random number hardcoded by main view  -->
</h1>
    

hard compilling can be (before | after | within) the specified tag in other view to be injected

you can send data to other view like this :


@hardcompile(test['name'=>$name,$title] before h1:title data:"echo mt_rand(1,1000)")

including-view

get other view included in view page

@display('main',['data'=>$data])

foreach-loop

in tempaltes

<div class="">
@foreach($users as $user)
  {$user->username}
@endforeach
</div>

Html-Creation

you can now create form with its input data

[php]
       print \Style\Style::form('/',[
        'method'=>'post',
        'enctype'=>'multipart/form-data',
        'id'=>'first-form'
   ])->formInput('username',['class'=>'form-control','type'=>'text'])->formInput('password',['class'=>'form-control','type'=>'password'])->formInput('file',['class'=>'form-input-file','type'=>'file'])->renderForm();

  [/php]

will output:

<form action="/" method="post" enctype="multipart/form-data" id="first-form">

		<input name="username" class="form-control" type="text">

		<input name="password" class="form-control" type="password">

		<input name="file" class="form-input-file" type="file">

</form>

Printing-Vars

{$var_name}

Terminate-the-code

of view like die you can use @backwithfalse it is just converted to return false and exit from code any code or html after it will not be executed

printing-html-content

without stopping entities you can print html code witout escaping it the main reason of it if you want to show a post content or has a block of html code to be appear and effected by browser you can use {@$post@} as an expample:

<div class="blog-post-content">
{@$posts->post_content@}
</div>

Expressions-of-statments:

| Expression | Description | | --- | --- | | {$var} | for printing the variable var with escaping against xss | | {%$var%} | printing var or any string escaping or filtering it | | {@$var@} | printing var or any string without escaping it or filtering it | | {%var='name'} | define a variable inside the view :$var='name'| | {%func echo ucfirst($var)%} |execute the function or echo it echo word is optional if you want to echo the function| | [comment]ww [/comment] | any thing in between it will not be compilled| | [php] var_dump($arr); [/php] | write php code| | {%if $var>0%} | define if statment| | {%else%},{%elseif%} and {%endif%} | define else or elseif statment and you can use endif statment to end the statment| | @addsection($name) |used in layout or parent view to implement section content that will be printed later in child view | | @spread($name) | extend the parent view in the child view | | @section($name) | start the section in child view | | @endsection($name) | end the section in child view | | @foeach | start the for each loop | | @endforeach | end the for each loop | | @for() | start the for loop | | @endfor | end the for loop | | @while() | start while statment | | @endwhile | end while statment | | @switch($var) | start the switch statment | | @case($name) | case condition inside switch statment | | @break | break the statment or the loop | | @continue | continue the statment or the loop | | @default | default condition inside switch statment | | @backwithfalse | it is just converted to return false and exit from code any code or html after it will not be executed | | @hardcompile(view_name[] before\|after\|within tagname:classname data:"php_code_here") | hard compiling other view_name and inject data content before or after or within tagname that has a classname this will send data to other view on every exacute of this experission |

Licence

published under the MIT Licence.


  Files folder image Files  
File Role Description
Files folder imagesrc (6 files, 2 directories)
Files folder imagetest (1 file, 1 directory)
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. Documentation

  Files folder image Files  /  src  
File Role Description
Files folder imageExceptions (3 files)
Files folder imageinterfaces (1 file)
  Plain text file CompileSectionsTrait.php Class Class source
  Plain text file CompileSpecialExperssionsTrait.php Class Class source
  Accessible without login Plain text file helpers.php Aux. Auxiliary script
  Plain text file HtmlBuliderTrait.php Class Class source
  Plain text file Style.php Class Class source
  Plain text file StyleEngine.php Class Class source

  Files folder image Files  /  src  /  Exceptions  
File Role Description
  Plain text file NoAccessToDeleteCacheException.php Class Class source
  Plain text file NoAccessToWriteException.php Class Class source
  Plain text file ViewNotFoundException.php Class Class source

  Files folder image Files  /  src  /  interfaces  
File Role Description
  Plain text file CustomRuleInterface.php Class Class source

  Files folder image Files  /  test  
File Role Description
Files folder imagetemplate (3 files)
  Accessible without login Plain text file index.php Example Example script

  Files folder image Files  /  test  /  template  
File Role Description
  Accessible without login Plain text file lay.stl.php Aux. Auxiliary script
  Accessible without login Plain text file page_sections.stl.php Aux. Auxiliary script
  Accessible without login Plain text file test.stl.php Aux. Auxiliary script

 Version Control Unique User Downloads Download Rankings  
 100%
Total:45
This week:0
All time:10,773
This week:74Up