简体   繁体   中英

Inherit dynamic template in Phalcon Volt

I need to load a page, that will be "inserted" in a template - as I read it, Volt's Template Inheritance should do the trick and it does... kinda. Hardcoded values, as shown in the examples, work fine - the following example works:

<!-- Template -->
<div id="site_content">     
{% block test %}
{% endblock %}
</div>

and the page, that inherits the template:

{% extends "../../templates/de/index.volt" %}

{% block test %}
    {{ content() }} {# this is a registered volt function that outputs the generated content #}
{% endblock %}

However, the same page might need to inherit a different template and that must be decided on runtime, so the name of the template must be generated dynamically. Two options occurred to me:

  • Set the template name to a variable and use it when extending - the problem here is that I don't see a way to use it afterwards. That guy seems to have had the same problem , but there is neither an answer of how to do it, nor a confirmation that it isn't possible at all.
  • Register another function to generate the complete string (eg {% extends "../../templates/de/index.volt" %}) and then compile it, eg

    $compiler->addFunction('get_template', function ($resolvedArgs, $exprArgs) use ($volt) { return $volt->getCompiler() ->compileString('{% extends "../../templates/de/index.volt" %}'); });

and then use that function in the page, eg

{{ get_template() }}

{% block test %}
    {{ content() }}
{% endblock %}

However, using that approach does not parse the page content (eg the content returned by the registered content() function is not shown). I'm also open to other solutions (using Twig instead of Volt is only a last resort, for performance issues), advices of what I'm doing wrong or pointers of useful articles on the topic. Thanks in advance!

尝试按照Phalcon文档中的说明使用部分: 使用部分

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM