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

Alternative to "useCustomPageTemplate" #45

Closed
pamtbaau opened this issue Dec 9, 2021 · 2 comments
Closed

Alternative to "useCustomPageTemplate" #45

pamtbaau opened this issue Dec 9, 2021 · 2 comments

Comments

@pamtbaau
Copy link

pamtbaau commented Dec 9, 2021

The setting useCustomPageTemplate allows the shortcode to be added to a page which doesn't use template calendar.html.twig, which gives me the impression that template calendar.html.twig isn't needed at all.

It seems the main use of calendar.md is to selectively activate the plugin instead of activating it on each and every page.

Considering the above, other ways of activating the plugin selectively could be used.

Two alternatives:

Both alternative provide the user the ability to add the shortcode to any page, without using template calendar.html.twig and still only active the plugin on specific pages.

Option 1

  • Use variable route in config file
routes:
  - /typography
  - /modular
  - /blog
  - /blog/hero-classes

In fullcalendar.php only enable eventhandlers when page is on route:

public function onPluginsInitialized()
{
  ...
  if ($this->isOnRoute()) {
     $this->enable([
	 'onGetPageTemplates' => ['onGetPageTemplates', 0],
	 'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
	 'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
	 'onAssetsInitialized' => ['onAssetsInitialized', 0],
     ]);
  }
}

private function isOnRoute(): bool
{
   $uri = $this->grav['uri']->uri();

   $routes = $this->config->get("plugins.$this->name.routes", '');

   $enable = $routes && (
      (gettype($routes) === 'string' && $routes === $uri) ||
      (is_array($routes) && in_array($uri, $routes)));

   return $enable;
}

Option 2

Add new variable to frontmatter of page on which plugin should be activated:

---
title: Modular
fullcalendar: true
---

[fullcalendar ... /]

In fullcalendar.php only add assets when frontmatter contains fullcalendar === true:

public function onPluginsInitialized()
{
  ...
  $this->enable([
    'onGetPageTemplates' => ['onGetPageTemplates', 0],
    'onShortcodeHandlers' => ['onShortcodeHandlers', 0],
    'onTwigTemplatePaths' => ['onTwigTemplatePaths', 0],
    'onPageInitialized' => ['onPageInitialized', 0],
  ]);
}

public function onPageInitialized(Event $event)
{
   /** @var Page */
   $page = $event['page'];

   if (isset($page->header()->fullcalendar) && $page->header()->fullcalendar === true) {
      $this->addAssets();
   }

Advantage/disadvantage

Option 1

  • Advantage:
    • None of the events will be handled when not on route.
  • Disadvantage:
    • When items containing shortcode are shown on /blog using filter, the route becomes /blog/tag:photography#body-wrapper. This can be fixed by using regex when comparing. Eg. #/blog(/.*)?#

Option 2

  • Advantage:
    • No problems with filtered blog items (see above)
    • Disadvantage: Some events are run before onPageInitialized and must be handled.
@wernerjoss
Copy link
Owner

well, calendar.html.twig is used to selectively activate the plugin instead of activating it on each and every page,
that is correct.
but the template does also additional things, which were the reason to use it even before.
one is to find calendar files (.ics) in the page folder and, if found, adds them to the calendars to display.
another is to provide the monthly picture feature.
so, it is needed for anyone who wants the mentioned features.
plus, anyone who does not like it as-is, can make a copy in the theme folder (which will then override the original) and modify that at will, according to other needs.
the useCustomTemplate Flag, however, is somewhat obscure, and ATM not so well documented, I agree.
I'll think about the proposed options, tend to #2.
however, I will first try to solve issue #44, which is more important, I think.

@wernerjoss
Copy link
Owner

Option 2 has now been implemented, the useCustomTemplate Flag is kept for backwards compatibility, but removed from the blueprint - also mentioned in the docs.

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

No branches or pull requests

2 participants