Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Shortcode: xx.html.twig not defined

Started by Muut Archive 10 years ago · 5 replies · 594 views
10 years ago

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:

TXT
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:

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

The actual shortcode file is: BlueBoxShortcode.php

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:

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

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

PHP
<?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');
    }
}
10 years ago

@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:

TXT
/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
<?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.

10 years ago

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 :)

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.

10 years ago

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.

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1359 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2957 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago