sebastienros/fluid

Access root object from template

ssougnez opened this issue · 3 comments

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 :-)

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.

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.

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