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 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看