douxie9347 2014-09-10 18:53
浏览 62
已采纳

PHP:将静态HTML内容与已解析的PHP变量集成?

I am working in a project which has the following restriction defined:
My PHP files must not have more than one opening or closing tag.
So it's PHP from top to bottom, but I am allowed to add static content by the means of 'import'.

What are proper/elegant ways to add static HTML content to my PHP index file (like outputting a website menu header or a formular) and at the same time resolve PHP variables inside the file.

Like a formular which makes a HTTP POST (login or register) and displays the previously entered email address in case of a mismatch, etc etc.

One way would be

echo "<form ...> 
 <input ... value='$lastemail'>";

But I dislike the quoting. echo <<< EOF is also not great for the purpose imho. I think HTML code should stay together without separating it into multiple echos so it can be validated.

So I am looking for a good solution to import/integrate static HTML code, like a template system and still resolve PHP variables.

Update:
The restriction is made to not mix HTML and PHP code.

I think I will need an engine/class/function which replaces variables inside a HTML template with PHP code. Like searching for ${variable} and replacing it with the php $variable as if it was PHP code.
I just thought maybe there is already something existing within PHP to solve that.

Update: Should I oppose the requirement ? Would be very interesting to hear the oppinion of a professional PHP developer with long history in that area. (On The restriction is made to not mix HTML and PHP code. )

  • 写回答

2条回答 默认 最新

  • ds261634878 2014-09-10 19:28
    关注

    Perhaps you could use a templating system? There are plenty out there for PHP. Some nice ones are (in my opinion):

    1. Twig, which is very small and fast
    2. Smarty, a little larger, but also fast and very popular
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 CreateFile无法获取特殊文件内容
  • ¥35 基于python的有ssl加密传输的socket聊天室
  • ¥15 数码管亮度控制器设计
  • ¥15 kafka客户端跨网段访问,看日志提示连接的还剩内网地址,且访问不通
  • ¥15 关于c语言代码的问题
  • ¥15 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥30 vue 页面窗口放大或者缩小元素会变化
  • ¥15 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。