duanlei20082008 2012-03-20 16:04
浏览 76
已采纳

代码可重用的HTML UI元素PHP

I'm new to PHP and I am currently creating a PHP web application for an assignment at university. During the development I had realized that alot of elements in the user interface were being replicated in different sections of my application. For example I may have a hyperlink button in a Clients Page called 'New Client' (<a class="bigbutton" href=".......">New Client</a>) and another hyperlink button in a projects page called 'New Project' (<a "bigbutton" href=".......">New Project</a>).

Normally, I could just copy the html that constructs the buttons on the Clients Page and Change the content to suit the 'New Project' button in the projects page. Now If I was convert the hyperlink button to the tag, I would have to go to every instance of that particular button to replace the tag with a tag and if this button exists on so many pages, then updating would be time consuming.

That was just a simplistic example, then I have to think for other repeated elements across multiple pages such as breadcrumbs, list tables etc.

Is there any way to code in a way where I can actually reuse these UI elements that may be worthwhile to look at? Perhaps I could try to code each of these elements into classes, but right now I am not sure.

Any hints, pointers or resources would very helpful and appreciated.

Thanks guys.

  • 写回答

2条回答 默认 最新

  • dstjh46606 2012-03-20 16:10
    关注

    You can make PHP functions which echo the HTML when called, optionally including some config stuff e.g.

    function make_button($id, $label) {
        echo '<button id="'.$id.'">'.$label.'</button>';
    }
    

    This can be done for large chunks of the page, or just small bits. Some systems I've used have a html_writer class for this sort of thing, others use render methods where you pass an object in and it'll pass back a string of HTML representing that object on a page.

    Up to you which you prefer. Also good to make small functions which can be reused in larger functions like:

    function make_buttons_from array($array) {
        foreach ($array as $id => $label) {
            $this->make_button($id, $label);
        }
    }
    

    The examples here echo their output, but you can also use string concatenation to assemble the pieces. Again, your call. There's not really a right way.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计