We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Extending using a dynamic value

So I am building an application that will be required to have multiple 'Base templates' which will be dynamically determined using the hostname. The page templates themselves will have the same content, it's just the container that will be different.

Rather than heading down the messier road of building in partials() or if statements within the base template itself, is there any way to dynamically reference a value using extends?

{% extends "../../data/company/" ~ "example" ~ "/templates/base.volt" %}

Returns an exception 'PhalconException: Syntax error, unexpected token ~ in \views\company/index.volt on line 1'

Attempting to use a variable like

{% extends baseName %}

Returns 'PhalconException: Syntax error, unexpected token IDENTIFIER(baseName) in \views\company/index.volt on line 1'

I did at one point try and use the 'setMainView' method against the view service in my bootstrap file like

$view->setMainView(sprintf("../../data/company/%s/templates/base", $variableName); 

and removing the 'extends' within my template(s), but then the template blocks themselves aren't processed, and everything renders from the content() function.

I'm 99% sure the functionality just is not there yet, is the volt project itself open source like Phalcon? I couldn't seem to find the repository.



98.9k
Accepted
answer

Volt does not support dynamic extending. Volt compiles templates to static PHP files, implementing this feature requires compile the templates in every request depending on the extended path which surely would impact performance.

I'm 99% sure the functionality just is not there yet, is the volt project itself open source like Phalcon? I couldn't seem to find the repository.

Here is the repository: https://github.com/phalcon/cphalcon/tree/master/ext/mvc/view/engine/volt



1.3k
edited Jun '15

when I try to extend a view on module from core, and the core also extend on her own path, Phalcon still use module relative path

sample:

  • app/
  • app/views/
  • app/views/template.volt extend core.colt
  • app/views/core.volt
  • app/modules/
  • app/modules/ModuleName/
  • app/modules/ModuleName/view/
  • app/modules/ModuleName/view/content.volt extend app/views/template.volt

when I call app/module/moduleName/views/content.volt, the template.volt cannot extend core.volt, instead of ../../../view/core.volt