在代码点火器中向模板添加动态内容

时间:2011-03-29 02:54:04

标签: php model-view-controller codeigniter

我正在尝试将HTML / CSS文件转换为模板。我请求帮助,但我没有在该帖子中收到任何建议,但我试图想出一种创建模板的方法。我这样做有一些小问题。我的控制器有main.php文件,它是网站的主页面,我已经注册了页眉,页脚和其余部分保持不变,除了主页内容在页面加载时发生变化。

我在views文件夹中有以下模板:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
   <title><?= $title ?></title>

   <style type="text/css"  >
       @import url('<?=base_url()?>/includes/front.css');
       @import url('<?=base_url()?>/images/');
   </style>

</head>
<body>
   <div id="container">
       <div id="header">
            <?= $logo ?> <!--This is an image tag to display the image on the template -->
            <?= $topnav ?>
       </div>

       <div id="menubar">
            <?= $menulist ?>
       </div>

       <div id="maincontent">
           <?= $flashcontent ?>
           <?= $resources ?>
           <?= $registerForm ?>
       </div>

       <div id="footer">
           <div>
               <?= $footertable1 ?>
           </div>
       </div>
   </div>
</body>
</html>

  

div id = maincontent

这是div标记,它会在网站加载时发生变化。在我的模板中,我已经包含了&lt;

<?= $flashcontent ?> <?= $resource ?> and  <?= registerForm ?>

flashcontent和资源应该在主页面中,当我点击主页面中的register时,应该显示registerForm。我一直在阅读代码点火器中的模板用户指南,但我无法理解如何使其动态化。请帮我解决这个问题。

这是config文件夹中的template.php。

$template['active_group'] = 'default';
$template['default']['template'] = 'template.php';
$template['default']['regions'] = array(
   'title',
   'logo' => array(
        'content' => array(''),
        'name' => 'Logo',
        'wrapper' => '<img>',
        'attributes' => array('id' => 'logo')
    ),
    'topnav' => array(
        'content' => array(),
        'name' => 'Topnav',
        'wrapper' => '<div>',
        'attributes' => array('id' => 'topnav', 'class' => 'topnav')
    ),
    'menulist' => array(
        'content' => array('<li>
                            <a href="#"> Home </a>
                            <a href="#"> Generator </a>
                            <a href="#"> News </a>
                            <a href="#"> Forum </a>
                            <a href="#"> About </a>
                            <a href="#"> Feedback </a>
                            </li>'),
        'name' => 'Menulist',
        'wrapper' => '<ul>',
        'attributes' => array('class' => 'menulist')
    ),
   'flashcontent' => array(
       'content' => array(''),
       'name' => 'Flashcontent',
       'wrapper' => '<div>',
       'attributes' => array('id' => 'flashcontent')
   ),
    'resources' => array(
       'content' => array(''),
       'name' => 'Resources',
       'wrapper' => '<div>',
       'attributes' => array('id' => 'resources')
   ),
    'registerForm' => array(
      'content' => array(),
        'name' => 'Registerform',
        'wrapper' => '<div>',
        'attributes' => array('id' => 'registerForm')
    ),
   'footertable1' => array(
       'content' => array('<ul> 
                            <li> <a href="../main">Home</a>
                            <a href="#">News & Events</a>
                            <a href="#">Forum</a>
                            <a href="#">About us</a>
                            <a href="#">Contact us</a>
                            <a href="#">Terms of use</a>
                            <a href="#">Privacy policy</a>
                            <a href="#">Site map</a>
                            </li>
                           </ul>'),
       'name' => '',
       'wrapper' => '<div>',
       'attributes' => array('id' => 'footertable1')
   )
);
$template['default']['parser'] = 'parser';
$template['default']['parser_method'] = 'parse';
$template['default']['parse_template'] = FALSE;

提前致谢。

3 个答案:

答案 0 :(得分:3)

您使用的是CMS还是自定义数据库?动态元素来自哪里?

制作动态元素的基础知识如下:

高于你的HTML:

<?php
$logosrc="mylogo.gif";
?>

然后在你的html:

<img src='<? echo $logosrc;?>' />

答案 1 :(得分:2)

看起来您正在使用Colin Williams Template library

我没有太多关注,但请阅读@ http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html#manipulation

$this->template->add_region($name, $settings = array())
     

add_region()方法允许动态添加区域   写作。至少必须是$ name   用于识别该地区。   可选地,$ settings数组可以是   传入以提供默认内容,   包装和属性。

$this->template->add_region('flash');
$this->template->write_view('flash', 'some_flash');    
$this->template->add_region('login');
$this->template->write_view('login', 'some_login');

修改

(再次,假设您正在使用我认为您的模板库),

您的模板中有一个地区content。您有一个名为registerForm的视图,其中只包含表单。

controller method注册时,您只需执行以下操作:

$this->template->write_view('content', 'registerForm');

图书馆会将观看内容registerForm加载到content区域。

显然,您可以拥有任意数量的区域,但模板库允许您定义希望显示任何内容的区域(区域),然后将HTML或视图直接输出到该区域。

要么我误解了,但我认为我不能解释它比这简单得多。文档也相当不错。

编辑第二个

这是我使用这个库做了一个项目的示例方法。

class Welcome extends Controller {

    function __construct()
    {
        parent::__construct();
        $this->template->set_template('my_template'); 
        // as defined in config/template.php    
    }

    function index()
    {
        $this->template->write('title', 'This is my $title region');

            // here I am writing some views to my regions $content and $sidebar
        // content/home-page-view is just a view containing text.

        $this->template->write_view('content', 'content/home-page-view');
        $this->template->write_view('sidebar', 'sidebar/home-page-sidebar');

        // lets go..
        $this->template->render();
    }

现在当我访问mysite.com/welcome/index时,我的HTML模板将显示我在方法中写的内容。

我的HTML模板看起来有点像:

<html>
<head>
<title><?php echo $title; ?></title>
</head>
<body>
<?php echo $content; ?>

<?php echo $sidebar; ?>
</body>
</html>

我的config / template.php文件如下所示:

$template['default']['template'] = 'template/master';
$template['default']['regions'] = array(
    'title',
    'content',
    'sidebar'
    );
$template['default']['parser'] = 'parser';
$template['default']['parser_method'] = 'parse';
$template['default']['parse_template'] = FALSE;

template/master指的是我的视图文件夹(template / master.php)中的一个文件,其中包含我的布局(如上所述)。

答案 2 :(得分:0)

  1. 使用uri_segment来控制您的动态内容 在main.php

    <?php
    //...
    $template['registerForm'] = '';
    if($this->uri->segment(1) === false) {
        $template['registerForm'] = $this->load->view('form/registration');
    }
    //...
    ?>
    
  2. 点击时使用JavaScript加载新页面。在main.php中创建一个简单的函数,只需加载该表单模板并通过ajax显示该表单。