chevere/danky

Typed template system

0.3.0 2022-12-26 13:11 UTC

This package is auto-updated.

Last update: 2024-11-14 15:01:47 UTC


README

Chevere

Build Code size Apache-2.0 PHPStan Mutation testing badge

Quality Gate Status Maintainability Rating Reliability Rating Security Rating Coverage Technical Debt CodeFactor Codacy Badge

What is Danky?

Danky is a typed template system for PHP. Contrary to all other template systems, in Danky templates are classes.

🦄 Templates explicit declare its scope on construct, the $render property can be of type string or Template.

<?php // Quote.php

use Chevere\Danky\Template;

class Quote extends Template
{
    public function __construct(string $text, string $author) {
        $this->render =
            <<<HTML
            <quote>"$text" --$author</quote>
            HTML;
    }
};

That <<<HTML ... is Heredoc syntax string literal. In Danky, you use all the stuff that has been always there to handle multi-line string literals. Heredoc is great for templates as it evaluates variables, making templates clean to read.

<?php // Home.php

use Chevere\Danky\Template;

class Home extends Template
{
    public function __construct(Template $content) {
        $this->render =
            <<<HTML
            <main>
                $content
            </main>
            HTML;
    }
};

Template classes implements Stringable, you can use any template object within string literals.

<?php // index.php

use function Chevere\Danky\import;
use Home;
use Quote;

echo
    new Home(
        content: new Quote(
            text: 'Hello, world!',
            author: 'Rodolfo'
        )
    );

🥳 Congratulations! You just mastered Danky.

<main>
    <quote>"Hello, world!"</quote>
</main>

Now run php demo/index.php for a more complete example.

License

Copyright 2022 Rodolfo Berrios A.

Chevere is licensed under the Apache License, Version 2.0. See LICENSE for the full license text.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.