Feed on
Posts
Comments
推薦本文到Plurk噗浪去!
apache_software_foundation_logo_3074

Apache 可以透過 mod_mime 模組且根據使用者定義的副檔名來 response data 給 Client 端,此功能可以應用在前台搭配 Template Library,例如 Mustache Logic-less templates,透過此 Apache 模組 可以在 html 檔案將定義好全部 Template,一次讀取進來,底下舉個例子:

<script type="text/x-mustache-template" data-id="me">
  <!--#include file="assets/templates/test1.mustache" -->
</script>

我們希望 apache 能夠讀取 assets/templates/test1.mustache,並且將檔案內容放到 script 裡面,這時候就必須在 apache httpd.conf 定義 text/x-mustache-template

<IfModule mime_module>
    AddType text/x-mustache-template .mustache
    AddOutputFilter INCLUDES .mustache
</IfModule>


接著必須設定網站根目錄可以輸出 include file

<Directory "C:/xampp/htdocs">
    Options All
    AllowOverride All
    SetOutputFilter INCLUDES
    Order allow,deny
    Allow from all
</Directory>

上面例子是 xampp httpd.conf 檔,設定完成後重新啟動 apache,大家就可以發現,原本的 js 就會變成

<script type="text/x-mustache-template" data-id="me">
  Hi, {{first_name}} {{last_name}} 使用者
</script>

這有個缺點就是,之後在轉移機器,就要在重新設定 Apache,為了省掉這個步驟,推薦大家使用 RequireJs 模組: Text plugin

推薦本文到Plurk噗浪去!