---
title: "smarty什么时候会从新编译template"
author: "xombat"
date: "2009-01-09T18:40:05Z"
tags: ["PHP"]
categories: ["编程语言"]
slug: "smarty-template"
draft: false
lastmod: "2009-01-09T18:40:05Z"
id: 313702
url: "https://orei11y.iteye.com/blog/313702"
---

从smarty.class.php文件中的下列代码可以看出来：


```java
   /**
     * test if resource needs compiling
     *
     * @param string $resource_name
     * @param string $compile_path
     * @return boolean
     */
    function _is_compiled($resource_name, $compile_path)
    {
        if (!$this->force_compile && file_exists($compile_path)) {
            if (!$this->compile_check) {
                // no need to check compiled file
                return true;
            } else {
                // get file source and timestamp
                $_params = array('resource_name' => $resource_name, 'get_source'=>false);
                if (!$this->_fetch_resource_info($_params)) {
                    return false;
                }
                if ($_params['resource_timestamp'] <= filemtime($compile_path)) {
                    // template not expired, no recompile
                    return true;
                } else {
                    // compile template
                    return false;
                }
            }
        } else {
            // compiled template does not exist, or forced compile
            return false;
        }
    }
```


首先会看，是否设置了force_compile，如果为true，那么每次都要进行重新编译。
然后看是否要compile_check，如果为false，那么永远不会进行编译。
如果为true，那么进行检查，检查什么呢？检查文件的修改时间，<span style="color:red">如果编译结果的时间>=原模板文件的修改时间，那么不进行重新编译，否则进行编译。</span>

关键在于这个“等号”，我们应该将这个“=”去掉呢？还是他真的有必要？

有事，待续...