将动态内容添加到代码点火器中的模板

发布于 2024-10-27 16:59:05 字数 4473 浏览 1 评论 0原文

我正在尝试将 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。我一直在阅读代码点火器中的模板用户指南,但我无法理解如何使其动态化。请帮助我解决这个问题。

这是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;

提前致谢。

I am trying a HTML/CSS file to convert to a template. I asked for help but I didn't receive any suggestions in that post yet I tried to figure out a way to create a template. I am having few glitches in doing that. My controller has main.php file which is the main page of the website, and I have register where the header, footer, and rest remain same except that main content changes when the page loads.

I have the following template in the views folder:

<!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>

In the

div id = maincontent

This is the div tag which would be changing when a site loads. In my template I have included <

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

The flashcontent and resources are supposed to be in the main page and registerForm is supposed to be shown when I click on register in the main page. I have been reading the template user guide in the code igniter but I am unable to understand how to make this dynamic. Please help me in getting over this issue.

This is the template.php in config folder.

$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;

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

花桑 2024-11-03 16:59:05

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

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

在 html 上方:

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

然后在 html 中:

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

Are you using a CMS or a custom database? Where are the dynamic elements coming from?

The basics for making a dynamic element are as follows:

Above your html:

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

Then in your html:

<img src='<? echo $logosrc;?>' />
丿*梦醉红颜 2024-11-03 16:59:05

您似乎正在使用Colin Williams 模板库

我没有看过太多,但请阅读@ 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 的视图,其中仅包含表单。

在用于注册的控制器方法中,您只需执行以下操作:

$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) 中的一个文件,其中包含我的布局(如多于)。

Looks like you're working with Colin Williams Template library.

I haven't looked at it too much but take a read @ http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html#manipulation

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

The add_region() method allows one to dynamically add regions for
writing. At a minimum, a $name must be
supplied to identify the region.
Optionally, a $settings array can be
passed in to provide default content,
wrappers and attributes.

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

Edit

(Again, assuming you are using the template library I think you are),

You have a region content in your template. You have a view called registerForm which contains just the form.

In your controller method for register, you simply have to do:

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

The library will load the content of the view registerForm in to the content region.

You can have as many regions as you like, obviously, but the template library allows you to define an area (a region) that you want any content to appear, and then outputs your HTML or views directly in to the region.

Either I misunderstand but I don't think I can explain it much simpler than this. The docs are rather good as well.

Edit the second

Here's a sample method from a project I did a while back using this library.

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();
    }

now when I access mysite.com/welcome/index my HTML template will be displayed with the content I wrote in the method.

My HTML template looks a little like:

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

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

My config/template.php file looks like this:

$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 refers to a file in my views folder (template/master.php) which contains my layout (as above).

苏璃陌 2024-11-03 16:59:05
  1. 使用 uri_segment 控制您的动态内容。
    在您的 main.php 中:

    <前><代码>uri->segment(1) === false) {
    $template['registerForm'] = $this->load->view('表单/注册');
    }
    //...
    ?>

  2. 单击时使用 JavaScript 加载新页面。在 main.php 中创建一个简单的函数,仅加载该表单模板并通过 ajax 显示该表单。

  1. Use uri_segment to control your dynamic content.
    In your main.php:

    <?php
    //...
    $template['registerForm'] = '';
    if($this->uri->segment(1) === false) {
        $template['registerForm'] = $this->load->view('form/registration');
    }
    //...
    ?>
    
  2. Use JavaScript to load new page when clicked. Make a simple function in main.php just to load that form template and show that form via ajax.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文