PHP code example of sfw2 / render

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

    

sfw2 / render example snippets



use Handlebars\Handlebars;
use SFW2\Render\Conditions\IsAjaxRequest;
use SFW2\Render\Conditions\MatchesPath;
use SFW2\Render\RenderJson;
use SFW2\Render\RenderOnConditions;
   
// Have a look at salesforce/handlebars-php for the proper use   
$handlebars = new Handlebars([
    "loader" => StringLoader(),
]);
                
$render = new RenderOnConditions();

// Checks, if "X-Requested-With" header line is set and, if so, render data in json-format
$render->addEngine(new IsAjaxRequest(), new RenderJson());

// Checks, if certain request-path is set and, if so, render data in xml-format.
$render->addEngine(new MatchesPath('/'), new RenderJson($handlebars));

// Render other stuff in html-format
$render->addEngine(new AlwaysTrue(),  new RenderHtml($handlebars, '<html>{{content}}</html>'));


use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use SFW2\Render\RenderInterface;

class MyController{
  
    public function __construct(
        private RenderInterface $render
    ) {
    }
    
    public function myAction(RequestInterface $request, ResponseInterface $response, array $data): ResponseInterface
    {
        return $this->render->render($request, $response, [], 'mytemplate.handlebars');
    }
}