简体   繁体   English

在Phalcon Volt中继承动态模板

[英]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. 我需要加载一个页面,该页面将被“插入”到模板中-在我阅读该页面时,Volt的Template Inheritance应该可以解决问题,并且确实可以。 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 注册另一个函数以生成完整的字符串(例如{%扩展“ ../../templates/de/index.volt”%}),然后对其进行编译,例如

    $compiler->addFunction('get_template', function ($resolvedArgs, $exprArgs) use ($volt) { return $volt->getCompiler() ->compileString('{% extends "../../templates/de/index.volt" %}'); }); $ compiler-> addFunction('get_template',函数($ resolvedArgs,$ exprArgs)使用($ volt){返回$ volt-> getCompiler()-> compileString('{%扩展“ ../../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). 但是,使用该方法不会解析页面内容(例如,未显示已注册的content()函数返回的内容)。 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. 我也愿意接受其他解决方案(对于性能问题,使用Twig代替Volt只是万不得已),关于我做错事的建议或有关该主题的有用文章的指南。 Thanks in advance! 提前致谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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