dongrong1856 2016-05-30 12:04
浏览 69
已采纳

将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
  • 写回答

1条回答 默认 最新

  • douliao5550 2016-05-30 14:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥15 DruidDataSource一直closing
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据