项目作者: SaemonZixel

项目描述 :
eXtensible PHP Templates - template engine like XSLT, but with BEM philosophy like XJST or BH
高级语言: PHP
项目地址: git://github.com/SaemonZixel/xphpt.git
创建时间: 2017-07-21T18:49:26Z
项目社区:https://github.com/SaemonZixel/xphpt

开源协议:

下载


xphpt

eXtensible PHP Templates - template engine like XSL-T, but with BEM philosophy like XJST or BH

Stable version: 0.6

Requires: PHP 5.2+

Template example (BEM style)

Contents of 404notfound.phpt file:

  1. --DESCRIPTION--
  2. Page 404 Not found!
  3. --MATCH--
  4. $block == 'request'
  5. --PHP--
  6. <?php
  7. return array(
  8. 'block' => 'page',
  9. 'tag' => 'html',
  10. 'attrs' => array('xmlns' => 'http://www.w3.org/1999/xhtml'),
  11. 'html_before' => '<!doctype html>',
  12. 'content' => array(
  13. array(
  14. 'block' => 'head',
  15. 'tag' => 'head',
  16. 'content' => array(
  17. array(
  18. 'tag' => 'meta',
  19. 'attrs' => array(
  20. 'http-equiv' => 'Content-Type',
  21. 'content' => 'text/html; charset=UTF-8')
  22. ),
  23. array(
  24. 'elem' => 'title',
  25. 'tag' => 'title',
  26. 'attrs' => array('class' => null), // removes the class attribute
  27. 'content' => array('404 Not Found!')
  28. )
  29. )
  30. ),
  31. array(
  32. 'block' => 'body',
  33. 'tag' => 'body',
  34. 'content' => array(
  35. array(
  36. 'elem' => 'content',
  37. 'html' => '<h1>Page not found!</h1><p>Sorry...</p>'
  38. )
  39. )
  40. )
  41. )
  42. );
  43. ?>

Common use case (BEM style)

Contents of index.php file:

  1. <?php
  2. include 'xphpt.php';
  3. $config = array(
  4. 'templates' => $_SERVER['DOCUMENT_ROOT'].'/xphptpls',
  5. 'templates_cache' => $_SERVER['DOCUMENT_ROOT'].'/xphptpls-cache', // if many templates
  6. 'apply_traversal_mode' => 'bem' // default mode
  7. );
  8. $bem_array = array(
  9. 'block' => 'request',
  10. 'req_uri' => $_SERVER['REQUEST_URI']
  11. );
  12. echo applyCtx($bem_array, $config);
  13. exit;
  14. ?>

Template example (XSL-T style)

Contents of 404notfound.phpt file:

  1. --DESCRIPTION--
  2. Blog template
  3. --MATCH--
  4. $block == 'response'
  5. --PHP--
  6. <!doctype html>
  7. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="ru">
  8. <head>
  9. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  10. <meta name="Robots" content="index,follow" />
  11. <title><?php echo isset($blog_title) ? htmlspecialchars($blog_title) : 'My blog'; ?></title>
  12. </head>
  13. <body>
  14. <h1><?php echo isset($blog_title) ? htmlspecialchars($blog_title) : 'My blog'; ?></h1>
  15. <?php apply_templates($blog_posts); ?>
  16. </body>
  17. </html>
  18. --MATCH_BLOG_POST--
  19. isset($post_type) and $post_type == 'post'
  20. isset($post_status) and $post_status == 'publish'
  21. --PHP_BLOG_POST--
  22. <article id="post_<?php echo $ID ?>">
  23. <h2><?php echo $post_title; ?></h2>
  24. <date><?php echo $post_date; ?></date>
  25. <div>
  26. <?php echo $post_content."\n"; ?>
  27. </div>
  28. </article>

Common use case (XSL-T style)

Contents of index.php file:

  1. <?php
  2. include 'xphpt.php';
  3. $data = array(
  4. 'block' => 'response',
  5. 'blog_title' => 'My test blog',
  6. 'blog_posts' =>array(
  7. array('ID' => 1, 'post_date' => '2018-01-01 09:00:00', 'post_type' => 'post', 'post_status' => 'publish', 'post_title' => 'First post', 'post_content' => 'Text for first post...'),
  8. array('ID' => 2, 'post_date' => '2018-01-01 10:00:00', 'post_type' => 'post', 'post_status' => 'publish', 'post_title' => 'Second post', 'post_content' => 'Text for second post...'),
  9. array('ID' => 3, 'post_date' => '2018-01-01 10:00:00', 'post_type' => 'post', 'post_status' => 'draft', 'post_title' => 'Third post', 'post_content' => 'Text for third post... (draft)')
  10. )
  11. );
  12. $mode = '';
  13. $params = array();
  14. $config = array(
  15. 'templates' => $_SERVER['DOCUMENT_ROOT'].'/xphptpls',
  16. 'templates_cache' => $_SERVER['DOCUMENT_ROOT'].'/xphptpls-cache', // if many templates
  17. 'apply_traversal_mode' => 'xslt'
  18. );
  19. echo apply_templates($data, $mode, $params, $config);
  20. exit;
  21. ?>

Debugging

  • Line number is preserved in error messages!
  • You can set $GLOBALS[‘xphpt_debug’] = true to show additional info.
  • You can set $GLOBALS[‘xphpt_debug_apply_call_limit’] = N for protect by infinity cyclec.