douyi1966 2013-12-16 01:39
浏览 87
已采纳

Smarty PHP模板,mod重写更改URL但不是页面

I've rewritten this question now as I don't think the problem is to do with the URL rewrite.

I'm building a site with PHP and Smarty and using fairly common url rewriting using apache's mod rewrite.

Locally, if I visit: example.com/search/?q=test, I get search results for "test". /search is not a real directory, it's forwarding to search.php?q=

When I upload, it's not working. I've enabled rewrite with sudo a2enmod rewrite and the url is updating, the browser shows example.com/search/?q=test in the address bar, doesn't throw a 404, but just serves up my index.php page instead.

I've realised the problem is probably to do with my Smarty caching. I've got a URL variable that triggers the cache to be rebuilt, ?rebuildCacheNow.

If I go to /account, I get the index page, but if I go to /account?rebuildCacheNow the account page shows up.

If I then go back to the index, /, it still shows the account page. Going to /?rebuildCacheNow shows me the home page.

This is my main Smarty template:

{* Smarty *}

<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"><![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"><![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"><![endif]-->
<!--[if gt IE 8]><!--><html lang="en"><!--<![endif]-->

{include 'head.tpl'}

<body id="top">

{include 'header.tpl'}

<div class="content-container">
    <div class="container content">
      {include $page.content}
    </div>
</div>

{include 'footer.tpl'}

<script src="{$JQUERY_CDN}"></script>
<script src="{$BOOTSTRAP_JS_CDN}"></script>
<script src="/js/main.js"></script>
{$page.scripts}

</body>
</html>

Every page serves up this template, but I'm using PHP to assign $page.content. So the /buy page sets $page.content = 'buy.tpl' and /search sets $page.content = 'search.tpl'.

But it looks like the main.tpl is being cached - is there a better way of using 1 main outline template and embedding content templates within?

  • 写回答

1条回答 默认 最新

  • dongshui9690 2014-01-05 10:18
    关注

    Ok I've worked it out. So locally I have the rebuildCacheNow set to go off on every page to ensure I see the changes each time - that's why I didn't notice it.

    When I worked out it was to do with the caching of the main.tpl, checking the Smarty docs shows that I can assign a cache_id to the rendered template:

    http://www.smarty.net/docsv2/en/caching.multiple.caches.tpl

    So when I render the Smarty template, I'm giving it the cache ID based on the hash of the $page.content variable:

    // make cache id, based on template path
    $cache_id = sha1( $page[ 'content' ] );
    
    $smarty->display( 'main.tpl', $cache_id );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 Hadoop集群部署启动Hadoop时碰到问题
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 QTableWidget重绘程序崩溃
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站