lotfio/caprice

Caprice nice and easy weasy templating engine for php

1.1.2 2021-09-22 11:24 UTC

README

caprice Preview

License PHP version Version Coverage Build Status Static Analysis

🍬 easy weezy templating engine for php 🍬

🔥 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 :

  • PHP 8 or newer versions
  • PHPUnit >= 9 (for testing purpose)

👌 Features :

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

🚀 Installation & Use :

    composer require lotfio/caprice

💥 testing :

    composer test

✏️ 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:

code block

  • you can write any php inside code blocks
    #php
        $var1 = "foo";
        $var2 = "bar";
        echo $var1 . " and " . $var2;
    #endphp

echo statement

    {{ " hello caprice " }}

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 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 syntax
    // for loop
    #for ($i = 0; $i <= 10; $i++)
        {{ $i }} <br>
    #endfor

while loop

  • while loop syntax
    // while loop
    #while ($condition)
        // loop
    #endwhile

do while loop

  • do while syntax
    // do while 
    #do
        {{ "do something" }}
    #enddo($whileCondition)

continue & break loop

    // continue & break statements
    #while (TRUE)
        #if(condition) #continue #endif
        #if(another_condition) #break #endif
    #endwhile

include / require statements

    // 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")

layout

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

helpers

    // functions
    // dump
    #dump($variable) OR #dd($variable)

✋ Custom directives:

  • 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);

🚁 TODO

  • Adding helpers
  • Adding unit test helpers

💻 Contributing

  • Thank you for considering to contribute to Caprice. All the contribution guidelines are mentioned here.

📃 ChangeLog

🍺 Support the development

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

📋 License

  • Caprice is an open-source software licensed under the MIT license.