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 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测