【问题标题】:Add dynamic content to a template in code igniter在 codeigniter 中向模板添加动态内容
【发布时间】:2011-03-29 02:54:03
【问题描述】:

我正在尝试将 HTML/CSS 文件转换为模板。我寻求帮助,但在那篇文章中没有收到任何建议,但我试图找出一种创建模板的方法。我在这样做时遇到了一些小故障。我的控制器有 main.php 文件,它是网站的主页,我已经注册了页眉、页脚和其余部分保持不变的位置,只是页面加载时主要内容发生了变化。

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

<!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 = 主要内容

这是在网站加载时会发生变化的 div 标签。在我的模板中,我包含了

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

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

这是配置文件夹中的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;

提前致谢。

【问题讨论】:

    标签: php model-view-controller codeigniter


    【解决方案1】:

    您使用的是 CMS 还是自定义数据库?动态元素从何而来?

    制作动态元素的基本原理如下:

    在您的 html 上方:

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

    然后在你的html中:

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

    【讨论】:

    • 我正在使用自定义数据库。在控制器文件夹中,我有 main.php 和 register.php,我正在加载 flashcontent 和资源。我必须在 regiter.php 而不是 flash 内容中加载注册表单,但是我拥有所有三个 flash 内容、资源和表单。徽标图像来自 CSS,即我在 CSS 中使用了背景图像,但无法检索
    • @Ross 你是对的。图像文件夹的路径已损坏。现在它工作正常。
    【解决方案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-&gt;template-&gt;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) 中的一个文件,其中包含我的布局(如上)。

    【讨论】:

    • 我关心的问题是,我应该在 template.php 中回显 $maincontent。我应该删除它还是应该这样保留它。当我为寄存器使用相同的模板时,我不需要模板中的 flashcontent 和资源。我只需要回显 registerForm 即可。有了这个 add_region, set_template 我不确定计算机如何知道我必须将我的 registerForm 准确地放在 maincontainer div 标签内
    • 你还告诉 CI 写入哪个区域,因此它知道将 registerForm 放在 $mainContent 所在的位置,例如。
    • 谢谢。我明白你解释的。我主要担心的是,当我加载应该只显示 registerForm 的寄存器视图时。它还显示了 registerForm 视图上方的 flashcontent 和资源。这就是我感到困惑的地方。有没有办法销毁flash内容和资源。
    • 用户指南指出不建议在模板配置中包含标记。只需定义区域,如果您需要内容,请写信给他们。否则无论如何你都会有内容。我认为您不能动态删除它们
    【解决方案3】:
    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 显示该表单。

    【讨论】:

    • 你能详细解释一下吗?我是codeigniter的新手。如果您能给我提供一个链接,我会很高兴为我提供解决此问题的深入方法
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-15
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多