2012-12-16 6 views
2

내 파일 구조 :액세스 스마티 템플릿

: header.php에서

--header.php 
--smarty 
    --templates 
    -- x.tpl 
    --cache 
    --configs 
    --templates_c 
--articles 
    -- testPage.php 

코드

$smarty = new Smarty(); 

$smarty->setTemplateDir('smarty/templates'); 
$smarty->setCompileDir('smarty/templates_c'); 
$smarty->setCacheDir('smarty/cache'); 
$smarty->setConfigDir('smarty/configs'); 

코드 testPage.php에

<?php 
    include('../header.php'); 
    $smarty->display('x.tpl'); 
?> 

는이 오류를 타격하고

PHP Fatal error: Uncaught exception 'SmartyException' with message 'Unable to 
load template file 'x.tpl'' in 
/usr/local/lib/php/Smarty/sysplugins/smarty_internal_templatebase.php:127 

내 testPage.php에서 멋진 템플릿에 액세스하는 올바른 경로를 설정하는 방법은 무엇입니까?

+1

절대 경로를 제공하려고 시도 했습니까? – shawndreck

+0

혹시 그 사람을 해결하지 못했습니까? – Ted

답변

1

짧은 대답은 testpage.php에서 하나의 디렉토리로 이동해야합니다. 예를 들어 header.php와 마찬가지로 스마트 디렉토리가 들어있는 디렉토리로 이동해야합니다. 똑똑한 디렉토리를 포함합니다. 친절하게이 일을

$smarty->setTemplateDir('../smarty/templates'); 

한 가지 방법은 포함되어있는 것을 사용하고 프로젝트의 루트 디렉토리에 도착하는 방법을 정의하고있다.

testPage.php

define("PATH_TO_ROOT", "../"); 

에서 다음이 그 다음 다른 위치에있을 수있는 다른 PHP 파일에서 스마티 디렉토리 설정에 사소한 있습니다 header.php

$smarty->setTemplateDir(PATH_TO_ROOT.'smarty/templates'); 
$smarty->setCompileDir(PATH_TO_ROOT.'smarty/templates_c'); 
$smarty->setCacheDir(PATH_TO_ROOT.'smarty/cache'); 
$smarty->setConfigDir(PATH_TO_ROOT.'smarty/configs'); 

인치 예 : "tests/webtests/frontend"디렉토리에서 PATH_TO_ROOT를 "../../../"로 정의 할 수 있으며 Smarty를 설정하는 호출이 여전히 작동합니다.

header.php가 PATH_TO_ROOT가 정의되어 있는지 확인하여 직접 호출되지 않도록 할 수도 있습니다.

부수적으로, Smarty 디렉토리 아래에 templates_c 및 cache 디렉토리가없는 대신 다른 곳에서 별도의 디렉토리를 생성하여 생성 된 데이터를 작성할 수 있으므로 (주입 공격에 취약 할 수 있음) 고려할 수 있습니다. 내 프로젝트의 경우 'var'디렉토리는 프로젝트 루트 디렉토리에 있으며 로그 파일, 캐시, 생성 된 템플릿 등에 대한 모든 디렉토리를 포함합니다. 'var'의 하위 디렉토리에있는 모든 내용이 '안전하지 않은'것으로 간주되어 생각할 수 있습니다. 무엇이 안전하고 훨씬 쉬운지에 대해서.

+0

아니요. header.php가 트리 디렉토리의 다른 깊이에있는 여러 php 파일에 포함되어 있으면 ../smarty 경로가 존재하지 않는 디렉토리에 연결되기 때문입니다. – Aubin

+0

안녕하세요, Aubin, 당신은 오해했습니다. 실행중인 파일은 testPage.php이므로 모든 디렉토리 경로는 해당 파일이있는 디렉토리와 관련이 있으므로 header.php와 관련이 없습니다. – Danack

+0

PATH_TO_ROOT은 항상 "../"이 아닙니다. – Aubin

관련 문제