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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵