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

Access root object from template #575

Open
ssougnez opened this issue Jun 8, 2023 · 3 comments
Open

Access root object from template #575

ssougnez opened this issue Jun 8, 2023 · 3 comments

Comments

@ssougnez
Copy link

ssougnez commented Jun 8, 2023

I'm having this use case where I simply want to display a JSON representation of the object passed to the template.
I cannot pass an object with a Root property, I really have to pass the object itself.

I tried with "{{ context | json }}" or "{{ model | json }}" but nothing works.

So the question is "is it possible to access the root object from within the template ?".

Thanks :-)

@sebastienros
Copy link
Owner

If you use the overload of TemplateContext that passes a model no. You could optionally add the same object as a custom property, something like 'Model' too.

If that doesn't answer your question I could use another more detailed example to understand what you are trying to do. But also I am not sure | json would do what you expect. Maybe you need a custom filter (or overwrite this one) to do something special.

@ssougnez
Copy link
Author

ssougnez commented Jun 9, 2023

The idea is simply for debug purpose. I want to be able to display a json representation of the object passed to the model. I could create a custom filter but the issue is the same, I don't know what to apply the filter one.

I know it would be easier to pass an object with a single property that actually contains my object but it would mean that I have to update all my templates to take that into account. Which I why I was wondering if it was possible to access the object somehow in the template.

@sebastienros
Copy link
Owner

I understand that you are passing the model to TemplateContext now. You can't access it directly from the template. What you can do is "also" add it as a custom property by doing templateContext.SetValue('TheModel', model) or any custom property you'd like.

Also in case that could give you some ideas, in Orchard we created a custom filter that will dump an object into the javascript console, so we can see the object graph in the console of developer tools:
https://github.com/OrchardCMS/OrchardCore/blob/970f002a168cc99181462ea10101612e8a68bf96/src/OrchardCore/OrchardCore.ContentManagement.Display/Liquid/ConsoleLogFilter.cs#L13

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