2017-03-03 32 views
2

如何将字符{{ }}更改为.php页面上的等效回显函数,几乎完全像laravels blade.php页面。 (如果可能的话)PHP,将{{}}转换为.php页面上的回显

我环顾四周,唯一类似的问题是我似乎找到的是“如何使用laravel刀片没有laravel”这不是我想要的。

编辑

进出口张贴因为apokryfos的回答这个新的数据

这是运行

function View($view) 
{ 
    $src = __DIR__.'/../../views/'.$view.'.view.php'; 
    $destination= __DIR__.'/../../views/'.$view.'.view.php'; 

    file_put_contents(
     $destination, 
     str_replace(["{{","}}" ],[ "<?=", "?>" ], file_get_contents($src)) 
    ); 

    echo file_get_contents($destination); 
} 

从控制器我用这个代码来调用该函数View('welcome');它的功能IM通过检索页面来工作,但唯一的问题是不会在页面上发布变量,而直接在网址中访问页面。

这是我的网页上的代码:

<?php 
    $item = 11; 
?> 
    a php variable is {{ $item }} 

这帖子“一个PHP变量is'and然后什么网页上,不知道为什么

+1

开放式刀片源代码,看看? –

回答

3

简单的事情是:

$src = "sourcefile.php"; 
$destination= "compiledsourcefile.php"; 

file_put_contents(
    $destination, 
    str_replace(["{{","}}" ],[ "<?=", "?>" ], file_get_contents($src)) 
); 

include $destination; 

为了呼应这直接做:

$src = "sourcefile.php"; 
$text = str_replace(["{{","}}" ],[ "<?=", "?>" ], file_get_contents($src)) 
eval("?>".$text); //Things get a bit ugly here 
echo $text; 

原因"?>"是因为EVAL在“PHP模式”默认启动但正确编写PHP文件将明确设置它需要进入PHP模式的区域,并默认以HTML模式启动。

注意:大多数人会说使用eval是不好的,虽然我确实同意使用eval未检查确实是不好的,但是在从你写的PHP文件中读取的代码上使用它的确如此该文件上的include并不差。

+0

这真的很有趣,我从来没有使用PHP这种方式:)感谢这 –

+0

我得到一个错误'file_put_contents ***未能打开流:没有错误*路径/行号*'。我的src和目的地是一样的,这会导致任何问题? –

+0

@Yasmin法国我的不好,有错误的方法 – apokryfos

-2

您可以使用此:

<?= $var ?> 

这是一样的:

<?php echo $var ?>