简体   繁体   English

Symfony PHP模板

[英]Symfony php templating

I need some guidance in Symfony templating. 我需要一些有关Symfony模板的指导。 I just want to set default template for all views in one controller. 我只想为一个控制器中的所有视图设置默认模板。 Instead of extending the template in every view page. 而不是在每个视图页面中扩展模板。 Please check the code how I am using right now. 请检查代码我现在如何使用。

//Controller
class HomeController extends Controller 
{
    public static $title="Customised Title";
    public function indexAction()
    {

          return $this->render('homeBundle:Home:index.html.php',array('name'=>'test','title'=>self::$title));
    }

    public function aboutAction()
    {
          //$this->view->extend('homeBundle:Templates:default.html.php');
          return $this->render('homeBundle:Home:about.html.php',array('name'=>'test','title'=>self::$title));
    }

}

Template 模板

views/Templates/default.html.php views / Templates / default.html.php

 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title><?php $view['slots']->output('title', isset($title)?$title:"Symfony Default Title") ?></title>
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <meta name="description" content="">
        <meta name="author" content="">
         <link href="<?php echo $view['assets']->getUrl('bundles/home/css/style.css') ?>" rel="stylesheet">
         <link href="<?php echo $view['assets']->getUrl('bundles/home/css/bootstrap-responsive.css') ?>" rel="stylesheet">
    </head>

    <body>
    <?php $view['slots']->output('_content') ?></body>
    </html>

Views /views/Home/about.html.php 查看/views/Home/about.html.php

<?php $view->extend('homeBundle:Templates:default.html.php') ?>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sagittis vulputate urna.

/views/Home/index.html.php /views/Home/index.html.php

<?php $view->extend('homeBundle:Templates:default.html.php') ?>
<?php
echo "hello ".$name;
?>

In the above views code we are extending homeBundle:Templates:default.html.php for each view. 在上面的视图代码中,我们为每个视图扩展了homeBundle:Templates:default.html.php。 Is it possible to have it in controller(May be in constructor)? 是否可以在控制器中使用它(可能在构造函数中)?

Thank you Vbee for your help. 感谢Vbee的帮助。 Actually I am expecting something like view should not extend any more. 实际上,我希望类似视图的内容不再扩展。 index.html.php should have only it's content. index.html.php应该只有它的内容。 Like 喜欢

<?php
        echo "hello ".$name;
?>

Instead of creating render method we can use static varialbe to achive same like you did. 除了创建渲染方法,我们还可以使用静态varialbe像您一样达到相同的效果。

class HomeController extends Controller 
{
    public static $title="Customised Title";
    public static $template="homeBundle:Templates:default.html.php";

    public function testAction()
    {

        return $this->render('homeBundle:Home:about.html.php',array('extend_view'=>self::$template,'name'=>'test','title'=>self::$title));
      }
}

But I want to render template completely at controller side and in view should have only action view content. 但是我想在控制器端完全渲染模板,并且在视图中应该只有动作视图内容。

Create your own render method in your controller: 在控制器中创建自己的渲染方法:

<?php
//Controller
class HomeController extends Controller 
{
    public static $title="Customised Title";

    public function testAction()
    {
        return $this->renderWithExtend('homeBundle:Home:about.html.php', array('name' => 'test', 'title' => self::$title));
    }

    public function renderWithExtend($route, $params)
    {
        return $this->render($route, array_merge(array('extend_view', 'homeBundle:Templates:default.html.php'), $params));
    }
}

Then in you view: 然后在您看来:

// index.html.php
<?php $view->extend($extend_view) ?>
<?php
    echo "hello ".$name;
?>

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

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