Skip to content
Grav 2.0 is officially stable. Read the announcement →
Archive

Integrate with external non-Twig templates

Started by Muut Archive 11 years ago · 5 replies · 993 views
11 years ago

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.

11 years ago

So far, I can use this PHP code:

TWIG
$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:

TWIG

{% 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?
11 years ago

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.

11 years ago

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.

11 years ago

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?

Suggested topics

Topic Participants Replies Views Activity
Archive · by Deleted User, 9 years ago
0 1346 9 years ago
Archive · by Muut Archive, 9 years ago
2 933 9 years ago
Archive · by Muut Archive, 9 years ago
2 4060 9 years ago
Archive · by Muut Archive, 9 years ago
1 2945 9 years ago
Archive · by Muut Archive, 9 years ago
3 1117 9 years ago