链接更改时加载不同的包含

时间:2014-07-13 10:48:56

标签: php smarty

想知道在php中是否可以使用这样的东西以及如何做到这一点。

我想将一个tpl分配给索引页面,当点击一个按钮并且索引更改为注册时我想要分配一个不同的tpl

这样的事情:

if('rendered_page' = signup.php){

$t->assign('rendered_page', $t->fetch('signup.tpl') );

{else} 

$t->assign('rendered_page', $t->fetch('login.tpl') );

}

$t->display( 'index.tpl' );

2 个答案:

答案 0 :(得分:0)

一切都取决于您的结构和需求。

你可以这样做:

在PHP中

<?php

$currentUrl = $_SERVER['REQUEST_URI'];

if($currentUrl == 'signup.php') { // notice that there are 2 = signs not one to compare

$t->assign('rendered_page', $t->fetch('signup.tpl') );

else {

$t->assign('rendered_page', $t->fetch('login.tpl') );

}

$t->display( 'index.tpl' );

在index.tpl

{$rendered_page}

但你也可以这样做(简单的显示模板不首先获取):

在PHP中

<?php

$currentUrl = $_SERVER['REQUEST_URI'];

if($currentUrl == 'signup.php') { // notice that there are 2 = signs not one to compare

$t->display('signup.tpl');

else {

$t->display('login.tpl');

}

最后一个选项是将它直接放在Smarty模板中,所以你可以这样做:

PHP中的

<?php

$currentUrl = $_SERVER['REQUEST_URI'];
$t->assign('currentUrl', $currentUrl);
$t->display('index.tpl');

在index.tpl

{if $currentUrl eq 'signup.php'}
   {include 'signup.tpl'}
{else}
   {include 'login.tpl'}
{/if}

答案 1 :(得分:-1)

根据您提供的说明,我认为您需要检查当前页面是什么,可以通过以下方式完成:

<?php
$currentpage = basename($_SERVER['PHP_SELF']);
?>

所以你的代码将成为:

<?php
$currentPage = basename($_SERVER['PHP_SELF']);

if($currentPage == "signup.php"){

     $t->assign('rendered_page', $t->fetch('signup.tpl') );

}else{ 

     $t->assign('rendered_page', $t->fetch('login.tpl') );

}
?>