duanchuaiwan0063 2008-11-04 17:41
浏览 81
已采纳

PHP标签库

I can't stand HTML intermixed with code. I can barely look at one of those templates without feeling nauseated. I know, when you have an army of "web designers" at your disposal, and you are the only developer, it makes sense to use some templating system. I heard about Smarty a lot. But in my case it has been always the source of yet more work.

Anyways, this time I'm going solo, so I don't want to touch an HTML template with the proverbial pole. So what I'm currently doing looks like this:

<?php
$page = new html_page('My wonderful page');
$page->add_contents(new html_tag('p', 'It works', array('id' => 'helloworld', 'class' => 'somecssclass')));

echo $page->render();
?>

Everything belongs to a nice hierarchy of objects, which is good and dandy. Of course I have a lot of smaller classes, and I'm thinking of using dynamic classes (for example, 'html_a' will automagically create an html_tag object of type 'a'.)

Now, my question: it seems that nobody else is doing this. Why? I'm too far out there, and my feeble, oxygen deprived mind is missing something from the big picture?

(I clearly remember an open source library that did exactly this, but can't find it anymore. So unless I'm actually imagining things, I'm not the only one who thought of this approach to render HTML.)

Do you have any thoughts on this? (please refrain to recommend another language; my favorite language is Perl, so I can outfan you easily :-). I'm stuck with PHP.)

Edit

Uh-uh, seems that I struck a nerve. Some clarifications:

  1. I'm the only developer in this project.
  2. How I'm mixing code with HTML? An "html_tag" object from my library is pretty similar to, say, a node in the DOM. The "render" method is the one that creates html (beautifully indented, I must add) but I don't write any opening or closing tag anywhere.
  3. I create small objects for several tasks. These objects have methods to build tag objects; these resulting objects are then inserted into, say, tables or pages.
  4. Did I mention that I'm stuck with PHP?
  5. My library have some primitive access methods to find objects. So the iterator example posted in 26288 can be implemented with relative ease.
  6. I'm not worried about performance (yet.) I've always thought that's a really nice problem to have, but I'm not there yet.
  • 写回答

13条回答 默认 最新

  • dptdb84606 2008-11-04 20:33
    关注

    Slightly different, but yet similar. I wrote an article about using DOM for binding variables to templates. You may find it interesting.

    Basic use-case:

    $t = new Domling('<p class="hello"></p>'); 
    $t->capture('hello')->bind("Hello World"); 
    echo $t->render();
    

    Which produces:

    <p class="hello">Hello World</p> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(12条)

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路