概述 快速入门 教程 手册 最佳实践 组件 参考 贡献

发布于 2015-08-27 16:55:22 | 131 次阅读 | 评论: 0 | 来源: 网络整理

More often than not, templates in a project share common elements, like the well-known header and footer. Using this helper, the static HTML code can be placed in a layout file along with “slots”, which represent the dynamic parts that will change on a page-by-page basis. These slots are then filled in by different children template. In other words, the layout file decorates the child template.

Displaying Slots

The slots are accessible by using the slots helper ($view['slots']). Use output() to display the content of the slot on that place:

<!-- views/layout.php -->
<!doctype html>
<html>
    <head>
        <title>
            <?php $view['slots']->output('title', 'Default title') ?>
        </title>
    </head>
    <body>
        <?php $view['slots']->output('_content') ?>
    </body>
</html>

The first argument of the method is the name of the slot. The method has an optional second argument, which is the default value to use if the slot is not available.

The _content slot is a special slot set by the PhpEngine. It contains the content of the subtemplate.

警告

If you’re using the standalone component, make sure you registered the SlotsHelper:

use SymfonyComponentTemplatingHelperSlotsHelper;

// ...
$templateEngine->set(new SlotsHelper());

Extending Templates

The extend() method is called in the sub-template to set its parent template. Then $view['slots']->set() can be used to set the content of a slot. All content which is not explicitly set in a slot is in the _content slot.

<!-- views/page.php -->
<?php $view->extend('layout.php') ?>

<?php $view['slots']->set('title', $page->title) ?>

<h1>
    <?php echo $page->title ?>
</h1>
<p>
    <?php echo $page->body ?>
</p>

注解

Multiple levels of inheritance is possible: a layout can extend another layout.

For large slots, there is also an extended syntax:

<?php $view['slots']->start('title') ?>
    Some large amount of HTML
<?php $view['slots']->stop() ?>
最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务