1
0
mirror of https://github.com/flarum/core.git synced 2025-10-11 06:54:26 +02:00

Allow custom variables to be set on the client app

This commit is contained in:
Toby Zerner
2015-07-31 20:08:27 +09:30
parent 513d896f51
commit e204794b91
7 changed files with 92 additions and 39 deletions

View File

@@ -26,13 +26,6 @@ class ClientView implements Renderable
*/
protected $title;
/**
* An API response that should be preloaded into the page.
*
* @var null|array|object
*/
protected $document;
/**
* The SEO content of the page, displayed in <noscript> tags.
*
@@ -47,6 +40,20 @@ class ClientView implements Renderable
*/
protected $layout;
/**
* An API response that should be preloaded into the page.
*
* @var null|array|object
*/
protected $document;
/**
* Other variables to preload into the page.
*
* @var array
*/
protected $variables = [];
/**
* An array of JS modules to import before booting the app.
*
@@ -122,17 +129,6 @@ class ClientView implements Renderable
$this->title = $title;
}
/**
* Set an API response to be preloaded into the page. This should be a
* JSON-API document.
*
* @param null|array|object $document
*/
public function setDocument($document)
{
$this->document = $document;
}
/**
* Set the SEO content of the page, to be displayed in <noscript> tags.
*
@@ -173,6 +169,28 @@ class ClientView implements Renderable
$this->footStrings[] = $string;
}
/**
* Set an API response to be preloaded into the page. This should be a
* JSON-API document.
*
* @param null|array|object $document
*/
public function setDocument($document)
{
$this->document = $document;
}
/**
* Set a variable to be preloaded into the app.
*
* @param string $name
* @param mixed $value
*/
public function setVariable($name, $value)
{
$this->variables[$name] = $value;
}
/**
* Add a JavaScript module to be imported before the app is booted.
*
@@ -210,10 +228,16 @@ class ClientView implements Renderable
$data = array_merge($data, $this->getDataFromDocument($user));
}
$view->data = $data;
$view->session = $this->getSession();
$view->app = [
'preload' => [
'data' => $data,
'session' => $this->getSession(),
'document' => $this->document
]
] + $this->variables;
$view->bootstrappers = $this->bootstrappers;
$view->title = ($this->title ? $this->title . ' - ' : '') . $forum->data->attributes->title;
$view->document = $this->document;
$view->forum = $forum->data;
$view->layout = app('view')->file($this->layout, ['forum' => $forum->data]);
$view->content = $this->content;
@@ -223,7 +247,6 @@ class ClientView implements Renderable
$view->head = implode("\n", $this->headStrings);
$view->foot = implode("\n", $this->footStrings);
$view->bootstrappers = $this->bootstrappers;
return $view->render();
}