将php代码分配给smarty模板引擎

问题描述:

I make a new website for my company and i want to use smarty (v3.1.29) for it. Now the problem is that we store the code for all pages in our database (home, products, downloads, ...). Some pages contains PHP inline functions like:

<?php include("functions.php"); ?>    

<p> Hello <?php echo printWorldInColor(); ?> </p>

In my template (.tpl) file I have a div-section to load the content from our database:

<html>
  <body>
    <div id="content">
      {$content}
    </div>
  </body>
</html>

So my code looks like this afterwards:

<html>
  <body>
    <div id="content">
      <?php include("functions.php"); ?>    

      <p> Hello <?php echo printWorldInColor(); ?> </p>
    </div>
  </body>
</html>

Is there a way that smarty processes the PHP-code before parsing it?

Hint: I dont want to edit my template file. I just want to parse the database content to my content-section of the website.


What i tried:

  • saved database-content into a string and replaced PHP-tags with smarty-PHP-tags, then assign it to the template
  • SmartyBC-Class

我为我的公司创建了一个新网站,我想使用smarty(v3.1.29)。 现在问题是我们存储 b> 代码 b>所有页面在我们的数据库 b>(家庭,产品,下载,...)。 有些页面包含PHP内联函数,如: p>

 &lt;?php include(“functions.php”);  ?&GT;  
 
&LT; p为H. 你好&lt;?php echo printWorldInColor();  ?&GT;  &lt; / p&gt; 
  code>  pre> 
 
 

在我的模板(.tpl)文件中,我有一个div部分来加载我们数据库中的内容: p> &lt; html&gt; &lt; body&gt; &lt; div id =“content”&gt; {$ content} &lt; / div&gt; &lt; / body&gt; &lt; / html&gt; code> pre>

所以我的代码后来看起来像这样: p>

 &lt; html&gt; \  n&lt; body&gt; 
&lt; div id =“content”&gt; 
&lt;?php include(“functions.php”);  ?&GT;  
 
&lt; p&gt; 你好&lt;?php echo printWorldInColor();  ?&GT;  &lt; / p&gt; 
&lt; / div&gt; 
&lt; / body&gt; 
&lt; / html&gt; 
  code>  pre> 
 
 

有没有办法聪明地处理 解析之前的PHP代码? p>

提示:我不想编辑我的模板文件。 我只是想将数据库内容解析到我网站的内容部分。 b>


我尝试过: p>

  • 将数据库内容保存到字符串中并用智能PHP标签替换PHP标签,然后将其分配给模板 li>
  • SmartyBC-Class li> ul> div>

You can't do that in Smarty. Also running php code stored in the database sounds like a terrible idea. But if for some reason you have to go on with this nonsense (and considering that you can't use eval), you can try this:

  1. Read the php code from the database.
  2. Save to a temporal php file
  3. Turn on output buffering with ob_start()
  4. include the file you have created
  5. assign the output to a variable with ob_get_clean()
  6. assign the variable to the template

But if I was you, I would try to do the project in another way.