Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overriding templates #23

Open
afbora opened this issue Aug 23, 2022 · 7 comments
Open

Overriding templates #23

afbora opened this issue Aug 23, 2022 · 7 comments

Comments

@afbora
Copy link

afbora commented Aug 23, 2022

Imagine I'm building a generic theme. But I need to overwrite some templates for a client. So I want it to look in multiple directories. And I want to put the templates I want to be overwritten under /site/templates.

For example:

Default template for theme path: /theme/templates/default.blade.php
Override template path: /site/templates/default.blade.php

Is this possible or is it a feature request?

@fabianmichael
Copy link
Collaborator

I am currently working on a template and component registry in a feature branch, see https://github.com/lukasleitsch/kirby-blade/tree/feature/components-registry

The goal is to make it possible for themes to be a regular plugin. The new branch allows you to do the following:

Kirby::plugin('my/theme', [
    'blade' => [
        'namespaces' => [
            'mynamespace => 'My\\Theme\\Components', // allows usage of <x-mynamespace::component-name />
        ],
        'templates' => __DIR__ . '/templates',
        'components' => [
          'demo' => Name\\Space\\SomeComponent::class, // Use class in arbitrary namespace as component
        ],
    ],
    'snippets => [
        'menu' => __DIR__ . '/snippets/menu.blade.php',
    ],
    'templates' => [
        'default' => __DIR__ . '/snippets/default.blade.php',
    ],
]);

As you can see, this gives you a ton of options for registering additional templates, snippets etc. The only thing I could not manage to implement are anonymous namespaced components. But apart from that, it already works very stable for a theme that I am currently working on. I decided to build it as a plugin, so updates can be handled through composer or a git submodule.

If you want to keep theme files within /theme, you could probably just write a companion plugin that registers the corrects paths.

@afbora
Copy link
Author

afbora commented Aug 24, 2022

Thank you for the information.

Then the overriding will be at plugin level, not possible at directory level. So in order to overwrite a template it will need to be registered via the plugin. Have I got it right?

@fabianmichael
Copy link
Collaborator

Thank you for the information.

Then the overriding will be at plugin level, not possible at directory level. So in order to overwrite a template it will need to be registered via the plugin. Have I got it right?

If you set blade.templates in a plugin, that path will be searched by Blade when looking for templates and components first. You could also define single files by using the templates and snippets keys instead, just like any other plugin in a non-Blade setup would. As soon, as the file also exists within the site/templates (or site/snippets) folder, that file is used instead. I tried to replicate how Kirby handles this out-of-the box: look for a template file within site/templates or if that does not exist: look for a plugin-provided template. I hope this answers your question?

@afbora
Copy link
Author

afbora commented Aug 24, 2022

Kirby::plugin('my/theme', [
    'blade' => [
        'templates' => __DIR__ . '/templates',
    ],
]);

Ah, now I understand a little more. Considering I set it up like in your example, it will first look in the site/templates directory and then in the path specified in the plugin, right?

@afbora
Copy link
Author

afbora commented Aug 30, 2022

@fabianmichael When do you plan to release the components-registry feature?

@fabianmichael
Copy link
Collaborator

fabianmichael commented Aug 31, 2022

@afbora As soon as I find the time for updating the README … don’t know when that will be, sorry.

@fabianmichael
Copy link
Collaborator

fabianmichael commented Oct 11, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants