PHP code example of chi-teck / trafaret

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

    

chi-teck / trafaret example snippets


$input = <<< 'HTML'
    <h1>Example</h1> 
    <div>
        <time>12:08</time>
        <span class="total">15</span>
    </div>
    HTML;

$trafaret = new Trafaret(
    <<< 'HTML'
        <h1>Example</h1>
        <div>
            <time>{{ time }}</time>
            <span class="total">{{ total }}</span>
        </div>
        HTML,
    [
        'time' => new Regex('/^\d\d:\d\d$/'),
        'total' => new GreaterThan(10),
    ],
);

$manager = Manager::createDefault();

try {
    $data = $manager->apply($trafaret, $input);
}
catch (ExceptionInterface $exception) {
    \file_put_contents('php://stderr', $exception->getMessage() . "\n");
    exit(1);
}

print_r($data);

final class HomePageTest extends SiteTestCase
{
    use TrafaretTrait;
    
    public function testMarkup(): void
    {
        $trafaret = Trafaret::createFromFile(__DIR__ . '/../fixtures/home-page.html.trf');
        $actual_html = $this->findByXpath('//div[@class = "page"]')->getOuterHtml();
        $this->assertStringByTrafaret($trafaret, $actual_html);
    }
}