Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
CodeIgniter-Smarty-如何从CodeIgniter中的一个位置将模板提取到主模板中_Codeigniter_Smarty_Fetch - Fatal编程技术网

CodeIgniter-Smarty-如何从CodeIgniter中的一个位置将模板提取到主模板中

CodeIgniter-Smarty-如何从CodeIgniter中的一个位置将模板提取到主模板中,codeigniter,smarty,fetch,Codeigniter,Smarty,Fetch,要解释我的问题是关于什么有点困难,但我会尝试:) 在我使用Smarty的自定义项目中,我有一个index.php文件,其中放置了url处理和内容的所有基本设置 例如: // Get information for the "about us" page if($_GET['qs']=='about_us'){ $data['text'] = 'Lorem Ipsum...'; $data['other_stuff'] = 'dolor sit amet'; // Required

要解释我的问题是关于什么有点困难,但我会尝试:)

在我使用Smarty的自定义项目中,我有一个index.php文件,其中放置了url处理和内容的所有基本设置

例如:

// Get information for the "about us" page

if($_GET['qs']=='about_us'){
  $data['text'] = 'Lorem Ipsum...';
  $data['other_stuff'] = 'dolor sit amet';

  // Required template file
  $data['tpl_Name'] = 'about.tpl';
}
if (isset($data['tpl_Name'])){
  $this->smarty->assign('content', $this->smarty->fetch($data['tpl_Name'])); 
  $this->smarty->view('master_site.tpl');
}
在这个文件的底部,我有一段代码,用于获取正确的模板,并将其发送到master_站点模板

例如:

// Get information for the "about us" page

if($_GET['qs']=='about_us'){
  $data['text'] = 'Lorem Ipsum...';
  $data['other_stuff'] = 'dolor sit amet';

  // Required template file
  $data['tpl_Name'] = 'about.tpl';
}
if (isset($data['tpl_Name'])){
  $this->smarty->assign('content', $this->smarty->fetch($data['tpl_Name'])); 
  $this->smarty->view('master_site.tpl');
}
在master_site.tpl中,我可以使用{$content}访问about.tpl 对于每个页面,{$content}变量都会更改,因为index.php是通过在我的应用程序中请求页面将所有数据发送到的页面

问题:CodeIgniter中是否有一个文件可以在每个页面上使用,这样我就可以创建类似上面的内容?现在我必须在控制器的每个功能中使用这段代码(第二个示例),这似乎效率低下

当然,我必须在每个函数中设置$data['tpl_Name'],但这没有问题


谢谢。

有几种方法可以将模板库集成到CodeIgniter中,其中很多方法取决于您希望如何将模板绑定在一起。以下内容可能很适合你,也可能只是为你指明了正确的方向。它不是唯一的Smarty/CI模板解决方案,但也是我熟悉的解决方案

。它允许您使用加载视图的标准方式--
$this->load->view('view',$data)
--但您可以在文件中使用Smarty的语法

我处理“主”模板的方式是通过Smarty的
{extends}
()。我加载的各个视图都扩展了一个主模板,或者扩展了指向一个主模板的其他层次结构

main\u template.php:

<html>
<head>
    <meta>
    <meta>
    <title>{$title|default:"Default Title"}</title>
</head>
<body>

<header>
    <h1>My Website</h1>
</header>

<nav>
    <ul>
        <li>Page Link</li>
        <li>Page Link</li>
        <li>Page Link</li>
        <li>Page Link</li>
    </ul>
</nav>

<section class="main-content">

{block name=content}{/block}

</section>

<footer>
    &copy; 2013 Me!
</footer>

</body>
</html>
{extends "main_template.php"}

{block "content"}

<h2>My Content Page</h2>

<p>Lorem ipsum and stuff...</p>

{/block}

我将让您调查Smarty文档以获得更深入的详细信息,但这是我以前所做的。:)

您是否考虑过扩展核心控制器?我的方法是将所有页面信息移出CSV文件(即sitemap.CSV)。然后将csv文件作为数组加载到构造函数中。然后,您可以将所有页面请求路由到一个方法,根据uri动态设置模板(就像您已经做的那样),还可以以相同的方式从sitemap.csv数组请求页面信息。