2010-12-08 74 views
3

我想知道,如果它是在所有可能創造一個流包裝,以便從數組一些代碼加載到使用類似以下包括從PHP代碼流

<?php include 'template://myarraykey/'; ?> 

,並讓它工作像從文件中做一個正常的包括?提問的原因是因爲我並不是真的想將模板存儲在文件系統中,它們要麼存在於memcache中,要麼存在於數據庫表中,並且不想使用eval()。

另外我會假設我需要allow_url_include設置爲on?

回答

2

包含可以採取任意網址。閱讀this。下面是從那裏取一個例子HTTP代碼:

<?php 

/* This example assumes that www.example.com is configured to parse .php 
* files and not .txt files. Also, 'Works' here means that the variables 
* $foo and $bar are available within the included file. */ 

// Won't work; file.txt wasn't handled by www.example.com as PHP 
include 'http://www.example.com/file.txt?foo=1&bar=2'; 

// Won't work; looks for a file named 'file.php?foo=1&bar=2' on the 
// local filesystem. 
include 'file.php?foo=1&bar=2'; 

// Works. 
include 'http://www.example.com/file.php?foo=1&bar=2'; 

$foo = 1; 
$bar = 2; 
include 'file.txt'; // Works. 
include 'file.php'; // Works. 

?> 

而只是將其更改爲include "template://$thevalue";

+0

經過一些編碼和一些測試後,我得出結論,我吠叫錯誤的樹,因爲使用一個流有相同的問題,因爲在這個例子中邪惡(),我決定把它存儲在數據庫和緩存到文件系統的方式,它受益於被APC緩存,每次模板編輯後我都會更新文件系統緩存。 – Stu 2010-12-15 10:43:57

2

EVAL,這個詞,是不是邪惡。你可以用它做的事情是。任何你想做的手段都會與eval具有相同的風險。所以只需使用eval,因爲確保它是一個更「已知」的問題。

1

由於include可以使用任何適當的流,你可以註冊自己的流包裝,我不明白爲什麼不。

只是爲了好玩,您可以嘗試一種替代方法:從memcached加載數據並使用data stream wrapper將其包含在內。

7

我知道這是一個老問題...但我認爲這是值得一提的是,你可以這樣做:

$content = ' 
    <?php if($var=true): ?> 
    print this html 
    <?php endif; ?> 
'; 

通常這會非常給EVAL繁瑣,但你可以這樣做:

include "data://text/plain;base64,".base64_encode($content); 

它會解析它像活潑!