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

Allow Template selection to be context aware (ASP.NET DisplayMode) #470

Open
SpeedyStuart opened this issue Feb 1, 2022 · 3 comments
Open

Comments

@SpeedyStuart
Copy link

Problem:
I need to be able to use different liquid templates based on runtime information (cookie, user-agent, etc.). Similar to DisplayMode in ASP.NET.

Whilst it is possible to provide a custom File Provider which contains this logic and returns appropriate templates, the Template Cache in FluidViewRenderer just caches templates based on path - so renders my logic useless.

Possible change

  • Add a new delegate to FluidViewEngineOptions that takes the template path and returns a Template Cache key (string)
  • Use this delegate (if present) in GetFluidTemplateAsync to generate the key for each template in the Template Cache

e.g.

// Allow templates to be cached by external factors
// Typically the rule would match any rules in custom IFileProvider implementations
// e.g. Serve different templates based on Authentication status / User Agent etc.
string cacheKey = path;
if (_fluidViewEngineOptions.TemplateCacheKeyProvider!=null)
{
    cacheKey = _fluidViewEngineOptions.TemplateCacheKeyProvider.Invoke(path);
}

if (cache.TemplateCache.TryGetValue(cacheKey, out var template))
{
    return template;
}

template = await ParseLiquidFileAsync(path, fileProvider, includeViewStarts);

cache.TemplateCache[cacheKey] = template;

return template;
@sebastienros
Copy link
Owner

Looks like a CMS feature ;) we have the same thing in Orchard.

Are you using the FluidMvcViewEngine engine or just the FluidViewEngine ?

@SpeedyStuart
Copy link
Author

Ah, ok.

The Mvc one.

@SpeedyStuart
Copy link
Author

Created pull request #486 for this

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