You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
3.0 KiB

  1. <?php
  2. /**
  3. * Slim - a micro PHP 5 framework
  4. *
  5. * @author Josh Lockhart
  6. * @link http://www.slimframework.com
  7. * @copyright 2011 Josh Lockhart
  8. *
  9. * MIT LICENSE
  10. *
  11. * Permission is hereby granted, free of charge, to any person obtaining
  12. * a copy of this software and associated documentation files (the
  13. * "Software"), to deal in the Software without restriction, including
  14. * without limitation the rights to use, copy, modify, merge, publish,
  15. * distribute, sublicense, and/or sell copies of the Software, and to
  16. * permit persons to whom the Software is furnished to do so, subject to
  17. * the following conditions:
  18. *
  19. * The above copyright notice and this permission notice shall be
  20. * included in all copies or substantial portions of the Software.
  21. *
  22. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. */
  30. namespace Slim\Extras\Views;
  31. /**
  32. * SavantView
  33. *
  34. * The SavantView is a Custom View class that renders templates using the
  35. * Savant3 template language (http://phpsavant.com/).
  36. *
  37. * There are two fields that you, the developer, will need to change:
  38. * - savantDirectory
  39. * - savantOptions
  40. *
  41. * @package Slim
  42. * @author Matthew Callis <http://superfamicom.org/>
  43. */
  44. class Savant extends \Slim\View
  45. {
  46. /**
  47. * @var string The path to the directory containing Savant3.php and the Savant3 folder without trailing slash.
  48. */
  49. public static $savantDirectory = null;
  50. /**
  51. * @var array The options for the Savant3 environment, see http://phpsavant.com/api/Savant3/
  52. */
  53. public static $savantOptions = array('template_path' => 'templates');
  54. /**
  55. * @var persistent instance of the Savant object
  56. */
  57. private static $savantInstance = null;
  58. /**
  59. * Renders a template using Savant3.php.
  60. *
  61. * @see View::render()
  62. * @param string $template The template name specified in Slim::render()
  63. * @return string
  64. */
  65. public function render($template)
  66. {
  67. $savant = $this->getInstance();
  68. $savant->assign($this->data);
  69. return $savant->fetch($template);
  70. }
  71. /**
  72. * Creates new Savant instance if it doesn't already exist, and returns it.
  73. *
  74. * @throws RuntimeException If Savant3 lib directory does not exist.
  75. * @return SavantInstance
  76. */
  77. private function getInstance()
  78. {
  79. if (!self::$savantInstance) {
  80. if (!is_dir(self::$savantDirectory)) {
  81. throw new \RuntimeException('Cannot set the Savant lib directory : ' . self::$savantDirectory . '. Directory does not exist.');
  82. }
  83. require_once self::$savantDirectory . '/Savant3.php';
  84. self::$savantInstance = new \Savant3(self::$savantOptions);
  85. }
  86. return self::$savantInstance;
  87. }
  88. }