One of PHP's simplest template engines

Source: Internet
Author: User
Self-use template engine.
  1. Define (' App_path ', __dir__);
  2. Class template{
  3. private static $_vars;
  4. private static $_path;
  5. private static $_prefix;
  6. Private Function __construct () {}
  7. public static function init ($path = null) {
  8. if (Isset ($path) && ($path! = ")) Self::$_path=app_path. ' /templates/'. $path. ' /';
  9. Elseself::$_path = App_path. ' /templates/';
  10. Self::$_vars = Array ();
  11. }
  12. public static function Set_path ($path) {
  13. Self::$_path = $path;
  14. }
  15. public static function Set_prefix ($prefix) {
  16. Self::$_prefix = $prefix;
  17. }
  18. public static function assign ($key, $value = null)
  19. {if (!isset (Self::$_vars)) Self::init ();
  20. if (Is_array ($key)) Self::$_vars = Array_merge (Self::$_vars, $key);
  21. ElseIf ($key! = ") && (Isset ($value)))
  22. self::$_vars[$key] = $value;
  23. }
  24. public static function Fetch ($file) {
  25. if (!isset (Self::$_vars)) Self::init ();
  26. if (count (self::$_vars) >0)
  27. {Extract (Self::$_vars,extr_prefix_all,self::$_prefix);
  28. Self::$_vars = Array ();
  29. }
  30. Ob_start ();
  31. Include Self::$_path. $file;
  32. $contents = Ob_get_contents ();
  33. Ob_end_clean ();
  34. Self::$_path = null;
  35. Return preg_replace ('!\s+! ', ', $contents);
  36. }
  37. }
  38. ?>
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.