PHPackages                             lotfio/caprice - 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. lotfio/caprice

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

lotfio/caprice
==============

Caprice nice and easy weasy templating engine for php

1.1.2(4y ago)14491[1 issues](https://github.com/lotfio/caprice/issues)[1 PRs](https://github.com/lotfio/caprice/pulls)MITPHPPHP ^8.0

Since Oct 9Pushed 3mo ago2 watchersCompare

[ Source](https://github.com/lotfio/caprice)[ Packagist](https://packagist.org/packages/lotfio/caprice)[ RSS](/packages/lotfio-caprice/feed)WikiDiscussions master Synced 3d ago

READMEChangelog (7)Dependencies (2)Versions (12)Used By (0)

 [![caprice Preview](https://github.com/lotfio/caprice/raw/master/docs/logo.png)](https://github.com/lotfio/caprice/blob/master/docs/logo.png)

 [![License](https://camo.githubusercontent.com/6e0a9ea6c2bdfc1181309ec1d4e77ff67c60f89abb1e8617ea9c4f8937295a08/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e63652d4d49542d6666643332612e737667)](https://camo.githubusercontent.com/6e0a9ea6c2bdfc1181309ec1d4e77ff67c60f89abb1e8617ea9c4f8937295a08/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4c6963656e63652d4d49542d6666643332612e737667) [![PHP version](https://camo.githubusercontent.com/4acbce31b48027de8c99c158522a27bf3b307a407acb4587b7dffff33359ecb2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382d3830386539622e737667)](https://camo.githubusercontent.com/4acbce31b48027de8c99c158522a27bf3b307a407acb4587b7dffff33359ecb2/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d382d3830386539622e737667) [![Version](https://camo.githubusercontent.com/0030962a05db58903696f4223c672a6f59cc990dc1cb6e9d49a973c578965c36/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f56657273696f6e2d312e312e322d6635336235372e737667)](https://camo.githubusercontent.com/0030962a05db58903696f4223c672a6f59cc990dc1cb6e9d49a973c578965c36/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f56657273696f6e2d312e312e322d6635336235372e737667) [![Coverage](https://camo.githubusercontent.com/3fe569e575aa8650df84e4e7a56162f8819f566551b9e02f3310836532978d4b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d39382532352d3237616536302e737667)](https://camo.githubusercontent.com/3fe569e575aa8650df84e4e7a56162f8819f566551b9e02f3310836532978d4b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f636f7665726167652d39382532352d3237616536302e737667) [![Build Status](https://camo.githubusercontent.com/0046e016bb950a703955a8ca17e88c2fb7adaf2c663325545283c41fb148fbde/68747470733a2f2f6a656e6b696e732e6c6f7466696f2e6e65742f6275696c645374617475732f69636f6e3f6a6f623d636170726963652b2d2b6369)](https://camo.githubusercontent.com/0046e016bb950a703955a8ca17e88c2fb7adaf2c663325545283c41fb148fbde/68747470733a2f2f6a656e6b696e732e6c6f7466696f2e6e65742f6275696c645374617475732f69636f6e3f6a6f623d636170726963652b2d2b6369) [![Static Analysis](https://camo.githubusercontent.com/5391a0d5a0b13c6bff4328fa8e80d6bd9fbad745ee9d3c5f92039984ecd68205/68747470733a2f2f6a656e6b696e732e6c6f7466696f2e6e65742f6275696c645374617475732f69636f6e3f6a6f623d636170726963652b2d2b5073616c6d267375626a6563743d5073616c6d26636f6c6f723d6461726b74757271756f697365)](https://camo.githubusercontent.com/5391a0d5a0b13c6bff4328fa8e80d6bd9fbad745ee9d3c5f92039984ecd68205/68747470733a2f2f6a656e6b696e732e6c6f7466696f2e6e65742f6275696c645374617475732f69636f6e3f6a6f623d636170726963652b2d2b5073616c6d267375626a6563743d5073616c6d26636f6c6f723d6461726b74757271756f697365)

 **🍬 easy weezy templating engine for php 🍬**

🔥 Introduction :
================

[](#fire-introduction-)

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.

📌 Requirements :
================

[](#pushpin-requirements-)

- PHP 8 or newer versions
- PHPUnit &gt;= 9 (for testing purpose)

👌 Features :
============

[](#ok_hand-features-)

- easy to use.
- friendly syntax.
- caching (one time compile).
- no performance loss.

🚀 Installation &amp; Use :
==========================

[](#rocket-installation--use-)

```
    composer require lotfio/caprice
```

💥 testing :
===========

[](#-testing-)

```
    composer test
```

✏️ Usage :
==========

[](#pencil2-usage-)

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

📥 Available syntax directives:
==============================

[](#inbox_tray-available-syntax-directives)

### code block

[](#code-block)

- you can write any php inside code blocks

```
    #php
        $var1 = "foo";
        $var2 = "bar";
        echo $var1 . " and " . $var2;
    #endphp
```

### echo statement

[](#echo-statement)

```
    {{ " hello caprice " }}
```

### if statement

[](#if-statement)

- 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

[](#for-in-loop)

- 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

[](#for-loop)

- for loop syntax

```
    // for loop
    #for ($i = 0; $i 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 ''; // this will evaluate to
