setup twig
continuous-integration/drone/push Build is passing Details

pull/7/head
maxime.batista 1 year ago
parent 7683dd2129
commit b8edb27f1c

@ -8,6 +8,7 @@
"altorouter/altorouter": "1.2.0",
"ext-json": "*",
"ext-pdo": "*",
"ext-pdo_sqlite": "*"
"ext-pdo_sqlite": "*",
"twig/twig":"^2.0"
}
}

@ -4,6 +4,7 @@ require "../vendor/autoload.php";
require "../config.php";
require "../sql/database.php";
use \Twig\Loader\FilesystemLoader;
use App\Connexion;
use App\Controller\SampleFormController;
use App\Gateway\FormResultGateway;
@ -23,6 +24,9 @@ function get_base_path() {
return $basePath;
}
$loader = new FilesystemLoader('../src/Views/');
$twig = new \Twig\Environment($loader);
$basePath = get_base_path();
$con = new Connexion(get_database());
@ -30,9 +34,11 @@ $con = new Connexion(get_database());
$router = new AltoRouter();
$router->setBasePath($basePath);
$sampleFormController = new SampleFormController(new FormResultGateway($con));
$sampleFormController = new SampleFormController(new FormResultGateway($con), $twig);
$router->map("GET", "/", fn() => $sampleFormController->displayForm());
$router->map("POST", "/submit", fn() => $sampleFormController->submitForm($_POST));
$router->map("GET", "/twig", fn() => $sampleFormController->displayFormTwig());
$router->map("POST", "/submit-twig", fn() => $sampleFormController->submitFormTwig($_POST));
$match = $router->match();

@ -41,7 +41,7 @@ class Connexion {
$stmnt->bindValue($name, $value[0], $value[1]);
}
$stmnt->execute();
return $stmnt->fetchAll();
return $stmnt->fetchAll(PDO::FETCH_ASSOC);
}
}

@ -4,17 +4,23 @@ namespace App\Controller;
require_once __DIR__ . "/../react-display.php";
use App\Gateway\FormResultGateway;
use \Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Error\RuntimeError;
use Twig\Error\SyntaxError;
class SampleFormController {
private FormResultGateway $gateway;
private Environment $twig;
/**
* @param FormResultGateway $gateway
*/
public function __construct(FormResultGateway $gateway)
public function __construct(FormResultGateway $gateway, Environment $twig)
{
$this->gateway = $gateway;
$this->twig = $twig;
}
@ -27,4 +33,22 @@ class SampleFormController {
$results = ["results" => $this->gateway->listResults()];
send_react_front("views/DisplayResults.tsx", $results);
}
public function displayFormTwig() {
try {
echo $this->twig->render('sample_form.html.twig', []);
} catch (LoaderError | RuntimeError | SyntaxError $e) {
echo "Twig error: $e";
}
}
public function submitFormTwig(array $request) {
$this->gateway->insert($request["name"], $request["description"]);
try {
$results = $this->gateway->listResults();
echo $this->twig->render('display_results.html.twig', ['results' => $results]);
} catch (LoaderError | RuntimeError | SyntaxError $e) {
echo "Twig error: $e";
}
}
}

@ -0,0 +1,18 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Twig view</title>
</head>
<body>
<h1>Hello world</h1>
{% for v in results %}
<p>username: {{ v.name }}</p>
<p>description: {{ v.description }}</p>
{% endfor %}
</body>
</html>

@ -0,0 +1,20 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Twig view</title>
</head>
<body>
<h1>Hello, this is a sample form made in Twig !</h1>
<form action="/submit-twig" method="POST">
<label for="name">your name: </label>
<input type="text" id="name" name="name"/>
<label for="password">a little description about yourself: </label>
<input type="text" id="password" name="description"/>
<input type="submit" value="click me to submit!"/>
</form>
</body>
</html>
Loading…
Cancel
Save