getgrav / grav-learn

Grav Learn (exhaustive grav documentation)
http://learn.getgrav.org
MIT License
232 stars 787 forks source link

Resource Locator API not documented #851

Open NicoHood opened 4 years ago

NicoHood commented 4 years ago

As stated in this forum post The Resource Locator is not documented anywhere. The Forum Post is the only useful hit available via google and the link to the API is dead now. I found the source here

Especially I'd like to know which other alias exist like user-data:// etc. I'd need to look for the location of a template.

Edit: Is it possible that there is no templates:// resource? Is it possible to add such a resource that looks into all templates provides by all plugins and theme? I've written a patch.

rhukster commented 4 years ago

I'll add resource locator to the list of things to document. Cheers.

NicoHood commented 4 years ago

The reason for adding the docs or adding templates://?

  1. It is used in multiple plugins but the parameters are not explained anywhere.

  2. I want to create a plugin that adds structured data (json-ld) to the page. It processes the page.header and if it finds an entry it searches a corresponding template.

For example:

structured_data:
    local_business:
        name: 'Pizza Restaurant'
        address: [...]
// Check if a template exists for the given data
$template = 'structured-data/json-ld/' . $key . '/' . $key . '.json.twig';
if ($locator->findResource('templates://' . $template, true, false) === false) {
    continue;
}
$this->grav['twig']->processTemplate($template);
[...]
rhukster commented 4 years ago

You don't need a stream for this, the easier solution is just this:

if (in_array($template, $this->grav['twig']->twig_paths)) {
  continue;
}
NicoHood commented 4 years ago

I've checked, but your solution does not work. The twig_paths contains the path of the template folder while the locator will search for the actual template file.

  0 => "/grav/user/themes/quark/templates"
  1 => "/grav/user/plugins/email/templates"
  2 => "/grav/user/plugins/form/templates"
  3 => "/grav/user/plugins/login/templates"
  4 => "/grav/user/plugins/ratings/templates"
  5 => "/grav/user/plugins/breadcrumbs/templates"
  6 => "/grav/user/plugins/pagination/templates"
  7 => "/grav/user/plugins/seo/templates"
  8 => "/grav/user/plugins/error/templates"
  9 => "/grav/system/templates"

vs

structured-data/json-ld/local_business/local_business.json.twig

Additionally the locator checks if the file also exists on the disk. I could also write code to deal with this case, but well... that is what the locator is used for. And it would make most sense to add the code $locator->addPath('templates', '', $this->grav['twig']->twig_paths); to the upstream project, rather than in my plugin itself.

mahagr commented 3 years ago

The issue is that with the current logic it's almost impossible to keep template:// up to date with twig paths (yeah, unfortunately they can change). At some point it would be really cool to add this behavior...

NicoHood commented 3 years ago

I dont understand. For me the suggested patch works.