PHP Template Engines
In this page:
$template = "Hello, {{ name }}!";
$output = str_replace("{{ name }}", htmlspecialchars($value), $template); // escape output
echo $output;
एक Template Engine क्या है?
एक template engine presentation markup को application logic से अलग रखता है, designers को उन्हें भरने वाला PHP समझे बिना simple placeholders इस्तेमाल करके templates edit करने देते हुए।
उदाहरण: What is a Template Engine?
<?php
// Declare `$template`, set to "Hello, {{ name }}!"
$template = "Hello, {{ name }}!";
// Declare `$data` as an array: `["name" => "Alice"]`
$data = ["name" => "Alice"];
// Print `str_replace("{{ name }}", $data["name"], $template)` to the output
echo str_replace("{{ name }}", $data["name"], $template);
?>
Login to try C/C++/Java/PHP code in the editor
Basic Variable Parsing
इसके core में, एक template engine placeholders (जैसे {{ name }}) के लिए एक template string scan करता है और corresponding variable की value substitute करता है, जो view files को dense PHP control structures से मुक्त रखता है।
उदाहरण: Basic Variable Parsing
<?php
// Define the function `render` taking `$template`, `$data`
function render($template, $data) {
// Loop over `$data`, binding each item to `$key => $value`
foreach ($data as $key => $value) {
// Declare `$template`, set to `str_replace("{{ $key }}", $value, $template)`
$template = str_replace("{{ $key }}", $value, $template);
}
// Return `$template` from this function
return $template;
}
// Print `render("Hi {{ name }}, you are {{ age }}", ["name" => "Alice", "age" => 30])` to the output
echo render("Hi {{ name }}, you are {{ age }}", ["name" => "Alice", "age" => 30]);
?>
Login to try C/C++/Java/PHP code in the editor
Layout Inheritance को Simulate करना
Layout inheritance एक child template को यह declare करने देता है कि यह एक shared parent layout को extend करता है और सिर्फ specific named blocks (जैसे एक page title या main content area) को override करता है, हर view में repeated header/footer markup से बचते हुए।
उदाहरण: Simulating Layout Inheritance
<?php
// Define the function `renderLayout` taking `$content`
function renderLayout($content) {
// Return `"<html><body>$content</body></html>"` from this function
return "<html><body>$content</body></html>";
}
// Declare `$pageContent`, set to "<h1>Home Page</h1>"
$pageContent = "<h1>Home Page</h1>";
// Print `renderLayout($pageContent)` to the output
echo renderLayout($pageContent);
?>
Login to try C/C++/Java/PHP code in the editor
Safe Output Escaping
क्योंकि raw user data में malicious HTML या scripts हो सकते हैं, template engines इसे print करने से पहले default रूप से variable output escape करते हैं, एक major XSS attack surface को automatically बंद करते हुए।
उदाहरण: Safe Output Escaping
<?php
// Define the function `render` taking `$template`, `$data`
function render($template, $data) {
// Loop over `$data`, binding each item to `$key => $value`
foreach ($data as $key => $value) {
// Declare `$template`, set to `str_replace("{{ $key }}", htmlspecialchars($value), $template)`
$template = str_replace("{{ $key }}", htmlspecialchars($value), $template);
}
// Return `$template` from this function
return $template;
}
// Print `render("Comment: {{ text }}", ["text" => "<script>alert(1)</script>"])` to the output
echo render("Comment: {{ text }}", ["text" => "<script>alert(1)</script>"]);
?>
Login to try C/C++/Java/PHP code in the editor
Modular Views
एक layout को छोटी included files में split करना -- एक header, एक footer, एक nav bar -- हर piece को focused और कई pages में reusable रखता है, यहाँ तक कि किसी पूरी templating library के बिना भी।
उदाहरण: Modular Views
<?php
function header() { return "<header>Site Header</header>"; }
function footer() { return "<footer>Site Footer</footer>"; }
echo header() . "<main>Page content</main>" . footer();
?>
Login to try C/C++/Java/PHP code in the editor
- एक template में user data बिना escape किए print करना।
- heavy logic को controller के बजाय template में डालना।
- जब input इसे तोड़ सके तब एक template engine नकली बनाने के लिए एक regular expression इस्तेमाल करना।
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first: