English 中文(简体)
PHP中的超额标记操纵
原标题:HTML markup manipulation in PHP
  • 时间:2012-01-15 13:38:59
  •  标签:
  • php
  • html
  • dom

• 与PHP DOM合作—— html操纵。 它对产出超文本标记确实非常有效(至少防止密码的逻辑和标志)。

但是,为了产生50条html标记线,必须在PHP中写出约200条线:(............)。

例:

............

$signup_form = $document->createElement( form );
$signup_form->setAttribute( id ,  signup_form );
$signup_form->setAttribute( action ,  registration/signup.php );
$signup_form->setAttribute( method ,  post );
$su_fname_label = $document->createElement( label );
$su_fname_label->setAttribute( for ,  fname );
$su_fname_label_content = $document->createTextNode( Name );
$su_fname_label->appendChild($su_fname_label_content);
$su_fname_textbox = $document->createElement( input );
$su_fname_textbox->setAttribute( name ,  fname );
$su_fname_textbox->setAttribute( class ,  valid );
$su_fname_textbox->setAttribute( placeholder ,  Please enter your name );
$su_fname_textbox->setAttribute( type ,  text );

......

如你所知,它只是形式的一个要素。 Imagine, 如果以超文本形式出现5个要素,则《特区法典》将十分庞大。 我不禁要问,是否通过一劳永逸地确定多重属性来加以排除? 任何建议?

最佳回答

Why don t you just output HTML and enabled output buffering. Then you can filter out the output using the tidy extension and be sure that your output HTML is valid, while still going as fast as writing pure HTML.

尽管如此,正如@Gordon建议的那样:

简言之,对方案编写者(x)html来说并不合适。 更像一个邮政处理员。

问题回答

你们为什么利用OMD来产生html的产出? 没有任何办法使人力部<><>>>!

Usually you just use PHP for templating. Create a separate file like so:

<?php // template.php
echo  <!DOCTYPE html> ,"
";
?>
<html>
    <head>
    <meta charset="utf-8">
    <title><?=htmlspecialchars($title, ENT_NOQUOTES,  utf-8 )?></title>
    </head>
    <body>
       <form id="signup_form" action="registration/signup.php" method="post">
           <label for="fname">Name</label>
           <input id="fname" name="fname" class="valid" placeholder="Please enter your name" type="text">
       </form>
    </body>
</html>

然后使用模板功能:

function render($__filename, $__data) {
    extract($__data, EXTR_SKIP);
    ob_start();
    include $__filename;
    return ob_get_clean();
}

将贵方的模板从您的法典中删除:

do_something();
$templatedata = array( title => The Title );
$html = render( template.php , $templatedata);

如果你们想要的是没有意识的东西(这是确保知情和正确理解的伟大想法!),那么你可以使用XSLT或第三方模板发动机,如PHPTAL

Depending on your need for speed and if you re able to use caching and the like, a library such as phpQuery could be of use.

缩略语 Query to PHP and has same globaltility and simplicity as 。 然而,从我的经验来看,这并不像我在制定法典时一样快(但I <>没有<>,但进行过任何彻底的测试,这只是一种感觉)。

The code you ve given as an example could be expressed along these lines with phpQuery (untested):

$form = pq ( <form /> )
          ->attr (array (
                     id  =>  signup_form ,
                     action  =>  registration/signup.php ,
                     method  =>  POST ,
                  ));
$label = pq ( <label /> )
          ->html ( Name )
          ->attr ( for ,  fname )
          ->appendTo ($form);
$text = pq ( <input /> )
          ->attr (array (
                     name  =>  fname ,
                     data-placeholder  =>  Please enter your name ,
                     type  =>  text ,
                  ))
          ->addClass ( valid )
          ->appendTo ($label);

I ve written it with the formatting I prefer, and it s slightly more lines than in the code you ve supplied; but it could easily be reduced to 5-10 or so lines with maintained readability, if you prefer more compact coding. At any rate, I find it quite a lot easier to read and you don t have to write quite so much DOM code in order to generate quite a lot of HTML code.

此外,考虑到<代码>php 质量 采用与<代码>j相同的总结概念 Query , an unwrapped node, for example $form in the above Code, is a regular MNode, 但pq()不在此列。 这使你在使用<条码>、质量<>/代码>方便例行或<条码>的“基本”功能方面有很大的灵活性。 PHP DOM。





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签