Full-featured content?

Some questions about the flexibility of Grav.
Some pages need to be very specific, a first step to do this is to bind
them to a specific template:

  • is there a way to bind a given content to a given template, and
    better, to maintain the page and the template in the same

  • how (if?) could I insert custom javascript, CSS, HTML, (xml?) in a
    during rendering of a specific page?
    Not only it relates to being able to get a specific template for a
    given page, but the way the template permits "inclusion/transclusion"
    of content’s additional resources.

  • same question about inserting PHP for a specific page (or defining
    Twig-usable PHP functions)? [yes that means un-cached page]

While the uses of the above capabilities are surely "plugin-specifics"
I’d be interested about the design decisions/limitations which would make
the above powerful/flexible/dangerous features impossible out of the


  1. you can’t maintain the page (content) and theme twig (template) in the same directory. The .md page file must be in it’s own distinct location (user/pages/ by default) as this is iterated over and cached aggressively. The theme’s twig templates must be in another directory so Twig can search for and find the appropriate template file. You can get a good idea of all the supported template files though by looking in user/themes/your_theme/templates folder however.

  2. You can dynamically add specific JS or CSS to a specific page via the assets plugin: https://github.com/getgrav/grav-plugin-assets. Alternatively you can use the twig template and reference JS or CSS for all pages of that template type.

  3. You cannot insert PHP, you can however use however write Twig. It’s quite a full featured template language and most any logic you can do with Twig. If you need more logic, the approach is to write a plugin to provide you with the custom data you need and then use Twig to manipulate it, or even write a custom Twig filter/function and make that available to use. We have yet to find a situation where either one of these approaches has been unable to provide a solution to any missing functionality.