[Wikitech-l] Code style: overuse of Html::element()

Antoine Musso hashar+wmf at free.fr
Mon May 13 22:10:42 UTC 2013


Le 13/05/13 19:26, Max Semenik a écrit :
> Hi, I've seen recently a lot of code like this:
> 
> $html = Html::openElement( 'div', array( 'class' => 'foo' )
>     . Html::rawElement( 'p', array(),
>         Html::element( 'span', array( 'id' => $somePotentiallyUnsafeId ),
>             $somePotentiallyUnsafeText
>         )
>     )
>     . Html::closeElement( 'div' );
> 
> IMO, cruft like this makes things harder to read and adds additional
> performance overhead.

Html is just like our Xml class, that let us raise the probability that
the result code will be valid.  That is also a good way to make sure the
content is properly escaped, though in the example above that could lead
to some mistake due to all the nested calls.

For the performance overhead, it surely exist but it is most probably
negligible unless the methods are in a heavily used code path.


Ideally we would use templates to generate all of that. That will let us
extract the views logic out of the PHP code.


-- 
Antoine "hashar" Musso




More information about the Wikitech-l mailing list