Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Processing of Twig and caching

Started by Muut Archive 10 years ago · 2 replies · 596 views
10 years ago

I would like to hide or show parts of a page depending on whether a user is logged in or not. Based on what rhukster suggests in Hiding parts of pages I use something like:
---twig
{% if grav.user.authorize('site.login') %}
This is text only logged users can see.
{% endif %}

YAML
This works fine when caching is disabled. However when caching is enabled it does not work. The previously cached page content gets output even though `process: twig: true` is set in the page header. This is the front matter for the (modular) page:
---yaml
title: live
published: true
process:
    twig: true
    markdown: false
routable: false
cache_enable: true
visible: false

The crude way is to disable caching completely but I keep hoping to find a way to serve cached content to non logged in users (anonymous visitors) for performance reasons.
Again any help is much appreciated!

10 years ago

From how I understand the life cycle, the cache check is too early for the process: twigsetting to matter. To do what you ask, I think you'd have to disable the global cache and write a plugin to manage the cache itself (which is theoretically possible; the API is exposed). The problem is that the concept of "logged in" is not a part of the core code. It's a plugin.

Maybe a dev will have another idea. I'm not sure this idea would work right now because there may not be event hooks in the right places.

10 years ago

I was already beginning to think this was a hard nut to crack and so it appears to be.
Not as a developer nor as an architect, I do think security should be in the a core. Of course, with the possibility to extend it's functionality using plugins.
For now I will pursue witch caching disabled. Grav is so fast I hardly notice any difference with or without caching enabled. Many thanks!

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1359 9 years ago
Archive · by Muut Archive, 9 years ago
2 936 9 years ago
Archive · by Muut Archive, 9 years ago
2 4066 9 years ago
Archive · by Muut Archive, 9 years ago
1 2957 9 years ago
Archive · by Muut Archive, 9 years ago
3 1121 9 years ago