Skip to content

Commit 477ee2a

Browse files
committed
Moved templates to templates.php file
1 parent b1367de commit 477ee2a

File tree

9 files changed

+87
-25
lines changed

9 files changed

+87
-25
lines changed

src/Errors/ErrorList.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@
55

66
use Fleshgrinder\Core\Formatter;
77

8+
use PHPForm\PHPFormConfig;
9+
810
class ErrorList extends ArrayObject
911
{
10-
const LIST_TEMPLATE = '<ul class="errorlist">{items}</ul>';
11-
const LIST_ITEM_TEMPLATE = '<li>{content}</li>';
12-
1312
/**
1413
* Returns the error list rendered as HTML.
1514
*
@@ -32,11 +31,14 @@ public function asUL()
3231
}
3332

3433
$items = [];
34+
$list_item_template = PHPFormConfig::getITemplate("ERRORLIST_ITEM");
3535

3636
foreach ($this as $error) {
37-
$items[] = Formatter::format($this::LIST_ITEM_TEMPLATE, array("content" => $error));
37+
$items[] = Formatter::format($list_item_template, array("content" => $error));
3838
}
3939

40-
return Formatter::format($this::LIST_TEMPLATE, array("items" => implode($items)));
40+
$list_template = PHPFormConfig::getITemplate("ERRORLIST");
41+
42+
return Formatter::format($list_template, array("items" => implode($items)));
4143
}
4244
}

src/Widgets/CheckboxSelectMultiple.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,26 @@
44
*/
55
namespace PHPForm\Widgets;
66

7-
use PHPForm\Widgets\Input;
7+
use PHPForm\PHPFormConfig;
88

99
class CheckboxSelectMultiple extends ChoiceWidget
1010
{
11-
const TEMPLATE = '<div>{options}</div>';
12-
const TEMPLATE_CHOICE = '<label for="{for}">' . Input::TEMPLATE . ' {label}</label>';
13-
1411
protected $allow_multiple_selected = true;
1512
protected $option_inherits_attrs = true;
1613
protected $selected_attribute = "checked";
1714
protected $input_type = "checkbox";
15+
16+
/**
17+
* The constructor.
18+
*/
19+
public function __construct(array $choices = array(), array $attrs = null)
20+
{
21+
$this->template = PHPFormConfig::getITemplate("CHECKBOX_SELECT_MULTIPLE");
22+
$this->template_choice = sprintf(
23+
PHPFormConfig::getITemplate("CHECKBOX_SELECT_MULTIPLE_ITEM"),
24+
PHPFormConfig::getITemplate("INPUT")
25+
);
26+
27+
parent::__construct($choices, $attrs);
28+
}
1829
}

src/Widgets/ChoiceWidget.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010

1111
abstract class ChoiceWidget extends Widget
1212
{
13-
const TEMPLATE_CHOICE = "";
14-
13+
protected $template_choice = "";
1514
protected $allow_multiple_selected = false;
1615
protected $input_type = null;
1716
protected $option_inherits_attrs = true;
@@ -70,7 +69,7 @@ public function getSubWidgets(string $name, $value, array $attrs = null)
7069

7170
$context = $this->getSubWidgetContext($name, $choice_value, $choice_label, $selected, $index, $attrs);
7271

73-
$subwidgets[] = Formatter::format(static::TEMPLATE_CHOICE, $context);
72+
$subwidgets[] = Formatter::format($this->template_choice, $context);
7473

7574
$index++;
7675
}

src/Widgets/Input.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,24 @@
44
*/
55
namespace PHPForm\Widgets;
66

7+
use PHPForm\PHPFormConfig;
8+
79
abstract class Input extends Widget
810
{
9-
const TEMPLATE = '<input type="{type}" name="{name}" [{attrs}?] [value="{value}?"]/>';
10-
1111
/**
1212
* @var string The input type to use for the widget.
1313
*/
1414
protected $input_type = null;
1515

16+
/**
17+
* The constructor.
18+
*/
19+
public function __construct(array $attrs = null)
20+
{
21+
$this->template = PHPFormConfig::getITemplate("INPUT");
22+
23+
parent::__construct($attrs);
24+
}
1625

1726
/**
1827
* Prepare context to be used on render method.

src/Widgets/RadioSelect.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,25 @@
44
*/
55
namespace PHPForm\Widgets;
66

7-
use PHPForm\Widgets\Input;
7+
use PHPForm\PHPFormConfig;
88

99
class RadioSelect extends ChoiceWidget
1010
{
11-
const TEMPLATE = '<div>{options}</div>';
12-
const TEMPLATE_CHOICE = '<label for="{for}">' . Input::TEMPLATE . ' {label}</label>';
13-
1411
protected $option_inherits_attrs = true;
1512
protected $selected_attribute = "checked";
1613
protected $input_type = "radio";
14+
15+
/**
16+
* The constructor.
17+
*/
18+
public function __construct(array $choices = array(), array $attrs = null)
19+
{
20+
$this->template = PHPFormConfig::getITemplate("RADIOSELECT");
21+
$this->template_choice = sprintf(
22+
PHPFormConfig::getITemplate("RADIOSELECT_ITEM"),
23+
PHPFormConfig::getITemplate("INPUT")
24+
);
25+
26+
parent::__construct($choices, $attrs);
27+
}
1728
}

src/Widgets/Select.php

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

7+
use PHPForm\PHPFormConfig;
8+
79
class Select extends ChoiceWidget
810
{
9-
const TEMPLATE = '<select name="{name}"[ {attrs}?]>{options}</select>';
10-
const TEMPLATE_CHOICE = '<option value="{value}"[ {attrs}?]>{label}</option>';
11-
1211
protected $option_inherits_attrs = false;
1312

13+
/**
14+
* The constructor.
15+
*/
16+
public function __construct(array $choices = array(), array $attrs = null)
17+
{
18+
$this->template = PHPFormConfig::getITemplate("SELECT");
19+
$this->template_choice = PHPFormConfig::getITemplate("SELECT_ITEM");
20+
21+
parent::__construct($choices, $attrs);
22+
}
23+
1424
public function getContext(string $name, $value, array $attrs = null)
1525
{
1626
if (is_null($attrs)) {

src/Widgets/Textarea.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
*/
55
namespace PHPForm\Widgets;
66

7+
use PHPForm\PHPFormConfig;
8+
79
class Textarea extends Widget
810
{
9-
const TEMPLATE = '<textarea name="{name}" [{attrs}?]>[{value}?]</textarea>';
10-
1111
/**
1212
* The constructor.
1313
*/
@@ -19,6 +19,8 @@ public function __construct(array $attrs = null)
1919
$extra_attrs = array_merge($extra_attrs, $attrs);
2020
}
2121

22+
$this->template = PHPFormConfig::getITemplate("TEXTAREA");
23+
2224
parent::__construct($extra_attrs);
2325
}
2426
}

src/Widgets/Widget.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
abstract class Widget
1212
{
1313
const AUTO_ID_TEMPLATE = "id_{name}[_{index}?]";
14-
const TEMPLATE = '';
14+
15+
protected $template = "";
1516

1617
/**
1718
* @var array Attributes to be added to the widget.
@@ -42,7 +43,7 @@ public function render(string $name, $value, array $attrs = null)
4243
{
4344
$context = $this->getContext($name, $value, $attrs);
4445

45-
return Formatter::format(static::TEMPLATE, $context);
46+
return Formatter::format($this->template, $context);
4647
}
4748

4849
/**

src/templates.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,21 @@
33
return array(
44
"LABEL" => '<label for="{for}"[ {attrs}?]>{contents}[ {required}?]</label>',
55
"LABEL_REQUIRED" => '<span class="required">*</span>',
6+
7+
// ErrorList
8+
"ERRORLIST" => '<ul class="errorlist">{items}</ul>',
9+
"ERRORLIST_ITEM" => '<li>{content}</li>',
10+
11+
// Widgets
12+
"TEXTAREA" => '<textarea name="{name}" [{attrs}?]>[{value}?]</textarea>',
13+
"INPUT" => '<input type="{type}" name="{name}" [{attrs}?] [value="{value}?"]/>',
14+
15+
"SELECT" => '<select name="{name}"[ {attrs}?]>{options}</select>',
16+
"SELECT_ITEM" => '<option value="{value}"[ {attrs}?]>{label}</option>',
17+
18+
"CHECKBOX_SELECT_MULTIPLE" => '<div>{options}</div>',
19+
"CHECKBOX_SELECT_MULTIPLE_ITEM" => '<label for="{for}">%s {label}</label>',
20+
21+
"RADIOSELECT" => '<div>{options}</div>',
22+
"RADIOSELECT_ITEM" => '<label for="{for}">%s {label}</label>',
623
);

0 commit comments

Comments
 (0)