PHP code example of webignition / stubble

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

    

webignition / stubble example snippets


use webignition\Stubble\VariableResolver;
use webignition\StubbleResolvable\Resolvable;

// Using a VariableResolver instance
$resolver = new VariableResolver();

$resolvable = new Resolvable(
    'Hello {{ name }}!',
    [
        'name' => 'World',
    ]
);

$resolvedTemplate = $resolver->resolve($resolvable);
echo $resolvedTemplate; // Hello World!

use webignition\Stubble\UnresolvedVariableException;
use webignition\Stubble\VariableResolver;
use webignition\StubbleResolvable\Resolvable;

$resolver = new VariableResolver();

$resolvable = new Resolvable(
    'Hello {{ name }} and welcome to {{ location }}.',
    [
        'name' => 'World',
    ]
);

try {
    $resolvedTemplate = $resolver->resolve($resolvable);
} catch (UnresolvedVariableException $exception) {
    // Do something useful ... logging?
    $exception->getVariable(); // 'location'
    $exception->getTemplate(); // 'Hello {{ name }} and welcome to {{ location }}.'
}

use webignition\Stubble\UnresolvedVariableFinder;
use webignition\Stubble\VariableResolver;
use webignition\StubbleResolvable\Resolvable;

$resolver = new VariableResolver(
    new UnresolvedVariableFinder([
        function (string $variable) {
            return 'location' === $variable;
        },        
    ])
);

$resolvable = new Resolvable(
    'Hello {{ name }} and welcome to {{ location }}.',
    [
        'name' => 'World',
    ]
);

$resolvedTemplate = $resolver->resolve($resolvable);
echo $resolvedTemplate; // Hello Jon and welcome to {{ location }}.

use webignition\Stubble\DeciderFactory;
use webignition\Stubble\VariableResolver;
use webignition\Stubble\UnresolvedVariableFinder;

$resolver = new VariableResolver(
    new UnresolvedVariableFinder([
        // Allow all unresolved variables
        DeciderFactory::createAllowAllDecider(),
        // Disallow all unresolved variables
        DeciderFactory::createDisallowAllDecider(),
        // Allow unresolved variables by pattern (regular expression),
        DeciderFactory::createAllowByPatternDecider('/^variable[0-9]$/')
    ])
);