本文介绍了如何为带有Volt Engine的Phalcon项目的页眉和页脚创建通用模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Phalcon伏特引擎为/views中的每个页面创建一个带有页眉和页脚的通用模板

I want to create a common template with header and footer for every pages in /views using phalcon volt engine

我的文件夹层次结构在下面

my folder hierarchy is below

/views
    /user
       register.volt
    /layouts
       header.volt
       footer.volt

我想同时将header.voltfooter.volt的代码都放入register.volt页面

I want to get both code of header.volt and footer.volt into register.volt page

这是header.volt

<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container-full">
    <div class="navbar-header">
        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
        </button>
        <a class="navbar-brand">Payroll</a>
    </div>
    <div class="collapse navbar-collapse navbar-right">
        <ul class="nav navbar-nav">
            <li>item 1</li>
        </ul>
    </div><!--/.nav-collapse -->
</div>

这是footer.volt

    <div class="footer">
        <div class="container container-full">
        &copy; Custom 2014
        </div>
    </div>

这是register.volt

<div class="register-contents">
    //register form going here
</div>

推荐答案

在phalcon中设置模板的关键是设置views目录的位置. Phalcon希望您的模板和partials目录相对于该views目录.对于单级应用程序,这足够简单:

The key to setting up templates in phalcon is setting the location of your views directory. Phalcon expects your templates and partials directories to be relative to that views directory. This is simple enough for a single level application:

$view = new \Phalcon\Mvc\View();
$view->setViewsDir( realpath( __DIR__ . '/views/' ) );
$view->setLayoutsDir( '/layouts/' );
$view->setPartialsDir( '/partials/' );

当您要有一个共享模板目录并为每个模块设置单独的视图目录时,在多模块设置中会遇到麻烦.

This gets tricky in a multiple module setup when you want to have a single shared template directory and separate views directories for each module.

$view = new \Phalcon\Mvc\View();
$view->setViewsDir( realpath( __DIR__ . '/views/' ) );
$view->setLayoutsDir( '../../../common/views/layouts/' );
$view->setPartialsDir( '../../../common/views/partials/' );

在布局目录中,创建您的主模板:

In your layouts directory, create your main template:

{{ getDoctype() }}
<html>
    {{ partial('head') }}
    <body>
        {{ partial('navigation') }}
        {{ flash.output() }}
        {{ get_content() }}
        {{ partial('footer') }}
    </body>
</html>

在您的局部目录中,放置您的头部,导航和页脚文件:

In your partials directory, place your head, navigation, and footer files:

head.volt

head.volt

<head>
    {{ tag.getTitle() }}
    {{ assets.outputCss() }}
    {{ assets.outputJs() }}
</head>

navigation.php

navigation.php

<?php
// get list of navigation elements from model
$navigation = \MyNamespace\Navigation::getNavElements();
echo "<ul class='nav'>\n";
forEach( $navigation as $element ){
    printf("\t<li><a href='%s'>%s</a></li>\n",$element['url'],$element['display']);
}
echo "</ul>\n";

footer.volt

footer.volt

<div class='footer'>
    <p>&copy; {{ date('Y') }} Your Company</p>
</div>

您还可以在页面内容之前或之后插入包含html代码段的其他模板.使用beforeRender()和afterRender()挂钩控制模板目录中的哪些文件插入位置.

You can also insert additional templates that contain html snippets before or after the content of the page. Use the beforeRender() and afterRender() hooks to control which files in your templates directory get inserted where.

这篇关于如何为带有Volt Engine的Phalcon项目的页眉和页脚创建通用模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 18:49