首页 文章

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

提问于
浏览
2

我正在尝试将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标记,它会在网站加载时发生变化 . 在我的模板中,我已经包含了<

<?= $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 回答

  • 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 for register中,您只需执行以下操作:

    $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)中的文件,其中包含我的布局(如上所示) .

  • 3

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

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

    在你的HTML之上:

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

    然后在你的HTML中:

    <img src='<? echo $logosrc;?>' />
    
  • 0
    • 使用 uri_segment 来控制动态内容 .
      在你的 main.php
    <?php
    //...
    $template['registerForm'] = '';
    if($this->uri->segment(1) === false) {
        $template['registerForm'] = $this->load->view('form/registration');
    }
    //...
    ?>
    
    • 单击时使用JavaScript加载新页面 . 在 main.php 中创建一个简单的函数,只是为了加载该表单模板并通过ajax显示该表单 .

相关问题