Shortcode: xx.html.twig not defined

Am trying to understand shortcode creation by copying examples in shortcode-core and shortcode-ui. Am using the files in shortcode-core as a base. Keep hitting my head against an error and need a nudge in the right direction.

Debugger is returning the error:

RuntimeException (404)
Template "partials/sc-bluebox.html.twig" is not defined.

Created two directories inside the shortcode-core directory: templates/partials

Inside /partials is a file called sc-bluebox.html.twig:

<div class="blueBox-wrapper clearfix">
    <div class="blueBox">
        <div class="bbox">{{ shortcode.getContent() }}</div>
    </div>
</div>

The actual shortcode file is: BlueBoxShortcode.php

<?php

namespace Grav\Plugin\Shortcodes;

{
    public function init()
    {
        $this->shortcode->getHandlers()->add('blubox', function(ShortcodeInterface $sc) {

            // Add assets
            $this->shortcode->addAssets('css', 'plugin://shortcode-core/css/sc-sandbox.css');
            $output = $this->twig->processTemplate('partials/sc-bluebox.html.twig', [
                'shortcode' => $sc,
            ]);

            return $output;
        });
    }
}

For some reason (unknown to me), the twig file seems to be not found (404)? At least that’s how I’m reading the error message. I’ve tried a variety of path changes to no avail.

FYI, the markdown text is something like this:

[blubox]This is the content that we're going to insert into the nested blue box.[/blubox]

In your plugin do you have an onTwigTemplates() event that adds your template/partials folder?

Like this: https://github.com/getgrav/grav-plugin-shortcode-ui/blob/develop/shortcode-ui.php#L24-L27

And is it in your list of getSubscribedEvents() ?

Actually your BlueBoxShortcode.php looks invalid… Should look very similar to this:

<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;

class BlueBoxShortcodePlugin extends Plugin
{
    /**
     * @return array
     */
    public static function getSubscribedEvents()
    {
        return [
            'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
            'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
        ];
    }
    /** 
     * Add current directory to twig lookup paths.
     */
    public function onTwigTemplatePaths()
    {
        $this->grav['twig']->twig_paths[] = __DIR__ . '/templates';
    }

    /**
     * Initialize configuration
     */
    public function onShortcodeHandlers()
    {
        $this->grav['shortcode']->registerAllShortcodes(__DIR__.'/shortcodes');
    }
}

@rhukster, thank you for your generous reply to my query. However, there may have been a misunderstanding. I wasn’t creating a new plugin, but, rather, simply adding an additional shortcodeinto the already existing shortcodes directory:

/user/plugins/shortcode-core/shortcodes

This is a learning exercise for me. It’s obvious that I need to study more working shortcode examples. Thanks.

I had created the sc-bluebox.html.twig file with the intent of learning about creating more complex shortcodes. My issue was that this .html.twig file I created was apparently not being recognized. The following simplified shortcode (BlueBoxShortcode.php) works fine.

<?php

namespace Grav\Plugin\Shortcodes;

use Thunder\Shortcode\Shortcode\ShortcodeInterface;

class BlueBoxShortcode extends Shortcode
{
    function init() 
    {
        $this->shortcode->getHandlers()->add('blubox', function(ShortcodeInterface $sc) {

            // Add assets
            $this->shortcode->addAssets('css', 'plugin://shortcode-core/css/sc-sandbox.css');
            return '<div class="blueBox-wrapper clearfix">
                      <div class="blueBox">
                        <div class="bbox">'.$sc->getContent().'</div>
                      </div>
                    </div>';
        });
    }
}

Need to study more example code. Looking forward to the new slider (with shortcode) you’re building.

Hmm. could of sworn i replied to this already.

Basically the short answer is that shortcode-core doesn’t support the Twig templates like shortcode-ui because it’s not adding the twig paths like I described in the original reply.

This is why it won’t work when you drop it into shortcode-core. However, it probably would of worked if you dropped it into shortocode-ui :slight_smile:

The best approach is to create your own plugin with any shortcodes you might wish to use like the example php file I gave before.

Appreciate the follow-up and the nudge in the right direction. You answer above provides a significant clue as to why I was stuck. Thanks. Onward.