PHP code example of mintyphp / template

1. Go to this page and download the library: Download mintyphp/template library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

mintyphp / template example snippets


use MintyPHP\Template\Template;

$template = new Template();
echo $template->render('<p>{{hi}}</p>',['hi' => 'Hello world']);
// Outputs: <p>Hello world</p>

$data = ['name' => 'john doe', 'date' => 'May 13, 1980'];

$filters = [
    'upper' => 'strtoupper',
    'capitalize' => 'ucfirst',
    'dateFormat' => fn($date, $format) => date($format, strtotime($date))
];

$template = new Template(null, $filters);
$html = $template->render(
    'Hello {{ name|upper }}, date: {{ date|dateFormat("Y-m-d") }}',
    $data
);
// Output: Hello JOHN DOE, date: 1980-05-13

$data = ['age' => 21];

$tests = [
    'adult' => fn($age) => is_numeric($age) && $age >= 18
];

$template = new Template(null, null, $tests);
$html = $template->render(
    '{% if age is adult %}You are an adult{% else %}You are a minor{% endif %}',
    $data
);
// Output: Adult

// Configure template loader for extends/: ?string {
    $path = __DIR__ . '/templates/' . $name;
    return file_exists($path) ? file_get_contents($path) : null;
};

$template = new Template($templateLoader);
$result = $template->render($templateContent, $data);
json
{
    "user_input": "<script>alert('XSS')</script>",
    "safe_html": "<strong>Bold Text</strong>"
}
json
{
    "blog": {
        "title": "My Tech Blog",
        "posts": [
            {
                "id": 1,
                "title": "Getting Started with PHP",
                "author": "Alice",
                "date": "2024-01-15",
                "excerpt": "Learn the basics of PHP programming...",
                "published": true,
                "views": 1234
            },
            {
                "id": 2,
                "title": "Advanced Template Engines",
                "author": "Bob",
                "date": "2024-01-20",
                "excerpt": "Deep dive into template engine design...",
                "published": true,
                "views": 856
            },
            {
                "id": 3,
                "title": "Upcoming Features",
                "author": "Alice",
                "date": "2024-02-01",
                "excerpt": "What's coming next...",
                "published": false,
                "views": 0
            }
        ]
    }
}
json
{
    "dashboard": {
        "user": "Admin",
        "stats": {
            "total_users": 1523,
            "active_users": 892,
            "total_revenue": 45678.90,
            "pending_orders": 23
        },
        "recent_activities": [
            {
                "user": "Alice",
                "action": "registered",
                "time": "2 minutes ago"
            },
            {
                "user": "Bob",
                "action": "made a purchase",
                "time": "5 minutes ago"
            },
            {
                "user": "Charlie",
                "action": "updated profile",
                "time": "10 minutes ago"
            }
        ]
    }
}