Skip to content

Creating Views

Scaffold:Native is not a compiler template engine. It uses native PHP syntax similar to Plates and FoilPHP. Like majority of template engines, Scaffold:Native supports inheritance through blocks. Furthermore, child templates declare blocks that can be overridden, extended and displayed by parent templates.

All Scaffold:Native templates must follow the namepace::relative/path/to/template format.

<?php

$this->parent('cms::layout');
$this->block('frontend', function ($params) {

?>
<article>
    <header>
        <h1><?=$this->esc($this->ucfirst($params['title']));?></h1>
    </header>
    <main>
        <?php foreach($params['paragraphs'] as $paragraph): ?>
            <p>
                <?=$this->esc($paragraph);?>
            </p>
        <?php endforeach; ?>
    </main>
</article>
<?php }); ?>



<html>
    <head>
        <title><?=$this->esc($title);?></title>
    </head>
    <body>
        <?=$this->block('frontend');?>
    </body>
</html>

Namespace and function callbacks are registered with the templating engine when it is constructed. Function callbacks area available as methods within the template context and must be callable.

The default template extension is phtml, and all template files live in the resources/views folder.

<?php

declare(strict_types=1);

namespace App\Infrastructure\Http\Controllers;

use Codefy\Framework\Http\BaseController;
use Qubus\View\Native\Exception\InvalidTemplateNameException;
use Qubus\View\Native\Exception\ViewException;

use function Qubus\Security\Helpers\die__;

final class HomeController extends BaseController
{
    /**
     * @throws ViewException
     * @throws InvalidTemplateNameException
     */
    public function index(): ?string
    {
        $params = [
            'title' => 'CodefyPHP Framework',
            'paragraphs' => [
                'My first paragraph.',
                'My second paragraph.',
            ],
        ];

        try {
            return $this->view->render('cms::index', $params);
        } catch (InvalidTemplateNameException | ViewException $e) {
            die__($e->getMessage());
        }
    }
}

Registered Function Callbacks

  • strip - Properly strip all HTML tags including script and style (default). This differs from PHP's native strip_tags() function because this function removes the contents of the tags.
    • Parameters
      • string $string - String containing HTML tags
      • bool $removeBreaks - Optional. Whether to remove left over line breaks and white space characters.
      • string $tags - Tags that should be removed.
      • bool $invert - Instead of removing tags, this option checks for which tags to not remove. Default: false.
  • trim - Removes all whitespace.
  • upper - Make a string uppercase (similar to ucwords).
  • lower - Make a string lowercase.
  • ucfirst - Uppercase the first character in a string.
  • lcfirst - Lowercase the first character in a string.
  • ucwords - Uppercase the first character of each word in a string.
  • esc - Escaping for HTML output.
  • escJs - Escaping for inline javascript.
  • escUrl - Escaping for url.
  • purify - Makes content safe to print on screen. To be used instead of esc for escaping rich text.
  • truncate - Truncates a string to the given length. It will optionally preserve HTML tags if $isHtml is set to true.
    • Parameters
      • string $string The string to truncate.
      • int $limit The number of characters to truncate.
      • string $continuation The string to use to denote it was truncated. Default ...
      • bool $isHtml Whether the string has HTML.
  • concat - Concatenation with separator (strings only).
    • Parameters
      • string $string1 Left string.
      • string $string2 Right string.
      • string $separator Delimiter to use between strings. Default: comma.
      • string ...$strings List of strings.