TemplateResponse.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. /**
  36. * name of the template
  37. * @var string
  38. */
  39. protected $templateName;
  40. /**
  41. * parameters
  42. * @var array
  43. */
  44. protected $params;
  45. /**
  46. * rendering type (admin, user, blank)
  47. * @var string
  48. */
  49. protected $renderAs;
  50. /**
  51. * app name
  52. * @var string
  53. */
  54. protected $appName;
  55. /**
  56. * constructor of TemplateResponse
  57. * @param string $appName the name of the app to load the template from
  58. * @param string $templateName the name of the template
  59. * @param array $params an array of parameters which should be passed to the
  60. * template
  61. * @param string $renderAs how the page should be rendered, defaults to user
  62. * @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0
  63. */
  64. public function __construct($appName, $templateName, array $params=array(),
  65. $renderAs='user') {
  66. $this->templateName = $templateName;
  67. $this->appName = $appName;
  68. $this->params = $params;
  69. $this->renderAs = $renderAs;
  70. }
  71. /**
  72. * Sets template parameters
  73. * @param array $params an array with key => value structure which sets template
  74. * variables
  75. * @return TemplateResponse Reference to this object
  76. * @since 6.0.0 - return value was added in 7.0.0
  77. */
  78. public function setParams(array $params){
  79. $this->params = $params;
  80. return $this;
  81. }
  82. /**
  83. * Used for accessing the set parameters
  84. * @return array the params
  85. * @since 6.0.0
  86. */
  87. public function getParams(){
  88. return $this->params;
  89. }
  90. /**
  91. * Used for accessing the name of the set template
  92. * @return string the name of the used template
  93. * @since 6.0.0
  94. */
  95. public function getTemplateName(){
  96. return $this->templateName;
  97. }
  98. /**
  99. * Sets the template page
  100. * @param string $renderAs admin, user or blank. Admin also prints the admin
  101. * settings header and footer, user renders the normal
  102. * normal page including footer and header and blank
  103. * just renders the plain template
  104. * @return TemplateResponse Reference to this object
  105. * @since 6.0.0 - return value was added in 7.0.0
  106. */
  107. public function renderAs($renderAs){
  108. $this->renderAs = $renderAs;
  109. return $this;
  110. }
  111. /**
  112. * Returns the set renderAs
  113. * @return string the renderAs value
  114. * @since 6.0.0
  115. */
  116. public function getRenderAs(){
  117. return $this->renderAs;
  118. }
  119. /**
  120. * Returns the rendered html
  121. * @return string the rendered html
  122. * @since 6.0.0
  123. */
  124. public function render(){
  125. // \OCP\Template needs an empty string instead of 'blank' for an unwrapped response
  126. $renderAs = $this->renderAs === 'blank' ? '' : $this->renderAs;
  127. $template = new \OCP\Template($this->appName, $this->templateName, $renderAs);
  128. foreach($this->params as $key => $value){
  129. $template->assign($key, $value);
  130. }
  131. return $template->fetchPage();
  132. }
  133. }