项目作者: senselogic

项目描述 :
Lightweight language and template engine which compiles to PHP.
高级语言: D
项目地址: git://github.com/senselogic/PHOENIX.git
创建时间: 2017-07-23T07:10:48Z
项目社区:https://github.com/senselogic/PHOENIX

开源协议:Other

下载


Phoenix

Lightweight language and template engine which compiles to PHP.

Description

Phoenix is a lightweight language and template engine which allows to program PHP web servers with a more concise and readable syntax.

Features

  • Keeps line numbers unchanged to ease development and debugging.
  • Extracts inline scripts and style sheets into external files.
  • Generates readable code, completely identical to handcrafted code.
  • Integrates easily and seamlessly with existing code.
  • Automatically watches file modifications for instant recompilation.

Sample

  1. <html>
  2. <head>
  3. <meta charset="utf8"/>
  4. <link rel="stylesheet" href="style.css">
  5. </head>
  6. <style file="style.styl">
  7. .text-color-gold
  8. {
  9. color: #C226D5 + 120deg - 20%;
  10. }
  11. .text-color-green
  12. {
  13. color: #84D5B9 - 50%;
  14. }
  15. </style>
  16. <body>
  17. <?
  18. local
  19. hello,
  20. hobbit,
  21. hobbit_count,
  22. message,
  23. bold,
  24. dwarf_name_array;
  25. import 'imported.php';
  26. include 'included.php';
  27. include 'included.php';
  28. import? 'notfound.php';
  29. include? 'notfound.php';
  30. hello = 'Hello';
  31. function GetMessage(
  32. string first_name,
  33. string last_name
  34. )
  35. {
  36. global
  37. hello;
  38. static local
  39. dear = 'dear';
  40. local
  41. message;
  42. message = hello .. ' ' .. dear .. ' ' .. first_name .. ' ' .. last_name .. ' !';
  43. return message;
  44. }
  45. class HOBBIT
  46. {
  47. attribute
  48. FirstName = '',
  49. LastName = '',
  50. RingCount = 0;
  51. static attribute
  52. HobbitCount = 0;
  53. method constructor(
  54. string first_name,
  55. string last_name,
  56. int ring_count
  57. )
  58. {
  59. .FirstName = first_name;
  60. .LastName = last_name;
  61. .RingCount = ring_count;
  62. self::AddHobbit();
  63. }
  64. method destructor()
  65. {
  66. .RemoveRings( .RingCount );
  67. self::RemoveHobbit();
  68. }
  69. method AddRings(
  70. int ring_count
  71. )
  72. {
  73. .RingCount += ring_count;
  74. }
  75. method RemoveRings(
  76. int ring_count
  77. )
  78. {
  79. .RingCount -= ring_count;
  80. }
  81. static method AddHobbit()
  82. {
  83. self::HobbitCount++;
  84. }
  85. static method RemoveHobbit()
  86. {
  87. self::HobbitCount--;
  88. }
  89. static method GetHobbitCount()
  90. {
  91. return self::HobbitCount;
  92. }
  93. }
  94. hobbit = new HOBBIT( 'Froddo', 'Baggins', 0 );
  95. hobbit.FirstName = 'Bilbo';
  96. hobbit.AddRings( 1 );
  97. hobbit_count = HOBBIT::GetHobbitCount();
  98. message = GetMessage( hobbit.FirstName, hobbit.LastName );
  99. echo '<p>' .. message .. '</p>';
  100. bold = '<b>bold</b>';
  101. dwarf_name_array = array( 'Balin', 'Dwalin', 'Oin', 'Gloin' );
  102. ?>
  103. <div>
  104. <p>
  105. There is <% hobbit_count %> hobbit.
  106. </p>
  107. <p>
  108. <% hobbit.FirstName .. ' ' .. hobbit.LastName %> has <% hobbit.RingCount %> ring.
  109. </p>
  110. <p>
  111. <% message %>
  112. </p>
  113. <p>
  114. <# bold #>
  115. </p>
  116. <p class="text-color-green">
  117. There are <% count( dwarf_name_array ) %> dwarves :
  118. </p>
  119. <ul>
  120. <? foreach ( var dwarf_name; dwarf_name_array ) { ?>
  121. <li class="text-color-gold">
  122. <% dwarf_name %>
  123. </li>
  124. <? } ?>
  125. </ul>
  126. </div>
  127. </body>
  128. </html>

Syntax

Most of the PHP syntax is kept unchanged, except that :

  • .phx files contain Phoenix code.
  • .pht files contain a mix of HTML and Phoenix code.
  • .. is used to concatenate strings.
  • . is used to access class members.
  • this is implicit when accessing class attributes and methods.
  • $ is implicit when using static attributes and declared variables.
  • # prevents the dollar insertion.
  • var declares a local variable at first use.
  • local declares local variables.
  • attribute declares class attributes.
  • method declares a class method.
  • constructor is the constructor method.
  • destructor is the destructor method.
  • foreach ( value; array ) is the foreach loop syntax.
  • include includes a file with a relative path.
  • include? includes a file with a relative path, if it exists.
  • import includes a file once with a relative path.
  • import? includes a file once with a relative path, if it exists.
  • <? ?> wraps Phoenix statements.
  • <% %> wraps an escaped expression to output.
  • <# #> wraps an unescaped expression to output.

Limitations

  • Local variables and class attributes must be declared.
  • The PHP files are generated without grammatical checking.

Installation

Install the DMD 2 compiler (using the MinGW setup option on Windows).

Build the executable with the following command line :

  1. dmd -m64 phoenix.d

Command line

  1. phoenix [options] <input folder> <output_folder>

Options

  1. --extract <tag> <script folder> : extract scripts inside this tag if they have a `file` attribute
  2. --compress : compress lines
  3. --trim : trim lines
  4. --create : create the PHP folders if needed
  5. --watch : watch the Phoenix files for modifications
  6. --pause 500 : time to wait before checking the Phoenix files again

Examples

  1. phoenix --extract style STYLE/ --compress --trim --create PHX/ PHP/

Converts the Phoenix files of the PHX/ folder into matching PHP files in the PHP/ folder,
extracting style sheets into the STYLE/ folder and creating the output folders if needed.

  1. phoenix --extract style STYLE/ --compress --trim --create --watch PHX/ PHP/

Converts the Phoenix files of the PHX/ folder into matching PHP files in the PHP/ folder,
extracting style sheets into the STYLE/ folder and creating the output folders if needed,
then watches the Phoenix files for modifications.

Version

1.0

Author

Eric Pelzer (ecstatic.coder@gmail.com).

License

This project is licensed under the GNU General Public License version 3.

See the LICENSE.md file for details.