Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add scripts and styles to template response
Added the capability of passing required scripts and styles to the template response.
  • Loading branch information
Unknown committed Aug 29, 2016
commit 172488abcbfba3e8abb16612aaccceab36c9e9e5
57 changes: 55 additions & 2 deletions lib/public/AppFramework/Http/TemplateResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,37 @@ class TemplateResponse extends Response {
*/
protected $appName;

/**
* styles (vendor, app)
* @var array
*/
protected $styles;

/**
* scripts (vendor, app)
* @var array
*/
protected $scripts;

/**
* constructor of TemplateResponse
* @param string $appName the name of the app to load the template from
* @param string $templateName the name of the template
* @param array $params an array of parameters which should be passed to the
* template
* @param string $renderAs how the page should be rendered, defaults to user
* @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0
* @param array $styles an array of styles which sould be added to the template
* @param array $scripts an array of scripts which should be added to the template
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This then needs to be removed.

* @since 6.0.0 - parameters $params and $renderAs were added in 7.0.0 - parameters $styles and $scripts were added in 11.0.0
*/
public function __construct($appName, $templateName, array $params=array(),
$renderAs='user') {
$renderAs='user', array $styles=array(), array $scripts=array()) {
$this->templateName = $templateName;
$this->appName = $appName;
$this->params = $params;
$this->renderAs = $renderAs;
$this->styles = $styles;
$this->scripts = $scripts;
}


Expand Down Expand Up @@ -154,7 +170,44 @@ public function render(){
$template->assign($key, $value);
}

$this->addScriptsAndStyles();

return $template->fetchPage();
}


/**
* adds the vendor and app scripts and styles to the templateresponse
* @since 11.0.0
*/
private function addScriptsAndStyles(){
if (array_key_exists('vendor', $this->scripts)){
foreach ($this->scripts['vendor'] as $application){
\OC_Util::addVendorScript($application);
}
}

if (array_key_exists('app', $this->scripts)) {
foreach ($this->scripts['app'] as $appName => $files) {
foreach ($files as $file) {
\OC_Util::addScript($appName, $file);
}
}
}

if (array_key_exists('vendor', $this->styles)) {
foreach ($this->styles['vendor'] as $application) {
\OC_Util::addVendorStyle($application);
}
}

if (array_key_exists('app', $this->styles)) {
foreach ($this->styles['app'] as $appName => $files) {
foreach ($files as $file) {
\OC_Util::addStyle($appName, $file);
}
}
}
}

}