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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀