【问题标题】:Codeigniter + Smarty integration, header/footer templatingCodeigniter + Smarty 集成,页眉/页脚模板
【发布时间】:2013-07-29 12:25:44
【问题描述】:

我通过本教程在我的 CI 安装中实现了 smarty:http://www.coolphptools.com/codeigniter-smarty

它工作正常,除了页眉和页脚是通过包含模板中的文件来加载的。

即。

{include file="header.tpl" title="Example Smarty Page" name="$Name"}
...
{include file="footer.tpl"}

有没有办法从控制器或 Smarty 库类中加载它们?

为了更清楚地说明我想要什么;如果没有模板引擎,我只会通过自定义加载器扩展 view 方法。

例如。

class MY_Loader extends CI_Loader {

    function view( $template, $data = array(), $return = false ) {

        $content = parent::view( 'header', $data, $return );
        $content .= parent::view( $template, $data, $return );
        $content .= parent::view( 'footer', $data, $return );

        if( $content )
            return $content;
    }
}

这一直对我有用,但现在我正在尝试 smarty,我一生都无法弄清楚如何让它像这样工作。

如果有人能指出我正确的方向。那简直太好了。

PS。抱歉,如果之前已经回答过这个问题,我在过去的 2 个小时里一直在谷歌搜索,但我似乎找不到任何东西。我的 PHP 技能充其量只是中等水平。

【问题讨论】:

    标签: php codeigniter codeigniter-2 smarty3


    【解决方案1】:

    我不是专家,但不久前我确实必须经历这个。 我所做的是这样的:

    header.tpl

    <html>
        <head>
        </head>
        <body>
    

    内容.tpl

    {include file="header.tpl"}
    {include file="$content"}
    {include file="footer.tpl"}
    

    页脚.tpl

        </body>
    </html>
    

    然后你可以只用 smarty 呼叫 content.tpl 并通过 $content 传递实际的正文内容。

    不过,就像我说的那样,我不是专家,因此语法可能不正确,并且可能有更“正确”的方式来执行此操作,但我认为这个想法就在那里。

    希望对您有所帮助。

    【讨论】:

      【解决方案2】:

      您应该使用{extends} 语法来利用template inheritance

      从您的基本模板文件开始(我们称之为template.php)。该文件将包含您的页眉 页脚代码,并将指定主要内容的位置(以及您想要指定的任何其他模板代码块,例如边栏)。

      (我在这里使用的是非常简单的 HTML,仅用于示例目的。请不要这样编码。)

      template.php:

      <html>
      <head>
          <title>{$title|default:'Default Page Title'}</title>
          <meta>
          <meta>
      </head>
      <body>
      
          <header>
              <h1>My Website</h1>
      
              <nav>
                  <!-- Navigation -->
              </nav>
          </header>
      
          <section class="content">
      
              {block name="content"}{/block}
      
          </section>
      
          <footer>
              &copy; 2013 My Website
          </footer>
      
      </body>
      

      然后,您的各个页面将{extend} 主模板文件,并为所有块指定必要的内容。请注意,您可以将块设为可选并使用默认值,因此不需要填充所有块(除非您以这种方式编写代码)。

      content_page.php:

      {extends file="template.php"}
      
      {block name="content"}
      
      <h2>Content Page</h2>
      
      <p>Some text.</p>
      
      {/block}
      

      【讨论】:

        【解决方案3】:
        you can user following way to include smarty tpl view in your controller:
        $this->smarty->view('content.tpl')
        
        and header.tpl in content page:
        {include file='header.tpl'} 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-08-28
          • 1970-01-01
          • 2014-09-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多