PHP code example of aesonus / message-templates

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

    

aesonus / message-templates example snippets


const TEMPLATES = [
   'key1' => 'template string, %s',
   'key2' => 'template string two, %s',
];

abstract class ConstTemplateSource implements TemplateSourceInterface
{
    public function getTemplate(): string
    {
        return TEMPLATES[$this->key];
    }
}

class Key1 extends ConstTemplateSource {
    protected $key = 'key1';
}

class Key2 extends ConstTemplateSource {
    protected $key = 'key2';
}

// Create the render object
$template = new RenderVsprintfTemplate();

// Set the template source for the render object
$template->setSource(new Key1);

//Output the template with data
echo $template->render(['purple']);

//Outputs: template string, purple

// Set another template source for the render object
$template->setSource(new Key2);

//Output the template with data
echo $template->render(['red']);

//Outputs: template string two, red