Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement requireJS and requireCSS #120

Merged
merged 9 commits into from
Apr 6, 2017
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
16 changes: 11 additions & 5 deletions docs/app.rst
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Will be true if application is currently rendering recursively through the Rende
Links
=====

.. php:method:: url(page, extension)
.. php:method:: url(page)

Method to generate links between pages. Specified with associative array::

Expand All @@ -40,8 +40,6 @@ this method must respond with a properly formatted url such as::

contact.php?from=John+Smith

If your pages use extension other than .php, then you should pass that extension too.

You may redefine this metod if you are using beautiful URLs and advanced
routing::

Expand All @@ -54,11 +52,19 @@ Includes

.. php:method:: requireJS($url)

Method to include additional JavaScript file in page.
Method to include additional JavaScript file in page::

$app->requireJS('https://code.jquery.com/jquery-3.1.1.js');
$app->requireJS('https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.10/semantic.min.js');

Using of CDN servers is always better than storing external libraries locally.
Most of the time CDN servers are faster (cached) and more reliable.

.. php:method:: requireCSS($url)

Method to include additional CSS stylesheet in page.
Method to include additional CSS stylesheet in page::

$app->requireCSS('http://semantic-ui.com/dist/semantic.css');

Hooks
=====
Expand Down
28 changes: 11 additions & 17 deletions src/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,6 @@ class App

public $ui_persistence = null;

/**
* If you specify a string, then it will be considered a filename
* from which to load the template.
*
* @var string
*/
public $defaultTemplate = 'html.html';

/**
* Constructor.
*
Expand Down Expand Up @@ -165,9 +157,12 @@ public function initLayout($layout, $options = [])
}
$layout->app = $this;

$this->html = new View(['defaultTemplate' => $this->defaultTemplate]);
$this->html->app = $this;
$this->html->init();
if (!$this->html) {
$this->html = new View(['defaultTemplate' => 'html.html']);
$this->html->app = $this;
$this->html->init();
}

$this->layout = $this->html->add($layout);

return $this;
Expand Down Expand Up @@ -273,12 +268,11 @@ public function loadTemplate($name)
/**
* Build a URL that application can use for call-backs.
*
* @param array|string $args List of new GET arguments
* @param string $extension Default file extension
* @param array|string $args List of new GET arguments
*
* @return string
*/
public function url($args = [], $extension = 'php')
public function url($args = [])
{
if (is_string($args)) {
$args = [$args];
Expand All @@ -291,7 +285,7 @@ public function url($args = [], $extension = 'php')
$page = $args[0];
unset($args[0]);

$url = $page ? ($page.($extension ? '.'.$extension : '')) : '';
$url = $page ? $page.'.php' : '';

$args = http_build_query($args);

Expand All @@ -311,7 +305,7 @@ public function url($args = [], $extension = 'php')
*/
public function requireJS($url)
{
$this->html->template->appendHTML('HEAD', '<script src="'.$this->url($url, null).'"></script>');
$this->html->template->appendHTML('HEAD', '<script src="'.$url.'"></script>');
Copy link
Member

Choose a reason for hiding this comment

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

wonder if we can use getTag here...


return $this;
}
Expand All @@ -325,7 +319,7 @@ public function requireJS($url)
*/
public function requireCSS($url)
{
$this->html->template->appendHTML('HEAD', '<link rel="stylesheet" type="text/css" href="'.$this->url($url, null).'">');
$this->html->template->appendHTML('HEAD', '<link rel="stylesheet" type="text/css" href="'.$url.'">');

return $this;
}
Expand Down