lotfio/caprice

Caprice:简单易用的PHP模板引擎

1.1.2 2021-09-22 11:24 UTC

README

caprice Preview

License PHP version Version Coverage Build Status Static Analysis

🍬 简单易用的PHP模板引擎 🍬

🔥 简介

Caprice是一个PHP模板引擎,旨在与HTML代码一起编写简洁的PHP语法。Caprice将语法编译生成PHP文件,这意味着没有性能损失,但有整洁的HTML文件和友好的语法。

📌 要求

  • PHP 8或更高版本
  • PHPUnit >= 9(用于测试目的)

👌 特点

  • 易于使用。
  • 友好的语法。
  • 缓存(一次性编译)。
  • 无性能损失。

🚀 安装 & 使用

    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

📥 可用语法指令

代码块

  • 您可以在代码块中编写任何PHP代码
    #php
        $var1 = "foo";
        $var2 = "bar";
        echo $var1 . " and " . $var2;
    #endphp

echo语句

    {{ " hello caprice " }}

if语句

  • 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循环

  • for in循环值
    // for in loop key only
    #for ($name in $array)
        {{ $name }}
    #endforin
  • for in循环键、值
    // for in loop key value
    #for ($name => $age in $array)
        {{ $name }} => {{ $age }}
    #endforin

for循环

  • for循环语法
    // for loop
    #for ($i = 0; $i <= 10; $i++)
        {{ $i }} <br>
    #endfor

while循环

  • while循环语法
    // while loop
    #while ($condition)
        // loop
    #endwhile

do while循环

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

continue & break循环

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

include / require语句

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

✋ 自定义指令

  • 您可以定义自己的自定义指令
  • 确保您的指令定义在调用编译之前
   // 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

  • 添加助手
  • 添加单元测试助手

💻 贡献

  • 感谢您考虑为Caprice做出贡献。所有贡献指南都列在这里

📃 变更日志

🍺 支持开发

  • 分享Caprice并让我们获得更多星标和贡献者。
  • 如果您觉得这个项目帮助您节省了开发时间,您可以给我一杯咖啡 :) : Paypal. 💖

📋 许可证

  • Caprice是一个开源软件,许可协议为MIT许可证