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

Introduce HttpRequest & HttpContext Fluid values #16979

Merged
merged 7 commits into from
Nov 9, 2024

Conversation

hishamco
Copy link
Member

@hishamco hishamco commented Nov 8, 2024

Last two Liquid accessor


return name switch
{
nameof(HttpContext.Items) => new ObjectValue(new HttpContextItemsWrapper(httpContext.Items)),
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add Request, User .. etc and deprecate the Request object in a major release

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about this one @sebastienros?


public override ValueTask<FluidValue> GetValueAsync(string name, TemplateContext context)
{
var httpContext = _context ?? GetHttpContext(context).ToObjectValue() as HttpContext;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't cache the _context, it's specific to each request. Get a new value of the context every time this method is called. And don't wrap it in an ObjectValue

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you want to cache the HttpContext in case the fluid value is used multiple times (not common I'd say, and for what gain). You could use the TemplateContext object which has a dictionary. And use a private constant to put it there. But again might be more work that benefits so I wouldn't advise that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't cache the _context, it's specific to each request. Get a new value of the context every time this method is called. And don't wrap it in an ObjectValue

The question again, how ToXXXValue() will be evaluated or should we ignore it for this particular object?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed it. Now I understand your question. And now you have your answer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically we have to decide what to render when we do {{ Request }}. Here it's returning "Request", but we could decide to return the url (even if it's more costly) or something that is valuable. I am fine if you think "" is actually better in that case, or "(Request)" if that helps users understand that it's an object.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with your changes, I was struggling to display something useful if we render {{ Request }} but what you did is much simpler and doesn't require construction injection

@sebastienros sebastienros merged commit ebde3ec into main Nov 9, 2024
27 checks passed
@sebastienros sebastienros deleted the hishamco/liquid-accessor branch November 9, 2024 21:04
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

Successfully merging this pull request may close these issues.

2 participants