Integrate with external non-Twig templates

Hi,

I need to create a theme that integrates and “extends” templates from an external CMS.

This CMS generates an HTML template with three placeholders:
###APP_INCLUDE_HEADER### : Placeholder for additional header content.
###APP_INCLUDE_CONTENT### : Placeholder for the “main content” of the application. ###APP_INCLUDE_FOOTER### : Placeholder for additional footer content.

I want to grab the template, replace the placeholders with Twig syntax and create and extend my new templates from there. Is it possible? How?

Please note also, the external template should be requested each time a user visits my Grav site, as the external CMS content may change at any time.

So far, I can use this PHP code:

$contents = file_get_contents("http://example.com/cms/template.html");
$contents = str_replace("###APPINCLUDEHEADER###", "{% block header %},{% endblock %}", $contents);
$contents = str_replace("###APPINCLUDECONTENT###", "{% block content %},{% endblock %}", $contents);
$contents = str_replace("###APPINCLUDEFOOTER###", "{% block footer %},{% endblock %}", $contents);
file_put_contents("base.html.twig", $contents);

And then extend the generated “base.html.twig” in my Twig template:

{% extends "base.html.twig" %}

{% block header %}
  {# Grav site's header #}
{% endblock %}

{% block content %}
  {# Grav site's body #}
{% endblock %}

{% block footer %}
  {# Grav site's footer #}
{% endblock %}
--- 
But I don't know how to link both processes together to request the external template each time a user visits my Grav site.

Should I build a plugin? Or a Twig function?
How should it look like?

This is a very specific request that i’ve not come across before. Are you sure this is the best approach? Would it make more sense to just include Grav in your external CMS rather than trying to include your external CMS in Grav?

you could create a very stripped down Grav theme that let’s you integrate very easily.

It would be nice to change the non-Grav CMS but, as I’m not the owner, I can’t.

In fact, the only way I can embed external content into this CMS is by using the HTML template they provide. So I have to find a way to dynamically load its HTML and replace its placeholders with my content.

Hopefully, I will find a way to do it with Grav.

Hi rhukster,

In another post about adding PHP on Grav you answered:

If you need to have custom PHP for some reason, Grav’s simple plugin system makes this very easy. You can do PHP ‘work’ then set the result as a Twig variable that can be output in a Twig template. Or you can create a Twig filter or function to let you do it dynamically. Or you maybe able to just use Twig’s existing functions to recreate the PHP in Twig syntax.

Can you provide any example of a “plugin that set the result as a Twig variable” and how it should be called from a Twig template?
Or is there any tutorial on how to “create a Twig filter or function” in Grav?

There isn’t a tutorial, but you can see plenty of filter examples in our Grav Twig extension: https://github.com/getgrav/grav/blob/develop/system/src/Grav/Common/Twig/TwigExtension.php

You can also see how simple it is to add your own twig extension by looking to see how it’s done in the SmartyPants plugin: https://github.com/getgrav/grav-plugin-smartypants