Question

Hello everyone and thank you for taking time to read this.

I've been using Phalcon for quite a while for a high performance JSON/XML API. The backend managing this application was/is still driven by an oudated version of symfony, but it is gonna be dropped in favour for Phalcon and the Volt Template engine.

Now my problem is the following:

Imagine a base application and a basic template and the application is modularized. Most modules are gonna be developed by different teams but they all have to integrate niceley, which from the program logic side is not a problem.

But imagine the following: You have a simple page, some forms, head, navigation, etc, etc. Now someone wants to add a module which injects a template block into the footer for whatever purpose. For example adding a TagCloud (for SEO purposes) into the footer.

The idea here is, that the plugin has way to edit any template files other than the ones it brings itself.

How can this be achieved without having to change the base templates after the initial development?

The idea is basically to hook into a event, lets call it TEMPLATE_RENDER for simplicity. TEMPLATE_RENDER is fired, every listener that is registered for it now has its chance to add stuff to the template like additional blocks etc. All without having to manually change the core templates.

It would be sufficient if there is a way to simply add a bunch of template files together in Volt and output the compiled result.


EDIT:

Okay, after some thought what I'm looking for in Volt is this:

Compiler#compileMultipleFiles(String... files);

So it can be used like this:

$compiler->compileMultipleFiles('/path/to/template1','/path/to/template2', ...);

Which would do nothing else "in theory" than take everything in file1, file2, ..., fileN and put it into one large file and then compile that as a single template. If it is not yet possible I could emulate that function by simple having each files contents combined into a single file or cache variable and use compileString() but that would break any relative paths in the template, which would be a problem.

I could also compile each template down manually, but than I would end up with an pure html document without the ability to append to blocks in the main template.

Was it helpful?

Solution

Apparently there is no such function directly.

You can however use an array and iterate over this area at the end of the primary template and dynamically include any file passed into there.

OTHER TIPS

I believe that you're looking for a Volt include. You can leave some tests in your templates like:

{% if foo.enabled %}
  {% include "foo/bar.volt" %}
{% endif %}

If you need something more complex than this you can use template inheritance also.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top