PHP code example of masked82 / mustache

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

    

masked82 / mustache example snippets



$mustache = new Mustache_Engine(
    array(
        'helpers' => array(
            'displayName' => function($source, \Mustache_LambdaHelper $lambdaHelper, \Mustache_Context $context) {
                // Access the current context:
                $contextValue = $context->last();
                return isset($contextValue->name) ? $contextValue->name : '';
            },
            'changeName' => function($source, \Mustache_LambdaHelper $lambdaHelper, \Mustache_Context $context) {
                // Remove the current context:
                $contextValue = $context->pop();
                // Change the context value:
                $contextValue->name = 'New Name';
                // Save the context value
                $context->push($contextValue);
                return $lambdaHelper->render($source);
            },
        ),
    )
);


echo $mustache->render(
    'Name:
    {{#person}}
        <p>
            {{#displayName}}{{/displayName}}
        </p>
        <p>
            {{#changeName}}
                The name is changed, but the last name is still {{lastName}}.
            {{/changeName}}
        </p>
        <p>
            {{#displayName}}{{/displayName}}
        </p>
    {{/person}}',
    array(
        'person' => array(
            'name' => 'Some Name',
            'lastName' => 'Last Name'
        )
    )
);