简体   繁体   English

如何为带有Volt Engine的Phalcon项目的页眉和页脚创建通用模板

[英]How do I create common template with header and footer for phalcon projects with Volt Engine

I want to create a common template with header and footer for every pages in /views using phalcon volt engine 我想使用Phalcon volt引擎为/views每个页面创建一个带有页眉和页脚的通用模板

my folder hierarchy is below 我的文件夹层次结构如下

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

I want to get both code of header.volt and footer.volt into register.volt page 我想将header.voltfooter.volt代码footer.volt放入register.volt页面

this is the code in header.volt 这是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>

this is the code in footer.volt 这是footer.volt的代码

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

this is the code in register.volt 这是register.volt的代码

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

The key to setting up templates in phalcon is setting the location of your views directory. 在phalcon中设置模板的关键是设置视图目录的位置。 Phalcon expects your templates and partials directories to be relative to that views directory. Phalcon希望您的模板和partials目录相对于该views目录。 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: 在您的partials目录中,放置您的head,navigation和footer文件:

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>

You can also insert additional templates that contain html snippets before or after the content of the page. 您还可以在页面内容之前或之后插入包含html代码段的其他模板。 Use the beforeRender() and afterRender() hooks to control which files in your templates directory get inserted where. 使用beforeRender()和afterRender()挂钩控制模板目录中的哪些文件插入到哪里。

I highly recommend you reading the documentation on phalcon available here: http://docs.phalconphp.com/en/latest/reference/views.html 我强烈建议您阅读以下有关phalcon的文档: http ://docs.phalconphp.com/en/latest/reference/views.html

Nevertheless, your folder structure should look something similar to this: 但是,您的文件夹结构应类似于以下内容:

/views
    index.volt
    /layouts
        register.volt
    /register
        index.volt

Where views/index.volt is the main layout for your site. 其中views / index.volt是您网站的主要布局。 This should include the header and footer. 这应该包括页眉和页脚。

Layouts folder is the layout folder for your controllers. 布局文件夹是您的控制器的布局文件夹。 So if you have the let's say loginController then it will search for login.volt inside layouts folder. 因此,如果您有loginController,那么它将在布局文件夹中搜索login.volt。

Next level of inheritance is the action view. 继承的下一级是操作视图。 So, after the controller's layout is called the view controller is called. 因此,在调用控制器的布局之后,将调用视图控制器。 In your case index.volt if you're controller is named RegisterController and your view is indexAction. 在本例中,如果您是控制器,则index.volt名为RegisterController,而您的视图为indexAction。

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

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