Themes without Twig?

Hello!

I would like give Grav a try with a personal project. However, I have my own template mechanism designed to sit on top of a CMS: I would need to use this rather than twig, while still using Grav’s administration backend and plugins. Basically, my template system provides a PHP function to render a page’s html and would in turn need a function to fetch a page’s content from the CMS.

Is something like this, circumventing Twig, possible? Does it even make sense?

Or, to put it more simply: Can I use PHP files directly for templates rather than Twig files? Would this have a negative impact on caching?

I would like to reuse my own set of templates that I already have.

I am pretty sure Twig is integral to Grav core operation at present, but tracking this in case someone can provide useful comment about how hard it would be to swap in other templating engines. I like Twig, but I have considered this question myself.

One of the reasons I didn’t get in Django about 10 years ago was that I disliked its only templating option. I think it’s good to have options, but I suspect this will be a low priority enhancement for a long time unless someone makes it their pet project.

1 Like

Hi, small question? :slight_smile: Why would you want to do it like this? It’s uncommon in the web industry to let complete html structures be rendered by php. Mostly you want to keep html/php completely loose from each other as they each have their own purposes… Sure you can do a full render from php html stuff into html, but what about custom css and stuff you’d later want to apply? Curious why you’d want to do it with this method =)

1 Like

Didn’t pay much attention to the “direct PHP” part actually. Yeah, I wonder why too now. I was thinking of other templating systems when I gave my answer.

I have my own template system based on Pug (formerly “Jade”) glued together by a small PHP kernel and GNU Make. The Pug files are compiled into HTML templates with “<?= $my_variable ?>” in appropriate places. The PHP “kernel” stiches it all together. It would be a time saver for me to reuse my modules.

Right now, for trying out Grav, I’m getting into Twig and I think it’s okay. But I have to say I really, really like Pug and I would really like to continue using it.

To be really honest, I have to admit I don’t get the point of Twig. To me it looks like PHP with a somewhat different syntax.

Okay I see why you’d want to do it like that than :slight_smile: Basicly Twig works in a very similar way, you give variables and others to the view, but twig renders it for you, instead of pure php echo stuff =) When you gain more and more content on a site, I can image your way of handling it would get very complex and hard to keep an overview, while twig remains ‘clean’ in some kind of way of keeping a structured and easy to see view. Nevertheless, I don’t think you can reuse your modules, cause Grav + twig is a magical combo … But like you said, how those elements get formed in the render part, I think it’s very easy to migrate your stuff to twig :slight_smile: as they kind of look the same …

1 Like

Well, I did give a rather short version: Building complex HTML structures was why I did it like this. My Pug templates contain no data manipulation whatsoever, not even simple for-loops. Any access to the CMS happens inside a “model” class, and any data manipulation inside a “view model” class in PHP. I know it seems like an idiosyncratic way of doing template stuff (because it probably is), but I like it because it allows for complete modularization and separation of concerns.

Anyways, thank you all for your feedback! I can see that this is not integrated into Grav. So, what I’m going to do is: I’m going to go the Twig way for a couple of small projects, get familiar with Grav and its architecture in the process, and then I’ll see further.

Awesome! That’s the spirit, twig provides super usefull stuff besides just the echo stuff, you’ll get on with it and like it more and more :stuck_out_tongue: goodluck! :slight_smile: