PHP code example of cleaniquecoders / placeholdify

1. Go to this page and download the library: Download cleaniquecoders/placeholdify 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/ */

    

cleaniquecoders / placeholdify example snippets


use CleaniqueCoders\Placeholdify\PlaceholderHandler;

$template = "Hello {name}, your order #{orderNo} totaling {amount} has been confirmed.";

$content = PlaceholderHandler::process($template, [
    'name' => 'John Doe',
    'orderNo' => '12345',
    'amount' => '$99.99'
]);

// Output: "Hello John Doe, your order #12345 totaling $99.99 has been confirmed."

$handler = new PlaceholderHandler();

$content = $handler
    ->add('name', $user->name)
    ->addDate('today', now(), 'F j, Y')
    ->addFormatted('total', 1234.56, 'currency', 'MYR')
    ->replace($template);

// Register context once
$handler->registerContextMapping('user', [
    'name' => 'name',
    'email' => 'email',
    'role' => fn($user) => $user->roles->pluck('name')->join(', '),
]);

// Use anywhere
$content = $handler
    ->useContext('user', $user, 'customer')
    ->replace('Hello {customer.name}, your role is {customer.role}');

$template = "Student: {name|upper}, Amount: {fee|currency:MYR}, Date: {created_at|date:d/m/Y}";

$content = $handler
    ->add('name', 'john doe')
    ->add('fee', 150.50)
    ->add('created_at', now())
    ->replaceWithModifiers($template);

// Output: "Student: JOHN DOE, Amount: MYR 150.50, Date: 16/10/2025"

use CleaniqueCoders\Placeholdify\PlaceholdifyBase;

class InvoiceTemplate extends PlaceholdifyBase
{
    protected function configure(): void
    {
        $this->handler->setFallback('N/A');
    }

    public function build($invoice): PlaceholderHandler
    {
        return $this->handler
            ->add('invoice_no', $invoice->invoice_number)
            ->addFormatted('total', $invoice->total, 'currency', 'MYR')
            ->addDate('invoice_date', $invoice->created_at, 'd/m/Y')
            ->useContext('customer', $invoice->customer, 'customer');
    }
}

// Usage
$template = new InvoiceTemplate();
$content = $template->generate($invoice, $templateContent);
bash
php artisan vendor:publish --tag=placeholdify-config