Can't load template in plugin?

I’m working on my first plugin. I know this topic has come up before, but I use a maintenance page on almost all of my websites and want to be able to install it with the kirby cli–they are very similar. So I’m building a plugin for it.

I’m trying to figure out how to load a maintenance page, but the template doesn’t load–it only sends me to the error page.

Here are the two main docs in the plugin:

# site/plugins/maintenance/maintenance.php
<?php

    /**
    * Set custom maintenance template
    **/
    $kirby->set('template', 'maintenance', __DIR__ . '/templates/maintenance.php');

    /**
    * route to maintenance page
    * if maintenance mode on
    **/
    $kirby->set('route', array(
        'pattern' => '/',
        'action'  => function() {
            $site = kirby()->site();
            $maintenanceMode = $site->maintenance_mode()->isTrue();

            if($maintenanceMode) {
                return go('/maintenance');
            } else {
                return site()->visit('/');
            }
        }
    ));
<!-- site/plugins/maintenance/templates/maintenance.php -->
    <?php snippet('header') ?>
    <h1>this is the maintenance page</h1>
    <?php snippet('footer') ?>

this is a bit out of my depth but i think you have to use tpl:load I think your getting the error page becuase the content is missing? This is basically a static page so maybe the solution in this thread useful.

i think what is happening is that your route is working, its hopping on to /maintence but coming up blank for content, and moving onto a 404 as a result. I think you need to force it load a template for the /maintence page, which is a virtual/static page.

Thanks @jimbobrjames! That linked page is different in my mind because it’s not a plugin and they didn’t use the Kirby extension registry. If the registry is available to plugins, why would I need to do the tpl:load method?

One more question: if I don’t have any content for this page, because its a coming soon page, would I need to have a content folder for it? Or could I include the content folder in the plugin so it would be entirely self-contained. :grin:

If you don’t load the template, sending the user to the maintenance page will only work if the content folder exists.

And you can register all sorts of stuff, but you can’t register a content folder. You could, however, make your plugin create that page from your plugin.

<?php

    /**
    * Set custom maintenance template
    **/
    $kirby->set('template', 'maintenance', __DIR__ . '/templates/maintenance.php');

    /**
    * route to maintenance page
    * if maintenance mode on
    **/
    $kirby->set('route', array(
        'pattern' => '/',
        'action'  => function() {
            $site = kirby()->site();
            $maintenanceMode = $site->maintenance_mode()->isTrue();

            if($maintenanceMode) {
                if(!$page = page('maintenance')) {
                  try {

                    $newPage = $site->children()->create('maintenance', 'maintenance', array(
                      'title' => 'Maintenance Page',
                      'text'  => 'This page is currently in maintenance mode',
                    ));

                  } catch(Exception $e) {

                    echo $e->getMessage();

                  }
                  }
                return go('maintenance');
            } else {
                return site()->visit('/');
            }
        }
    ));
2 Likes

Thanks @texnixe! So if I use the plugin register method for the template like so:

$kirby->set('template', 'maintenance', __DIR__ . '/templates/maintenance.php');

…this basically does the same thing as tpl:load?

And If I have therefore loaded the template, do I still need a content folder if I include all my content in the template itself? Basically I’m saying, “this site is coming soon…” and don’t need any real content.

No, registering just means that Kirby knows where to find the template (it would otherwise look for it in site/templates, not in your plugin folder).

You would still have to use tpl::load() to load the template, because this is not done automatically. How would Kirby know what template to load for a page that doesn’t exist? By default, the name of the content file determines the template that renders the content. In the absence of this, you have to tell Kirby to load the template.

There are a few downsides to the tpl::load() method, you would have to pass the variables on to the template and down to the snippets used within the template.

I’d probably still rather just load the template then to create a new page - and no, if you load the template, you don’t need a content folder.

See this example: Render a template without the corresponding content

1 Like