smarty模板引擎從設定檔中擷取資料的方法,smarty模板_PHP教程

來源:互聯網
上載者:User

smarty模板引擎從設定檔中擷取資料的方法,smarty模板


本文執行個體講述了smarty模板引擎從設定檔中擷取資料的方法。分享給大家供大家參考。具體如下:

當某個變數值,不希望在程式中寫死時,就可以把該變數寫到設定檔裡,並從中擷取(常見的配置樣式)。

第一步:先寫一個設定檔,如資料庫的 db.conf,尾碼名conf可以隨便寫,db.ini也行。檔案中內容的格式需要固定:key="值",每一行後面不需要添加分號或者什麼,直接回國換行,如:

設定檔:db.conf
複製代碼 代碼如下:host = "localhost"
username = "root"
password = "123456"
db_name = "liuyan"

模板檔案:temp.tpl

使用{config_load file="db.conf"} 將檔案引入進來。注意,如果寫相對路徑的話,要以訪問的頁面來看。比如這裡,temp.tpl放在templates目錄下,db.conf是放在與templates目錄同一層,但由於瀏覽器訪問的檔案index.php與db.conf同一層,所以,引用時,直接寫 {config_load file="db.conf"} 。
複製代碼 代碼如下:{config_load file="db.conf"}

smarty變數操作,從設定檔中擷取


{#host#}


{#username#}


瀏覽器訪問:index.php

與從php擷取變數資料不同,這裡不需要用assign分配,而在模板檔案裡直接載入
複製代碼 代碼如下:<?php
//建立smarty對象
require_once("./libs/Smarty.class.php");
$smarty = new Smarty();
$smarty->display("index.tpl");
?>

希望本文所述對大家的smarty程式設計有所協助。

http://www.bkjia.com/PHPjc/946751.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/946751.htmlTechArticlesmarty模板引擎從設定檔中擷取資料的方法,smarty模板 本文執行個體講述了smarty模板引擎從設定檔中擷取資料的方法。分享給大家供大家參考...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.