Skip to content

Commit 474d859

Browse files
committed
Added fallbacks to template loading
1 parent 318b4c0 commit 474d859

File tree

16 files changed

+84
-52
lines changed

16 files changed

+84
-52
lines changed

CHANGELOG.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,14 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
## [1.1.0] - XXXX-XX-XX
88
### Added
99
- Renderers to facilitate integrations of template-engines:
10-
- Added integration to `twig/twig` through `TwigRenderer` class renderer (defined as default);
10+
- Added `Renderer` interface;
11+
- Added `TwigRenderer` that integrates `twig/twig`;
12+
- Added fallback template loading support.
1113
- Template packs to facilitate customization and extensibility of templates:
12-
- Added template pack `default` and `bootstrap4`.
13-
- Support to configure default renderer and default template pack through `Config` singleton class;
14+
- Added template pack `default` and defined as fallback;
15+
- Added template pack `bootstrap4` that integrates custom elements of Bootstrap v4.0.0-beta.2.
16+
- Added extra arg `label` on method `getContext` of `Widget` class;
17+
- Support to configure renderer and template pack through `Config` singleton class;
1418

1519
### Changed
1620
- Class name `PHPFormConfig` to `Config` and moved to `src/` directory;

src/Config.php

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,73 +6,70 @@
66
class Config extends Singleton
77
{
88
/**
9-
* Template packs available
9+
* @var string Directory with templates
1010
*/
11-
const TEMPLATE_PACKS = array("default", "bootstrap4");
11+
protected $templates_dir = __DIR__ . '/templates/';
1212

1313
/**
14-
* Directory with templates
15-
* @var string
14+
* @var string Default fallback template pack
1615
*/
17-
protected $templates_dir = __DIR__ . '/templates/';
16+
protected $fallback_template_pack = "default";
1817

1918
/**
20-
* Template pack defined
21-
* @var string
19+
* @var string Default template pack defined. If null, fallback is used.
2220
*/
23-
protected $template_pack = "default";
21+
protected $template_pack = null;
2422

2523
/**
26-
* Renderer class used to render html content
27-
* @var string
24+
* @var string Renderer class used to render html content
2825
*/
2926
protected $renderer_class = TwigRenderer::class;
3027

3128
/**
32-
* Renderer instance based on $renderer_class.
33-
* @var PHPForm\Renderers\Renderer
29+
* @var PHPForm\Renderers\Renderer Renderer instance based on $renderer_class.
3430
*/
3531
private $renderer;
3632

3733
/**
3834
* Templates path accordingly with defined template pack.
35+
*
3936
* @return string
4037
*/
41-
private function getTemplatesPath()
38+
private function buildPath($template_pack)
4239
{
43-
$path = $this->templates_dir . $this->template_pack . "/";
40+
$path = $this->templates_dir . $template_pack . "/";
4441

4542
if (!file_exists($path)) {
46-
trigger_error("Template dir '$path' don't exists.", E_USER_ERROR);
43+
trigger_error("Template pack dir '$path' don't exists.", E_USER_ERROR);
4744
}
4845

4946
return $path;
5047
}
5148

5249
/**
5350
* Return renderer class instantiated
51+
*
5452
* @return PHPForm\Renderers\Renderer
5553
*/
5654
public function getRenderer()
5755
{
5856
if (is_null($this->renderer)) {
59-
$this->renderer = new $this->renderer_class($this->getTemplatesPath());
57+
$fallback_pack_dir = $this->buildPath($this->fallback_template_pack);
58+
$pack_dir = !is_null($this->template_pack) ? $this->buildPath($this->template_pack) : null;
59+
60+
$this->renderer = new $this->renderer_class($fallback_pack_dir, $pack_dir);
6061
}
6162

6263
return $this->renderer;
6364
}
6465

6566
/**
6667
* Define template pack
68+
*
6769
* @param string
6870
*/
6971
public function setTemplatePack($template_pack)
7072
{
71-
if (!in_array($template_pack, self::TEMPLATE_PACKS)) {
72-
$packs = implode(", ", self::TEMPLATE_PACKS);
73-
trigger_error("Template pack '$template_pack' not valid. Available packs are: $packs", E_USER_ERROR);
74-
}
75-
7673
$this->template_pack = $template_pack;
7774
}
7875
}

src/Fields/BoundField.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ protected function asWidget($widget = null, array $attrs = array())
7777

7878
$attrs = $this->buildWidgetAttrs($attrs);
7979

80-
return $widget->render($this->html_name, $this->getValue(), $attrs);
80+
return $widget->render($this->html_name, $this->getValue(), $this->label, $attrs);
8181
}
8282

8383
public function labelTag($contents = null, array $attrs = array())

src/Renderers/Renderer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
*/
55
namespace PHPForm\Renderers;
66

7-
abstract class Renderer
7+
interface Renderer
88
{
9-
abstract public function getTemplate($template_name);
10-
abstract public function render($template_name, $context);
9+
public function __construct(string $fallback_templates_dir, string $templates_dir);
10+
public function getTemplate(string $template_name);
11+
public function render(string $template_name, array $context);
1112
}

src/Renderers/TwigRenderer.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,31 @@
66

77
use Twig_Loader_Filesystem;
88
use Twig_Environment;
9+
use Twig_Loader_Chain;
910

10-
class TwigRenderer extends Renderer
11+
class TwigRenderer implements Renderer
1112
{
12-
private $loader;
1313
private $twig;
1414

15-
public function __construct($templates_dir)
15+
public function __construct(string $fallback_templates_dir, string $templates_dir = null)
1616
{
17-
$this->loader = new Twig_Loader_Filesystem($templates_dir);
18-
$this->twig = new Twig_Environment($this->loader);
17+
$loaders = new Twig_Loader_Chain();
18+
19+
if (!is_null($templates_dir)) {
20+
$loaders->addLoader(new Twig_Loader_Filesystem($templates_dir));
21+
}
22+
23+
$loaders->addLoader(new Twig_Loader_Filesystem($fallback_templates_dir));
24+
25+
$this->twig = new Twig_Environment($loaders);
1926
}
2027

21-
public function getTemplate($template_name)
28+
public function getTemplate(string $template_name)
2229
{
2330
return $this->twig->load($template_name);
2431
}
2532

26-
public function render($template_name, $context)
33+
public function render(string $template_name, array $context)
2734
{
2835
$template = $this->getTemplate($template_name);
2936
return $template->render($context);

src/Widgets/CheckboxInput.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ class CheckboxInput extends Input
99
const TEMPLATE = 'checkbox.html';
1010
const INPUT_TYPE = 'checkbox';
1111

12-
public function getContext(string $name, $value, array $attrs = null)
12+
public function getContext(string $name, $value, string $label = null, array $attrs = null)
1313
{
1414
if ($value) {
1515
$attrs = is_null($attrs) ? array() : $attrs;
1616
$attrs["checked"] = "checked";
1717
}
1818

19-
return parent::getContext($name, $value, $attrs);
19+
return parent::getContext($name, $value, $label, $attrs);
2020
}
2121

2222
public function valueFromData($data, $files, string $name)

src/Widgets/ChoiceWidget.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ protected function buildName(string $name)
7373
*
7474
* @return array
7575
*/
76-
protected function getContext(string $name, $value, array $attrs = null)
76+
protected function getContext(string $name, $value, string $label = null, array $attrs = null)
7777
{
78-
$context = parent::getContext($name, $value, $attrs);
78+
$context = parent::getContext($name, $value, $label, $attrs);
7979

8080
$context["name"] = $this->buildName($name);
8181
$context["options"] = $this->getOptions($name, $value, $attrs);

src/Widgets/Input.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ abstract class Input extends Widget
1818
*
1919
* @return array
2020
*/
21-
public function getContext(string $name, $value, array $attrs = null)
21+
public function getContext(string $name, $value, string $label = null, array $attrs = null)
2222
{
23-
$context = parent::getContext($name, $value, $attrs);
23+
$context = parent::getContext($name, $value, $label, $attrs);
2424

2525
$context["type"] = static::INPUT_TYPE;
2626

src/Widgets/Select.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ class Select extends ChoiceWidget
1313

1414
protected $option_inherits_attrs = false;
1515

16-
public function getContext(string $name, $value, array $attrs = null)
16+
public function getContext(string $name, $value, string $label = null, array $attrs = null)
1717
{
18-
$context = parent::getContext($name, $value, $attrs);
18+
$context = parent::getContext($name, $value, $label, $attrs);
1919

2020
if ($this->allow_multiple_selected) {
2121
$context["attrs"]["multiple"] = "multiple";

src/Widgets/Widget.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ public function __construct(array $attrs = null)
3535
*
3636
* @return string
3737
*/
38-
public function render(string $name, $value, array $attrs = null)
38+
public function render(string $name, $value, string $label = null, array $attrs = null)
3939
{
4040
$renderer = Config::getInstance()->getRenderer();
4141

42-
$context = $this->getContext($name, $value, $attrs);
42+
$context = $this->getContext($name, $value, $label, $attrs);
4343

4444
return $renderer->render(static::TEMPLATE, $context);
4545
}
@@ -53,7 +53,7 @@ public function render(string $name, $value, array $attrs = null)
5353
*
5454
* @return array
5555
*/
56-
protected function getContext(string $name, $value, array $attrs = null)
56+
protected function getContext(string $name, $value, string $label = null, array $attrs = null)
5757
{
5858
$value = $this->formatValue($value);
5959
$attrs = $this->buildAttrs($attrs);
@@ -63,9 +63,11 @@ protected function getContext(string $name, $value, array $attrs = null)
6363
}
6464

6565
return array(
66+
"for" => $this->buildAutoId($name),
6667
"name" => $name,
6768
"attrs" => $attrs,
6869
"value" => $value,
70+
"label" => $label,
6971
);
7072
}
7173

0 commit comments

Comments
 (0)