हम "हैलो वर्ल्ड" की तुलना में कुछ अधिक जटिल चीज़ खींचेंगे - एक पेड़। यह हमें कॉपी-पेस्ट का उपयोग करने से रोकेगा और नोड्स को प्रदर्शित करने के लिए कोड का पुन: उपयोग करने के लिए मजबूर करेगा। उनकी संख्या छोटे - 100 टुकड़े होने दें।
आइए एक साधारण ऑब्जेक्ट डेटा मॉडल बनाएं।
: getArrayData , getXMLData DOM.class Thing {
public $color, $shape, $childs= array();
function getArrayData(){
$childs= array();
foreach( $this->childs as $child ) $childs[]= $child->getArrayData();
$data= array(
'color' => $this->color,
'shape' => $this->shape,
'childs' => $childs,
);
return $data;
}
function getXMLData( $doc ){
$node= $doc->createElement( 'thing' );
$node->setAttribute( 'color', $this->color );
$node->setAttribute( 'shape', $this->shape );
foreach( $this->childs as $child ) $node->appendChild( $child->getXMLData( $doc ) );
return $node;
}
}
srand( 0 );
$things= array();
$colors= array( 'red', 'green', 'blue', 'yellow', 'magenta', 'cyan' );
$shapes= array( 'circle', 'ellipse', 'oval', 'rectangle', 'triangle', 'star', 'rhombus', 'trapeze', 'exploit<u>' );
for($i=0;$i<100;++$i){
$thing= new Thing;
$thing->color= $colors[ array_rand( $colors ) ];
$thing->shape= $shapes[ array_rand( $shapes ) ];
if( $i ){
$things[ array_rand( array_slice( $things, 0, 10 ) ) ]->childs[]= $thing;
};
$things[]= $thing;
}
, DOM 3 . , - .
XSLT
$t1= getTime();
$doc= new DOMDocument;
$data= $things[0]->getXMLData( $doc );
$t2= getTime();
$doc->appendChild( $data );
$xsl= new DOMDocument( );
$xsl->load( 'tpl.xsl' );
$proc= new XSLTProcessor( );
$proc->importStyleSheet( $xsl );
echo $proc->transformToXML( $doc );
$t3= getTime();
: 3 + 5 = 8<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="html" />
<xsl:template match=" thing ">
<div style="color:{@color}">
<xsl:value-of select=" @color " />
<xsl:text> </xsl:text>
<xsl:value-of select=" @shape " />
</div>
<blockquote>
<xsl:apply-templates select=" thing " />
</blockquote>
</xsl:template>
</xsl:stylesheet>
Smarty
- $t1= getTime();
- $data= $things[0]->getArrayData();
- $t2= getTime();
- $smarty= new Smarty;
- $smarty->template_dir = '.';
- $smarty->compile_dir = '.';
- $smarty->assign( 'thing', $data );
- $smarty->display( 'tpl.smarty' );
- $t3= getTime();
- {function name="proc"}
- <div style="color:{$thing.color|escape}">{$thing.color|escape} {$thing.shape|escape}</div>
- <blockquote>
- {foreach from=$thing.childs item=child}
- {proc thing=$child}
- {/foreach}
- </blockquote>
- {/function}
- {proc thing=$thing}
: 1 + 20 = 21
______________________
PHP
$t1= getTime();
$data= $things[0]->getArrayData();
$t2= getTime();
include( 'tpl.php' );
$t3= getTime();
: 1 + 2 = 3function akeurwbkurlycqvaelkuyrc( $data ){ ?>
<div style="color:<?=htmlspecialchars($data['color'])?>">
<?=htmlspecialchars($data['color'])?> <?=htmlspecialchars($data['shape'])?>
</div>
<blockquote>
<? foreach( $data['childs'] as $child ) akeurwbkurlycqvaelkuyrc( $child ) ?>
</blockquote>
<? } akeurwbkurlycqvaelkuyrc( $data );
function getTime(){
return 1000 * microtime( true );
}
<div style="position:absolute;top:0;right:0">
preprocessing: <?= $t2 - $t1 ?><br/>
templating: <?= $t3 - $t2 ?><br/>
total: <?= $t3 - $t1 ?><br/>
</div>
php 5.3.1, libxml 2.7.3, libxsl 1.1.23, smarty 3 rc2
PHP , DOM .