This bundle enables PHP objects to be rendered in Twig by translating the object's full name into a Twig template and then rendering that Twig template.
In a CMS it is often necessary to associate a certain (e.g. content) object with a (view) template by default.
Imagine that you have a project with various models which you would like to display. First it is necessary to configure how the templates should be located; this is done by mapping partial namespaces to paths.
psi_object_render:
mapping:
"Acme\\MyProject\\Model": "models"
This would cause the template for a class such as Acme\\MyProject\\Model\\Post
to be located at the (Twig) path models/Post.html.twig
.
The post can be rendered as follows:
<h1>Posts</h1>
{{ psi_render_object(post) }}
You then need to create the template - the template will be passed a single object named object
:
{# path/to/templates/models/Post.html.twig #}
<h2>{{ object.title }}</h2>
<p>{{ object.body }}</p>