PHP code example of totalcrm / docx-templator

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

    

totalcrm / docx-templator example snippets



    use TotalCRM\DocxTemplator\Templator;
    use TotalCRM\DocxTemplator\Document\WordDocument;
    
    $cachePath = 'path/to/writable/directory/';
    $templator = new Templator($cachePath);
    
    // Enable debug mode to generate template with every render call.
    // $templator->debug = true;
    
    // Enable track mode to generate template with every original document change.
    // $templator->trackDocument = true;
    
    $documentPath = 'path/to/document.docx';
    $document = new WordDocument($documentPath);
    
    $values = array(
        'library' => 'Templator 0.1',
        'simpleValue' => 'I am simple value',
        'nested' => array(
            'firstValue' => 'First child value',
            'secondValue' => 'Second child value'
        ),
        'header' => 'test of a table row',
        'students' => array(
            array('id' => 1, 'name' => 'Student 1', 'mark' => '10'),
            array('id' => 2, 'name' => 'Student 2', 'mark' => '4'),
            array('id' => 3, 'name' => 'Student 3', 'mark' => '7')
        ),
        'maxMark' => 10,
        'todo' => array(
            'TODO 1',
            'TODO 2',
            'TODO 3'
        )
    );
    $result = $templator->render($document, $values);
    
    // Now you can get template result.
    // 1. HTTP Download
    $result->download();
    
    // Or
    // 2. Save to file
    $saved = $result->save(__DIR__ . '/static', 'result.docx');
    if ($saved === true) {
         echo 'Saved!';
    }
    
    // Or
    // 3. Buffer output
    echo $result->output();