2010-12-08 50 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); 

它会解析它像活泼!