TemplateResponse.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  6. * @author Morris Jobke <hey@morrisjobke.de>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. * @author Thomas Tanghus <thomas@tanghus.net>
  9. *
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. /**
  26. * Public interface of ownCloud for apps to use.
  27. * AppFramework\HTTP\TemplateResponse class
  28. */
  29. namespace OCP\AppFramework\Http;
  30. /**
  31. * Response for a normal template
  32. * @since 6.0.0
  33. */
  34. class TemplateResponse extends Response {
  35. const EVENT_LOAD_ADDITIONAL_SCRIPTS = self::class . '::loadAdditionalScripts';
  36. const EVENT_LOAD_ADDITIONAL_SCRIPTS_LOGGEDIN = self::class . '::loadAdditionalScriptsLoggedIn';
  37. /**
  38. * name of the template
  39. * @var string
  40. */
  41. protected $templateName;
  42. /**
  43. * parameters
  44. * @var array
  45. */
  46. protected $params;
  47. /**
  48. * rendering type (admin, user, blank)
  49. * @var string
  50. */
  51. protected $renderAs;
  52. /**
  53. * app name
  54. * @var string
  55. */
  56. protected $appName;
  57. /**
  58. * constructor of TemplateResponse
  59. * @param string $appName the name of the app to load the template from
  60. * @param string $templateName the name of the template
  61. * @param array $params an array of parameters which should be passed to the
  62. * template
  63. * @param string $renderAs how the page should be rendered, defaults to user
  64. * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0
  65. */
  66. public function __construct($appName, $templateName, array $params=array(),
  67. $renderAs='user') {
  68. $this->templateName = $templateName;
  69. $this->appName = $appName;
  70. $this->params = $params;
  71. $this->renderAs = $renderAs;
  72. }
  73. /**
  74. * Sets template parameters
  75. * @param array $params an array with key => value structure which sets template
  76. * variables
  77. * @return TemplateResponse Reference to this object
  78. * @since 6.0.0 - return value was added in 7.0.0
  79. */
  80. public function setParams(array $params){
  81. $this->params = $params;
  82. return $this;
  83. }
  84. /**
  85. * Used for accessing the set parameters
  86. * @return array the params
  87. * @since 6.0.0
  88. */
  89. public function getParams(){
  90. return $this->params;
  91. }
  92. /**
  93. * Used for accessing the name of the set template
  94. * @return string the name of the used template
  95. * @since 6.0.0
  96. */
  97. public function getTemplateName(){
  98. return $this->templateName;
  99. }
  100. /**
  101. * Sets the template page
  102. * @param string $renderAs admin, user or blank. Admin also prints the admin
  103. * settings header and footer, user renders the normal
  104. * normal page including footer and header and blank
  105. * just renders the plain template
  106. * @return TemplateResponse Reference to this object
  107. * @since 6.0.0 - return value was added in 7.0.0
  108. */
  109. public function renderAs($renderAs){
  110. $this->renderAs = $renderAs;
  111. return $this;
  112. }
  113. /**
  114. * Returns the set renderAs
  115. * @return string the renderAs value
  116. * @since 6.0.0
  117. */
  118. public function getRenderAs(){
  119. return $this->renderAs;
  120. }
  121. /**
  122. * Returns the rendered html
  123. * @return string the rendered html
  124. * @since 6.0.0
  125. */
  126. public function render(){
  127. // \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
  128. $renderAs = $this->renderAs === 'blank' ? '' : $this->renderAs;
  129. $template = new \OCP\Template($this->appName, $this->templateName, $renderAs);
  130. foreach($this->params as $key => $value){
  131. $template->assign($key, $value);
  132. }
  133. return $template->fetchPage($this->params);
  134. }
  135. }