简体   繁体   English

Phalcon / Volt动态构建/渲染通用模板区域(局部)

[英]Phalcon / Volt dynamically build/render common template areas (partials)

I'm starting a project using Phalcon framework with Volt as a template engine. 我正在启动使用Phalcon框架并将Volt作为模板引擎的项目。 I have some experience with Symfony/Twig. 我对Symfony / Twig有一些经验。 I read documentation and tried searching all over the internet but can't find a satisfying way to accomplish what I want (I find ugly the solution described here: How do I create common template with header and footer for phalcon projects with Volt Engine ; it's not using Volt per se for the navigation.) 我阅读了文档并尝试在Internet上进行搜索,但找不到令人满意的方法来完成我想要的工作(我发现这里描述的解决方案很丑: 我如何使用Volt Engine为phalcon项目创建带有页眉和页脚的通用模板 ;本身不使用Volt进行导航。)

So the story is pretty easy: my base template consists of 4 parts: navigation, header, content and footer. 这样的故事非常简单:我的基本模板包括4个部分:导航,页眉,内容和页脚。 I use partials to include the common areas in the base template like the navigation, header and the footer, works fine with "static data". 我使用局部函数将基本模板中的公共区域(如导航,页眉和页脚)包括在内,可以很好地与“静态数据”配合使用。

Now the question is: how do I get to dynamically generate the navigation menu with items coming from the database? 现在的问题是:如何使用来自数据库的项动态生成导航菜单? The template will have common areas that have to come from DB also in the header, footer and a sidebar. 模板的页眉,页脚和侧边栏中将具有来自DB的公共区域。 Having to fetch that in all Controller actions sounds like overkill and not very DRY (maybe do it on the init part? but will have to be done in every controller. Maybe in an abstract controller, I dunno.) 必须在所有Controller动作中都获取该指令听起来像是过大杀伤力,并且不是很干(也许是在init上执行?但是必须在每个控制器中完成。也许在抽象控制器中,我不知道。)

What is the best way to accomplish this in Phalcon/Volt? 在Phalcon / Volt中完成此操作的最佳方法是什么? In Symfony/Twig you can call from the view a controller action, so you can have like a LayoutController that renders partials from a page. 在Symfony / Twig中,您可以从视图中调用控制器动作,因此可以像LayoutController一样呈现页面中的部分内容。

Thanks! 谢谢!

Here are few variants: 以下是几种变体:

1) Your controllers can extend a BaseController and in it's initialize() method you can assign those variables to the view. 1)您的控制器可以扩展BaseController,并可以在其initialize()方法中将这些变量分配给视图。

class BaseController extends \Phalcon\Mvc\Controller
{
    public function initialize()
    {
        // Common Variables
        $this->view->assetsSuffix = $this->config->debug ? '' : '.min';
    }

2) Create a custom Volt function which loads the data. 2)创建一个自定义的伏特函数来加载数据。

// In your Volt service:
$compiler->addFunction('getMenu', function($resolvedArgs, $exprArgs){
    return 'Helpers\CommonFunctions::getMenu(' . $resolvedArgs . ')';
})

// Helper file and function
public static function getMenu()
{
    return \Models\Menu::find();
}

// Volt usage
{% set menuItems = getMenu() %}
{% for item in menuItems %}

{% endfor %}

3) Use the models to query the DB directly from the template. 3)使用模型直接从模板中查询数据库。 However this is not yet supported with Volt (not sure if it is added in latest version, have to confirm). 但是,Volt尚不支持此功能(不确定是否已添加最新版本,必须进行确认)。

<?php $menuItems = \Models\Menu::find(); ?>
{% for item in menuItems %}

{% endfor %}

4) Ajax/Javascript, but this is really dependant on your application. 4) Ajax / Javascript,但这实际上取决于您的应用程序。 Something like Angular approach, but I will not get into details with this varaiant. 类似于Angular方法,但我不会详细介绍这种变化。

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

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