PHP code example of salesforce / handlebars-php

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

    

salesforce / handlebars-php example snippets




# With composer we can autoload the Handlebars package
equire 'src/Handlebars/Autoloader.php';
# Handlebars\Autoloader::register();

use Handlebars\Handlebars;
use Handlebars\Loader\FilesystemLoader;

# Set the partials files
$partialsDir = __DIR__."/templates";
$partialsLoader = new FilesystemLoader($partialsDir,
    [
        "extension" => "html"
    ]
);

# We'll use $handlebars throughout this the examples, assuming the will be all set this way
$handlebars = new Handlebars([
    "loader" => $partialsLoader,
    "partials_loader" => $partialsLoader
]);

# Will render the model to the templates/main.tpl template
$model = [...];
echo $handlebars->render("main", $model);



$model = [
    "name" => "Yolo Baggins",
    "title" => "I'm Title",
    "permalink" => "blog/",
    "foo" => "bar",
    "article" => [
        "title" => "My Article Title"
    ],
    "posts" => [
        [
            "title" => "Post #1",
            "id" => 1,
            "content" => "Content"
        ],
        [
            "title" => "Post 2",
            "id" => 2,
            "content" => "Content"
        ]
    ]
];

echo $handlebars->render("main", $model);

echo $handlebars("main", $model);



$model = [
    "title" => "I'm Title",
    "permalink" => "/blog/",
    "foo" => "bar",
    "article" => [
        "title" => "My Article Title"
    ],
    "posts" => [
        [
            "title" => "Post #1",
            "id" => 1,
            "content" => "Content"
        ],
        [
            "title" => "Post 2",
            "id" => 2,
            "content" => "Content"
        ]
    ]
];



$model = [
    "isActive" => true,
    "isValid" => false
];

echo $handlebars->render($template, $model);



$model = [
    "genres" => [
        "Hip-Hop",
        "Rap",
        "Techno",
        "Country"
    ],
    "cars" => [
        "category" => "Foreign",
        "count" => 4,
        "list" => [
            "Toyota",
            "Kia",
            "Honda",
            "Mazda"
        ],
        "category" => "WTF",
        "count" => 1,
        "list" => [
            "Fiat"
        ],
        "category" => "Luxury",
        "count" => 2,
        "list" => [
            "Mercedes Benz",
            "BMW"
        ]
    ],
];

    echo $engine->render($template, $model);    



$model = [
    "genres" => [
        "Hip-Hop",
        "Rap",
        "Techno",
        "Country"
    ],
    "other_genres" => [
        "genres" => [
        "Hip-Hop",
        "Rap",
        "Techno",
        "Country"
        ]
]
];

$handlebars->addHelper("upper",
    function($template, $context, $args, $source){
        return strtoupper($context->get($args));
    }
);

$handlebars = new Handlebars([
    "loader" => $partialsLoader,
    "partials_loader" => $partialsLoader,
    "enableDataVariables" => true
]);

'data' => ['apple', 'banana', 'carrot', 'zucchini']

'data' => [['apple', 'banana'], ['carrot', 'zucchini']]

$object = new stdClass;
$object->{'@first'} = 'apple';
$object->{'@last'} = 'banana';
$object->{'@index'} = 'carrot';
$object->{'@unknown'} = 'zucchini';
$data = ['objects' => [$object]];

$engine = new \Handlebars\Handlebars(array(
    'loader' => new \Handlebars\Loader\StringLoader(),
    'helpers' => new \Handlebars\Helpers(),
    'enableDataVariables'=> $enabled,
));
$engine->render($template, $data)