[ticket/15289] Use macros to generate form

PHPBB3-15289
This commit is contained in:
Rubén Calvo 2017-08-03 16:36:21 +02:00
parent 7e0845f930
commit 04a34d9f4f
4 changed files with 36 additions and 81 deletions

View file

@ -1,3 +1,4 @@
{% import 'forms.html' as forms %}
<!-- INCLUDE overall_header.html -->
<a id="maincontent"></a>
@ -29,13 +30,16 @@
<fieldset id="{{ storage.get_name }}_{{ provider.get_name }}_settings">
<legend>{{ lang('STORAGE_' ~ storage.get_name | upper ~ '_TITLE') }} - {{ lang('STORAGE_ADAPTER_' ~ provider.get_name | upper ~ '_NAME') }}</legend>
{% for name, options in provider.get_options %}
{% set lang_name = 'STORAGE_ADAPTER_' ~ provider.get_name | upper ~ '_OPTION_' ~ name | upper %}
{% set options = options|merge({'name': storage.get_name ~ '[' ~ name ~ ']'}) %}
{{ adm_block(lang(lang_name), '', input(options, attribute(CONFIG, 'storage\\' ~ storage.get_name ~ '\\config\\' ~ name))) }}
{% set l_name = 'STORAGE_ADAPTER_' ~ provider.get_name | upper ~ '_OPTION_' ~ name | upper %}
{% set options = options|merge({'name': storage.get_name ~ '[' ~ name ~ ']'}) %}
{% set options = options|merge({'value': attribute(CONFIG, 'storage\\' ~ storage.get_name ~ '\\config\\' ~ name)}) %}
{{ forms.form_row(
forms.form_label(l_name),
forms.form_control(options)
) }}
{% endfor %}
</fieldset>
{% endfor %}
{% endfor %}
<fieldset class="submit-buttons">

View file

@ -0,0 +1,28 @@
{% macro input(name, value, type, size) %}
<input type="{{ type|default('text') }}" name="{{ name }}" value="{{ value|e }}" size="{{ size|default(20) }}" />
{% endmacro %}
{% macro textarea(name, value, rows, cols) %}
<textarea name="{{ name }}" rows="{{ rows|default(10) }}" cols="{{ cols|default(40) }}">{{ value|e }}</textarea>
{% endmacro %}
{% macro form_control(params) %}
{% if params['type'] == 'text' %}
{{ _self.input(params['name'], params['value'], params['type']) }}
{% endif %}
{% endmacro %}
{% macro form_label(name, description = '') %}
<label for="">{{ lang(name) }}</label>{% if description is not empty %}<br /><span>{{ lang(description) }}</span>{% endif %}
{% endmacro %}
{% macro form_row(left_content, right_content) %}
<dl>
<dt>
{{ left_content }}
</dt>
<dd>
{{ right_content}}
</dd>
</dl>
{% endmacro %}

View file

@ -48,12 +48,6 @@ services:
tags:
- { name: twig.extension }
template.twig.extensions.form:
class: phpbb\template\twig\extension\form
arguments:
tags:
- { name: twig.extension }
template.twig.extensions.debug:
class: Twig_Extension_Debug

View file

@ -1,71 +0,0 @@
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\template\twig\extension;
// I will use this to generate forms in twig until there is something better
class form extends \Twig_Extension
{
/**
* Constructor
*/
public function __construct()
{
}
public function getFunctions()
{
return [
new \Twig_SimpleFunction('input', '\\phpbb\\template\\twig\\extension\\form::generate_input'),
new \Twig_SimpleFunction('adm_block', '\\phpbb\\template\\twig\\extension\\form::generate_block'),
];
}
public static function generate_input($options, $value = '')
{
$input = '<input value="' . $value . '"';
switch ($options['type'])
{
case 'radio':
break;
default:
foreach ($options as $key => $value)
{
if (in_array($key, ['lang']))
continue;
$input .= "$key=\"$value\" ";
}
break;
}
$input .= '>';
return $input;
}
public static function generate_block($name, $description = '', $content)
{
return <<<EOF
<dl>
<dt><label for="">$name</label><br /><span>$description</span></dt>
<dd>
$content
</dd>
</dl>
EOF;
}
}