hello again! is it possible to do sth like category_x.yaml for e.g. each blog category – or say a subtree_b.yaml whose settings would make definitions in page headers unneccessary? i have mind authors just dropping their articles into specialized folders without having to do those anything but easy yaml definitions. so do you think it is possible to say: every markdown file beside this local yaml file will get a page header “taxonomy: category: event” and “taxonomy: tag: theatre”? thanks a lot for your patience! (i am novice to “templating from scratch”
)
First make sure you put backticks around filenames and such or this forum will mess up any names with underscores because it thinks you are doing italics.
Anyway, No there is no way to have a specific yaml that provides inherited values for a particular subfolder. However, you could achieve this kind of functionality by providing custom settings in the site.yaml, and dynamically looking up a set of these settings based on your current folder.
So something like this in site.yaml:
events:
taxonomy:
category: event
tag: theatre
Then in your twig template called perhaps events.html.twig
:
{% set taxonomy = site.events.taxonomy %}
Of course this is not going to be directly associated to the page, merely useful for rendering. If you wanted to do something and automatically add header values to a page, you could write a simple plugin to do so.
Ah! Thought that YAML has functionality like TypoScript: Setting/getting variables from anywhere. So there is no cascading and possibility to overwrite variables from top to bottom? (Please forgive me. I know you are not my personal trainer.)
I like your alternative procedure. But I don’t understand the whole namespace thing. Is there something like a map or cheatsheet to get an idea of the differences and functions of p. ex. site and page or … when to use config.sth, when page.sth.?
Maybe the best would be to have a look at some simple plugins to get more familiar with this topic as a whole. Giants onto my shoulders. Thank you very much!
Grrr… just lost my huge reply! Time for shorter version:
-
All YAML configuration is accessed via the Config object, so
$grav['config']->get('site.something.else')
will access thesite: something: else
value fromsite.yaml
. The same approach works for config, plugins, anything really. -
The preferred location for any custom settings is
site.yaml
. Nothing precludes you from usingsystem.yaml
but that is intended for Grav-core configuration and settings.site.yaml
is for your site settings, so use that.
Do you sleep sometimes?
From Taxonomy.php lines 49 to 67:
public function addTaxonomy(Page $page, $page_taxonomy = null)
{
if (!$page_taxonomy) {
$page_taxonomy = $page->taxonomy();
}
/** @var Config $config */
$config = $this->grav['config'];
if ($config->get('site.taxonomies') && count($page_taxonomy) > 0) {
foreach ((array) $config->get('site.taxonomies') as $taxonomy) {
if (isset($page_taxonomy[$taxonomy])) {
foreach ((array) $page_taxonomy[$taxonomy] as $item) {
// TODO: move to pages class?
$this->taxonomy_map[$taxonomy][(string) $item][$page->path()] = array('slug' => $page->slug());
}
}
}
}
}
This must be the place where to add an extra check for say “Is there a site.yaml in the current folder?”, isn’t it? As soon I have learned enough PHP I will try to hack this.
This is where the plugin comes in. You could add to the taxonomies directly during the onPageProcessed event. This way you it will cache the page headers with your dynamically added taxonomies. We are doing something similar to this in the archives
plugin. Basically it’s calculating a year, and dynamically adding that year as a taxonomy to the pages so that they can be easily grouped and displayed in a list.
If you use plugin, you can very simply load any yaml file from the filesystem by using
$array = \Grav\Common\File\CompiledYamlFile::instance($filename)->content();
This will also cache the yaml file into PHP file making it faster. If the file doesn’t exist, it just returns empty array.