I'm trying to render a template from a console command, but i'm getting the following error:

Craft\ConsoleApp and its behaviors do not have a method or closure named "getTheme"

The code looks like this:

public function actionIndexTemplate ($id = null)
    $criteria          = craft()->elements->getCriteria(ElementType::Entry);
    $criteria->section = 'products';
    $criteria->limit   = 1;
    $criteria->id      = $id;
    $entry             = $criteria->first();

    if ( $entry ) {
        $output = craft()->templates->render('indexer/Product', [
            'element' => $entry
        $data       = json_decode($output, $assoc = true);
    else {
        echo "No entry found\n";

And here is the stack trace:

CException: Craft\ConsoleApp and its behaviors do not have a method or closure named "getTheme". in /craft/app/framework/base/CComponent.php:266
Stack trace:
#0 /craft/app/framework/base/CErrorHandler.php(390): CComponent->__call('getTheme', Array)
#1 /craft/app/framework/base/CErrorHandler.php(340): CErrorHandler->getViewFile('exception', 500)
#2 /craft/app/etc/errors/ErrorHandler.php(233): CErrorHandler->render('exception', Array)
#3 /craft/app/etc/errors/ErrorHandler.php(137): Craft\ErrorHandler->handleTwigError(Object(Craft\TemplateLoaderException))
#4 /craft/app/framework/base/CErrorHandler.php(131): Craft\ErrorHandler->handleException(Object(Craft\TemplateLoaderException))
#5 /craft/app/framework/base/CApplication.php(750): CErrorHandler->handle(Object(CExceptionEvent))
#6 [internal function]: CApplication->handleException(Object(Craft\TemplateLoaderException))
#7 {main}%

Here is the underlying error:

2016/11/23 18:15:13 [error] [exception.Craft\TemplateLoaderException] Craft\TemplateLoaderException: Ikke i stand til å finne malen “indexer/Product”. in /craft/app/etc/templating/twigextensions/TemplateLoader.php:123
Stack trace:
#0 /craft/app/etc/templating/twigextensions/TemplateLoader.php(71): Craft\TemplateLoader->_findTemplate('indexer/P...')
#1 /craft/app/vendor/twig/twig/lib/Twig/Environment.php(312): Craft\TemplateLoader->getCacheKey('indexer/P...')
#2 /craft/app/vendor/twig/twig/lib/Twig/Environment.php(378): Twig_Environment->getTemplateClass('indexer/P...', NULL)
#3 /craft/app/etc/templating/TwigEnvironment.php(61): Twig_Environment->loadTemplate('indexer/P...', NULL)
#4 /craft/app/vendor/twig/twig/lib/Twig/Environment.php(347): Craft\TwigEnvironment->loadTemplate('indexer/P...')
#5 /craft/app/services/TemplatesService.php(253): Twig_Environment->render('indexer/P...', Array)
#6 /craft/plugins/indexer/services/Indexer_IndexService.php(136): Craft\TemplatesService->render('indexer/P...', Array)
#7 /craft/plugins/indexer/consolecommands/IndexerCommand.php(93): Craft\Indexer_IndexService->renderTemplate(Array)
#8 [internal function]: Craft\IndexerCommand->actionIndexTemplate('35262')
#9 /craft/app/framework/console/CConsoleCommand.php(172): ReflectionMethod->invokeArgs(Object(Craft\IndexerCommand), Array)
#10 /craft/app/framework/console/CConsoleCommandRunner.php(71): CConsoleCommand->run(Array)
#11 /craft/app/framework/console/CConsoleApplication.php(92): CConsoleCommandRunner->run(Array)
#12 /craft/app/framework/base/CApplication.php(185): CConsoleApplication->processRequest()
#13 /craft/app/etc/console/yiic.php(81): CApplication->run()
#14 /craft/app/etc/console/yiic(4): require_once('/Ind...')
  • Can you share the full stack trace leading up to that error?
    – Brad Bell
    Commented Nov 23, 2016 at 17:48
  • @BradBell added it to the post. Commented Nov 23, 2016 at 18:08
  • Looks like another error is happening first, then the getTheme error happens when Craft tries to render the default "exception" template. Try checking craft/storage/runtime/logs for the underlying error message on the first error.
    – Brad Bell
    Commented Nov 23, 2016 at 18:10
  • @BradBell Looks like it's not finding the template, and is throwing a TemplateLoaderException. Where does it look for templates when running in the context of a command? The same call works in a controller. I also guess the templateloader should an exception in console mode more gracefully, as to not trigger the getTheme() error. Commented Nov 23, 2016 at 19:29
  • Agree the 2nd issue is a bug. Try using setTemplatesPath to your plugin's template folder like so: craftcms.stackexchange.com/a/4392/57
    – Brad Bell
    Commented Nov 23, 2016 at 19:42


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.