рдпрд╣ рдПрдХ рдмрд╣реБрдд рд╣реА рдЖрдо рдЧрд▓рдд рдзрд╛рд░рдгрд╛ рд╣реИ рдХрд┐ xslt рдареЛрд╕ рдмреНрд░реЗрдХ рд╣реИ, рдФрд░ рд╕рдордЭрджрд╛рд░ рд╣рдорд╛рд░рд╛ рд╕рдм рдХреБрдЫ рд╣реИред рд╣рдо рд╕реНрдорд╛рд░реНрдЯ рд╕рд┐рдВрдЯреИрдХреНрд╕ рдХреА рд╕реБрдЧрдорддрд╛ рдФрд░ xslt рд╕рдорд░реНрдерди рдХреА рд╕реБрд╡рд┐рдзрд╛ рдХреЛ рдЕрд▓рдЧ рдХрд░рддреЗ рд╣реИрдВ, рдФрд░ рд╣рдореЗрдВ рдЙрдирдХреЗ рдХрд╛рдо рдХреА рдЧрддрд┐ рдкрд░ рдареАрдХ рд╕реЗ рдЯрдХрдЯрдХреА рд▓рдЧрд╛рдиреЗ рджреЗрддреЗ рд╣реИрдВред
рд╣рдо "рд╣реИрд▓реЛ рд╡рд░реНрд▓реНрдб" рдХреА рддреБрд▓рдирд╛ рдореЗрдВ рдХреБрдЫ рдЕрдзрд┐рдХ рдЬрдЯрд┐рд▓ рдЪреАрдЬрд╝ рдЦреАрдВрдЪреЗрдВрдЧреЗ - рдПрдХ рдкреЗрдбрд╝ред рдпрд╣ рд╣рдореЗрдВ рдХреЙрдкреА-рдкреЗрд╕реНрдЯ рдХрд╛ рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рд╕реЗ рд░реЛрдХреЗрдЧрд╛ рдФрд░ рдиреЛрдбреНрд╕ рдХреЛ рдкреНрд░рджрд░реНрд╢рд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдХреЛрдб рдХрд╛ рдкреБрди: рдЙрдкрдпреЛрдЧ рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдордЬрдмреВрд░ рдХрд░реЗрдЧрд╛ред рдЙрдирдХреА рд╕рдВрдЦреНрдпрд╛ рдЫреЛрдЯреЗ - 100 рдЯреБрдХрдбрд╝реЗ рд╣реЛрдиреЗ рджреЗрдВред
рдЖрдЗрдП рдПрдХ рд╕рд╛рдзрд╛рд░рдг рдСрдмреНрдЬреЗрдХреНрдЯ рдбреЗрдЯрд╛ рдореЙрдбрд▓ рдмрдирд╛рдПрдВред
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;
}
: getArrayData , getXMLData DOM.
, 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();
<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>
: 3 + 5 = 8
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();
function 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 );
: 1 + 2 = 3
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 .