← Back to PHP Course | Chapter 15: Web Development | Lesson 2 of 12

PHP Template Engines

एक template engine code से अलग रखी एक fill-in-the-blanks page design जैसा है। Designers page कैसा दिखता है यह बदलते हैं जबकि programmers information supply करते हैं।
Syntax
php
$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
<?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);
?>

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
<?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]);
?>

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
<?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);
?>

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
<?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>"]);
?>

Modular Views

एक layout को छोटी included files में split करना -- एक header, एक footer, एक nav bar -- हर piece को focused और कई pages में reusable रखता है, यहाँ तक कि किसी पूरी templating library के बिना भी।

उदाहरण: Modular Views

php
<?php
function header() { return "<header>Site Header</header>"; }
function footer() { return "<footer>Site Footer</footer>"; }
echo header() . "<main>Page content</main>" . footer();
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. एक template में user data बिना escape किए print करना।
  2. heavy logic को controller के बजाय template में डालना।
  3. जब input इसे तोड़ सके तब एक template engine नकली बनाने के लिए एक regular expression इस्तेमाल करना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.