PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/org/tubepress/template/SimpleTemplate.class.php

http://tubepress.googlecode.com/
PHP | 68 lines | 41 code | 8 blank | 19 comment | 3 complexity | 8e84da340464f3b3e5917707a0be544e MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-4.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright 2006 - 2010 Eric D. Hough (http://ehough.com)
  4. *
  5. * This file is part of TubePress (http://tubepress.org)
  6. *
  7. * TubePress is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * TubePress is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with TubePress. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. function_exists('tubepress_load_classes')
  22. || require(dirname(__FILE__) . '/../../../tubepress_classloader.php');
  23. tubepress_load_classes(array('org_tubepress_util_StringUtils',
  24. 'org_tubepress_template_Template'));
  25. class org_tubepress_template_SimpleTemplate implements org_tubepress_template_Template
  26. {
  27. private $_source;
  28. private $_path;
  29. public function __construct()
  30. {
  31. $this->_source = array();
  32. }
  33. public function setPath($path)
  34. {
  35. if (!is_readable($path)) {
  36. throw new Exception("Cannot read template at $path");
  37. }
  38. $this->_path = $path;
  39. }
  40. public function setVariable($name, $value)
  41. {
  42. $this->_source[$name] = $value;
  43. }
  44. public function toString()
  45. {
  46. if (!isset($this->_path)) {
  47. throw new Exception('Can\'t build template when no file is set');
  48. }
  49. ob_start();
  50. extract($this->_source);
  51. include realpath($this->_path);
  52. $result = ob_get_contents();
  53. ob_end_clean();
  54. return org_tubepress_util_StringUtils::removeEmptyLines($result);
  55. }
  56. public function reset()
  57. {
  58. $this->_source = array();
  59. }
  60. }