PHP code example of kodi-app / kodi-twig-provider

1. Go to this page and download the library: Download kodi-app/kodi-twig-provider 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/ */

    

kodi-app / kodi-twig-provider example snippets


$application->run([
    // ...
    KodiConf::SERVICES => [
        // List of Services
        [
            "class_name" => TwigServiceProvider::class,
            "parameters" => [
                // [Mandatory] Absolute path to directory which contains the *.twig files
                Twig::TWIG_PATH             => PATH_BASE."/src/KodiTest/View",
                
                // [Optional] Relative path to page template
                Twig::PAGE_TEMPLATE_PATH    => "/frame/frame.twig",
                
                // [Optional] List of ContentProviders
                Twig::CONTENT_PROVIDERS     => [
                    [
                        "class_name" => PageTitleProvider::class,
                        "parameters" => [
                            "name"  => "page_title",
                            "title" => "Hello world!"
                        ]
                    ],
                    // ...
                ]
            ]
        ]
        // ...
    ],
    // ...
]);

/** @var Twig $twig You can get Twig via Application singleton instance */
$twig = Application::get("twig")->getTwigEnvironment;

/** @var Twig_Environment $twig If you want to use the original Twig_Environment */
$twig = $twig->getTwigEnvironment();

/**
 * Renders the html content to string based on parameter. If the HTTP request is an AJAX request it will render only the template
 * in other cases it renders also the page_template and puts the template content to the appropriate position of the page_template.
 *
 * If you want to prevent the usage of page_template you have to set the $forceRawTemplate parameter to true.
 *
 *
 * @param string $templateName Relative path to *.twig template file
 * @param array $parameters Parameters for twig template file
 * @param bool $forceRawTemplate Prevents the rendering of page template of it is true.
 * @param null $pageTemplate Relative path to a page_template file if you dont want to use the default one.
 * @return string
 * @throws HttpInternalServerErrorException When the pageTemplate does not exist.
 */
public function render($templateName, array $parameters = [], bool $forceRawTemplate = false,$pageTemplate = null) {
    // ...
}

Twig::CONTENT_PROVIDERS => [
    [
        "class_name" => PageTitleProvider::class,
        "parameters" => [
            "name"  => "page_title",
            "title" => "Hello world!"
        ]
    ],
    // ...
]