Skip to content

Commit

Permalink
multitemplate
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimonka2 committed Jan 15, 2020
1 parent 91c292f commit 09e94a2
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 34 deletions.
1 change: 1 addition & 0 deletions src/Form/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public function renderElement(IElement $element, $aroundHTML = null)

public function getTemplate($tag)
{
// logger('getTemplate', [$tag]);
foreach (explode(',', $this->cofig_template_path) as $path) {
$template = config('flatform.' . $path . '.' . $tag, null);
if(!is_null($template)) return $template;
Expand Down
15 changes: 10 additions & 5 deletions src/Form/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,16 @@ public function renderElement()
if(!$this->hidden) {
$html = $this->render();
$template = $this->template;
if(!is_null($template) && $template != false) return $this->context->renderView(
view($template)
->with('element', $this)
->with('html', $html)
);
if(!is_null($template) && $template != false) {
foreach(explode(';', $template) as $template) {
$html = $this->context->renderView(
view($template)
->with('element', $this)
->with('html', $html)
);
}

}
return $html;
}
}
Expand Down
48 changes: 19 additions & 29 deletions src/Form/ElementFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

class ElementFactory
{
private const tag_template = '/[_-a-zA-Z0-9]+/';
private $context;
private $binds = [];

Expand All @@ -32,55 +33,44 @@ protected static function transferIndexedElement(&$element1, &$element2, $index,
}
}

protected static function smartMerge($element1, $element2)
protected static function mergeTemplate($element, $template)
{
self::transferIndexedElement($element1, $element2, '+style', ';');
self::transferIndexedElement($element1, $element2, '+class', ' ');
return array_merge($element1, $element2);
if(is_null($template)) return $element;
self::transferIndexedElement($element, $template, '+style', ';');
self::transferIndexedElement($element, $template, '+class', ' ');
self::transferIndexedElement($element, $template, 'template', ';');
return array_merge($element, $template);
}



public function createElement(array $element): IElement
{
$def_type = config('flatform.form.default-type', 'div');

$type = strtolower($element['type'] ?? '');
$template = null;
if (isset($element['template'])) {
// template is already given
$template = $element['template'];
if ($template != false) {
$template = $this->context->getTemplate($template);
}
} else {
if(in_array($type, config('flatform.form.inputs', [])) ){
// apply input template
$template = $this->context->getTemplate('input');

if ($template != false && preg_match(self::tag_template, $template)) {
$element = self::mergeTemplate($element, $this->context->getTemplate($template));
}
}
if(is_array($template)) {
$element = array_merge($template, $element);
if(isset($template['type'])) $element['type'] = $template['type'];
}

$template = null;
$type = $element['type'] ?? $def_type;
// use type as a template
if (!isset($element['template']) || $element['template'] != false) {
$template = $this->context->getTemplate($type);
if(is_array($template)) $element = self::smartMerge($template, $element);
$type = strtolower($element['type'] ?? $def_type);

// use type as a template
if ($element['template'] ?? true != false) {
$element = self::mergeTemplate($element, $this->context->getTemplate($type));
}
if(is_array($template)) {
$element = array_merge($template, $element);
if(isset($template['type'])) $element['type'] = $template['type'];

if( ($element['template'] ?? true != false) && in_array($type, config('flatform.form.inputs', [])) ){
// apply input template
$element = self::mergeTemplate($element, $this->context->getTemplate('input') );
}

if (isset($this->binds[$type])) {
return self::_createElement($this->binds[$type], $element, $this->context);
}


$class = $this->binds[$def_type];
if (empty($element['type']) ) $element['type'] = $def_type;
return self::_createElement($class, $element, $this->context);
Expand Down

0 comments on commit 09e94a2

Please sign in to comment.