-1

I created a plugin with the createIndexData function:

public static function createIndexData($file): bool
{
    $pages = self::getPages();
    $grav = Grav::instance();
    if($pages !== null){
        $locator = $grav['locator'];
        $grav['twig']->twig_paths[]= array_merge(Grav::instance()['twig']->twig_paths, $locator->findResources('theme://templates'));
        $grav->fireEvent('onTwigTemplatePaths');

        $page = $pages->find('/austria/_price-list');
        /*dump($page);*/
        dump($page->content());
        exit();
    }
    return false;
}

The plugin is called in onPagesInitialized. I just need to get the processed content from the modular page. In the modular page, I include content from another page: (/pages/austria/_price-list)

{% set moduleName= 'price-list' %} 
{% set pageUrl = page.find(uri.path).rawRoute[1:] %} 
{% set pageUrl = '/global-variables-and-templates/price-list/data/_austria' %} 
{% set page = page.find(pageUrl) %} 
{% if not page %} 
   PAGE NOT EXIST!!!!<br />url: {{pageUrl}}<br />modul-name: {{moduleName}} 
{% else %}
    {% include page.template~'.html.twig' with {'content' : page.content} %} 
{% endif %}

plugin returns error: Template "modular/price-list-global.html.twig" is not defined. What is wrong? Or how to get the processed content from the modular page in grav in the custom plugin?

DarkBee
  • 16,592
  • 6
  • 46
  • 58
  • What you're trying to achieve is unclear to me and you might be going into the wrong direction. You might want to consider editing your question to add some clarity. – passerby Jan 29 '22 at 18:00
  • I need to get the content of a modular page. I need to get the content of a modular page that is processed (twig). When I call $page->content(), during processing "include page" an error is displayed: Template "xyz" is not defined – Daniel Kopťák Jan 29 '22 at 18:58
  • Repeating the question in one sentence doesn't make it more clear... 1) Where do you need the content of the module? In PHP or Twig? About PHP snippet: 2) What are you trying to achieve with function 'createIndexData($file)'? 3) Code seems messy, please only show correct code. 4) Where is required template created? In theme or current plugin? 5) Why is event being fired? About Twig snippet: 6) You are shadowing variable 'page' which is considered not done.. 7) Why are you including the template of the page to be included? 8) ... – passerby Jan 30 '22 at 07:03

1 Answers1

-1

solution for plugin error: Template "modular/some-template.html.twig" is not defined add to function:

$grav = Grav::instance();
$grav['twig']->twig_paths[]= __DIR__ .'/../../../themes/yourtheme/templates';
  • If template is defined in theme, there is no need to add it to Twig's paths. If you need a hardcoded path, you need to scratch your head... – passerby Jan 30 '22 at 07:09