Thinkphp5.1 文件引入
1、視圖文件中引入其他的視圖文件
文件目錄如下圖:
需要實現在index.html中引入public文件夾下面的四個html模板文件,index.html引入代碼如下:
{include file="/public/_meta"} {include file="/public/_header"} {include file="/public/_menu"} <div>中間部分填寫頁面特有的內容</div> {include file="/public/_footer"}
通過這種方式,能夠將通用的,不常改變的內容抽取出來,避免了代碼冗余。
2、視圖文件中引入public文件下的靜態文件
文件目錄如下圖:
我們需要在index.html的視圖模板中引入一些靜態的css、js文件,這些文件一般存放在public目錄下,我們現在采用相對路徑的方式引入這些文件,index.html文件引入代碼如下:
<link rel="stylesheet" type="text/css" href="/../static/Hadmin/static/h-ui/css/H-ui.min.css" rel="external nofollow" /> <link rel="stylesheet" type="text/css" href="/../static/Hadmin/static/h-ui.admin/css/H-ui.admin.css" rel="external nofollow" /> <link rel="stylesheet" type="text/css" href="/../static/Hadmin/lib/Hui-iconfont/1.0.8/iconfont.css" rel="external nofollow" /> <link rel="stylesheet" type="text/css" href="/../static/Hadmin/static/h-ui.admin/skin/default/skin.css" rel="external nofollow" id="skin" /> <link rel="stylesheet" type="text/css" href="/../static/Hadmin/static/h-ui.admin/css/style.css" rel="external nofollow" />
在我的測試項目中,上述代碼引入成功。若實際實踐過程中引入失敗,不要急,介紹個方便調試的辦法:首先,刷新你的網頁;第二步,右鍵查看網頁源代碼;第三步,復制對應的文件引入鏈接,在新的頁面中打開;第四步,檢查瀏覽器實際識別到的文件路徑是哪里,通過調整url調整到正確的文件路徑,即能正確訪問到文件內容為止;第五步,參照這個能夠使用的文件路徑對現有的路徑進行改進即可。
3、自己定義全局變量,在模板文件中使用
需要說明一下,tp5.1更改了配置變量名,更改如下:
'tpl_replace_string' => [ '__STATIC__' => '/static', ],
具體實現步驟:
1、在Config/template.php中加入自己需要定義的常量。
2、直接在模板中使用即可,如下:
<link rel="stylesheet" type="text/css" href="__STATIC__/Hadmin/static/h-ui/css/H-ui.min.css" rel="external nofollow" />
這個路徑具體設置,根據你的實際項目目錄而定,自行更改即可。
總結
以上所述是小編給大家介紹的thinkphp5.1 文件引入路徑問題及注意事項,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!