如何将HTML模板转换为Cake PHP模板

时间:2011-07-21 13:16:38

标签: php html cakephp

我是蛋糕php的初学者。我需要将我的HTML模板转换为Cake PHP模板。任何想法?

2 个答案:

答案 0 :(得分:9)

这很容易。

  1. 将模板主文件扩展名替换为.ctp
  2. index.htmlindex.ctp
  3. 查看您的文件布局(html代码)并确定您希望在所有页面上显示该模板的哪个部分;
  4. 通常大多数模板的设置如下:

    <html>
    <head>
        <title>My Site</title>
        // You will include your javascript and css files here
        <?php
            echo $this->Html->css(array('cake.generic','default'));
            echo $this->Html->script(array('myscript','jquery'));
        ?>
    </head>
    <body>
        <div id="container">
            <div id="header"></div>
            <div id="body">
                <div id="main_content" style="width:600px;float:left">
                       <?php 
                            //Code for this should be in your home.ctp
                            // in your pages folder. Usually I cut this content from
                            // my template and place the whole thing in that file
                            // everything else happens magically
                            echo $content_for_layout; 
                       ?>
                </div>
                <div id="side_content" style="width:300px;float:left">
                     <!-- You can have content here manually or create elements and include them here like the following -->
                     <?php $this->element("sidebar_content"); ?>
                </div>
            </div>
            <div id="footer">...</div>
        </div>
     </body>
    

  5. 然后,您应该将所有图片上传到/img文件夹中的/app/webroot文件夹

  6. 将图片路径更改为引用/img文件夹。

  7. 您必须对所有CSS和JS文件执行相同操作。将它们放在/app/webroot位置的相应文件夹中。

  8. 祝你好运!

答案 1 :(得分:1)

将模板保存到APP/views/layouts/template.ctp

确保它至少有两个变量:

<title><?php echo $title_for_layout; ?></title>

<body>
    <?php echo $content_for_layout; ?>
</body>

启动视图或控制器并添加 $this->layout = 'template'; // view/method$layout = 'template'; // controller;

查看蛋糕default.ctp了解相关信息。