Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resource Locator API not documented #851

Open
NicoHood opened this issue Nov 9, 2020 · 6 comments
Open

Resource Locator API not documented #851

NicoHood opened this issue Nov 9, 2020 · 6 comments

Comments

@NicoHood
Copy link
Contributor

NicoHood commented Nov 9, 2020

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
Copy link
Member

rhukster commented Nov 9, 2020

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

@NicoHood
Copy link
Contributor Author

NicoHood commented Nov 9, 2020

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
Copy link
Member

rhukster commented Nov 9, 2020

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
Copy link
Contributor Author

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
Copy link
Member

mahagr commented Jan 15, 2021

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
Copy link
Contributor Author

I dont understand. For me the suggested patch works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants