Firstly the code:
Formbuilder.php
class FormBuilder
{
protected $input;
private $method;
private $action;
public function __construct($method, $action = NULL)
{
$this->method = $method;
$this->action = $action;
}
public function newInput($type, $name, $value, $placeholder = NULL, $class = NULL, $id = NULL)
{
$this->input[] = array (
'type' => $type,
'name' => $name,
'value' => $value,
'placeholder' => $placeholder,
'class' => $class,
'id' => $id
);
}
public function newLabel($name, $title = NULL)
{
$this->input[] = array (
'type' => 'label',
'name' => $name,
'title' => $title
);
}
public function newTextarea($name, $value = NULL, $placeholder = NULL)
{
$this->input[] = array (
'type' => 'textarea',
'name' => $name,
'value' => $value,
'placeholder' => $placeholder
);
}
public function renderForm()
{
$html = '<form action="' . $this->action . '" method="' . $this->method . '">';
for ($i = 0; $i < count($this->input); $i++) {
switch($this->input[$i]['type']) {
case "label":
$html .= '<div><p><label for="' . $this->input[$i]['name'] . '">' . $this->input[$i]['title'] . '</label><br />';
break;
case "text":
case "password":
case "hidden":
case "submit":
case "email":
$html .= '<input type="' . $this->input[$i]['type'] . '" ';
$html .= ' name="' . $this->input[$i]['name'] . '" ';
$html .= ' value="' . $this->input[$i]['value'] . '" ';
$html .= ' placeholder="' . $this->input[$i]['placeholder'] . '" ';
$html .= ' class="' . $this->input[$i]['class'] . '" ';
$html .= ' id="' . $this->input[$i]['id'] . '"></p></div> ';
break;
case "textarea":
$html .= '<textarea name="' . $this->input[$i]['name'] . ' value="' . $this->input[$i]['value'] . '" placeholder="' . $this->input[$i]['placeholder'] . '"></textarea>';
break;
}
}
$html .= '</form>';
$form = sprintf($html);
// print $form;
Registration.php
class Registration extends \Core\CoreController
{
public function buildForm()
{
$regform = new FormBuilder('post');
$regform->newLabel('username', 'Enter a Username:');
$regform->newInput('text','username','','Enter Username', 'form-control', 'username');
$regform->newLabel('password', 'Enter password:');
$regform->newInput('password', 'password', '', 'Enter Password', 'form-control', 'password');
$regform->newLabel('password', 'Re-enter password:');
$regform->newInput('password', 'password_chk', '', 'Password Again', 'form-control', 'password_chk');
$regform->newLabel('useremail', 'Enter email address:');
$regform->newInput('email','useremail','','Email Address', 'form-control', 'useremail');
$regform->newInput('submit','submit','Register');
$regform->renderForm();
}
public function registerAction()
{
$newform = $this->buildForm();
View::renderTwigTemplate('Frontend/Tests/tests.html.twig',[
'newform' => $newform
]);
}
}
}
}
Twig Template
%
extends "base.html.twig" %}
{% block body %}
{{ newform }}
{% endblock %
The problem:
As you can see in Formbuilder.php i assign a variable ($form) to a sprintf function, i have to use sprintf because if i use print it overrides the twig template and just prints the form even without being called by Twig at least with sprintf i can assign it to a variable.
What i am trying to do for hours now is get my $form variable inside the buildForm method in Registration.php where i can then assign it to a Twig variable and render it where i want it inside the Twig template, as i said using print just does what its supposed to do, print the form above my call to the extends "Base.html.twig" which is obviously not what i want.
I could possibly get away without having to assign a variable to sprintf function as this line of code ($regform->renderForm) in Registration.php buildForm method is what renders the print so im guessing it also holds the sprintf, i just need to find a way to assign it to the $newform variable for Twig.
Thanks