🍬 easy weezy templating engine for php 🍬
Caprice is PHP templating engine that aims to write clean PHP syntax along side with HTML code. caprice compiles the syntax and generate php files which means no performance loss but a clean html files with a friendly syntax.
- PHP 8 or newer versions
- PHPUnit >= 9 (for testing purpose)
- easy to use.
- friendly syntax.
- caching (one time compile).
- no performance loss.
composer require lotfio/caprice
composer test
use Caprice\Caprice;
require 'vendor/autoload.php';
$caprice = new Caprice;
// load caprice predefined directives
$caprice->loadPredefinedDirectives();
// set views location and cache location
$caprice->setCompileLocations('views', 'cache');
// helpful for development environment
$caprice->enableRecompile();
// file to compile => views/test.cap.php
// you can remove .cap.php extension for both
$compiled = $caprice->compile("test");
require $compiled; // require your compiled file
- you can write any php inside code blocks
#php
$var1 = "foo";
$var2 = "bar";
echo $var1 . " and " . $var2;
#endphp
{{ " hello caprice " }}
- if only
// if statement
#if ($condition)
// logic
#endif
- if else
// if statement
#if ($condition)
// if logic
#else
// else logic
#endif
- if elseif
#if ($condiftion)
// if logic
#elseif ($condition2)
// elseif logic
#else
// else logic
#endif
- for in loop value only
// for in loop key only
#for ($name in $array)
{{ $name }}
#endforin
- for in loop key, value
// for in loop key value
#for ($name => $age in $array)
{{ $name }} => {{ $age }}
#endforin
- for loop syntax
// for loop
#for ($i = 0; $i <= 10; $i++)
{{ $i }} <br>
#endfor
- while loop syntax
// while loop
#while ($condition)
// loop
#endwhile
- do while syntax
// do while
#do
{{ "do something" }}
#enddo($whileCondition)
// continue & break statements
#while (TRUE)
#if(condition) #continue #endif
#if(another_condition) #break #endif
#endwhile
// include/require statements
// you can remove .cap.php extension for both
// you use . to access folder instead of /
#require("file.cap.php")
#include("file.cap.php")
// extends a base layout
// here we are extending master.cap.php from layouts folder
#extends("layouts.master")
// load a section
#yield("sectionName")
// define a section
#section("sectionName")
// section content
#endsection
// functions
// dump
#dump($variable) OR #dd($variable)
- you can define your custom directives:
- make sure that the definition of your directives goes before calling compile
// simple directives
$caprice->directive("#test", function(){
return 'replace test directive with this string';
});
// expression directive
// example #call($var)
$caprice->directive("#call", function(string $expression, string $match, array $extras){
return '<?php call'. $expression . ';?>'; // this will evaluate to <?php call($var);\?\>
});
// class method directive
// MyDirective class should implement DirectiveInterface
$caprice->directive("#call", MyDirective::class);
- Adding helpers
- Adding unit test helpers
- Thank you for considering to contribute to Caprice. All the contribution guidelines are mentioned here.
- Here you can find the ChangeLog.
- Share Caprice and lets get more stars and more contributors.
- If this project helped you reduce time to develop, you can give me a cup of coffee :) : Paypal. 💖
- Caprice is an open-source software licensed under the MIT license.