# When and How to Use XHP Categories

DevFeed: [When and How to Use XHP Categories](<https://devfeed.tech/articles/when-and-how-to-use-xhp-categories-22038.md>)

Original publisher: [Read original article](<https://codebeforethehorse.tumblr.com/post/65461659692>)

Author: Codebeforethehorse

Published: 2013-10-29T21:23:00Z

Content type: tutorial

Language: en

Sources: [Stefan Parker](<https://devfeed.tech/sources/stefan-parker.md>)

Topics: [html elements](<https://devfeed.tech/topics/html-elements.md>), [HTML](<https://devfeed.tech/topics/html.md>), [HTML5](<https://devfeed.tech/topics/html5.md>), [ui](<https://devfeed.tech/topics/ui.md>), [Web Development](<https://devfeed.tech/topics/web-development.md>)

Tags: [how-to](<https://devfeed.tech/tags/how-to.md>), [html-elements](<https://devfeed.tech/tags/html-elements.md>), [html5](<https://devfeed.tech/tags/html5.md>), [opinion](<https://devfeed.tech/tags/opinion.md>), [ui](<https://devfeed.tech/tags/ui.md>), [xhp](<https://devfeed.tech/tags/xhp.md>)

## AI overview

The article explains how XHP validates child elements in two passes: custom components are rendered into HTML primitives before the resulting HTML tree is validated. It recommends grouping custom XHP elements by the type of root node returned from render(), allowing related abstractions to remain composable while preserving child validation.

## Source excerpt

I remember when we first added the children keyword to XHP. We had the problem that validating them was really cumbersome. Nearly every element was valid inside a <div> but not all (for instance <meta>). Listing out every valid child wasn't very elegant and certainly would cause problems for custom XHP components. Fortunately, the HTML spec categorized elements just for this purpose. Grouping elements into "block" or "inline" categories made validation far simpler. But if you've used XHP you might be wondering, "Why have I never needed to define my elements as inline or block before?" Well, the answer lies in how XHP renders an element tree. First it will render down all your custom elements into their eventual HTML primitives, then it will render the entire HTML primitive tree into a text string. The key here is that there are actually two passes, meaning XHP validates children in two sets: your elements first and then core HTML elements second. When you put a custom component inside of a <span> element, you don't need to give it a category of %phrase (the HTML5 equivalent of %inline). When XHP renders the tree it will wait on validating the children of the root <span> until its children are HTML elements. class :ui:hello-world extends :x:element { protected function render() { return <b>Hello World!</b>; } } $root = <span> <ui:hello-world /> </span>; So when you render $root, XHP will first render the <ui:hello-world> instance (which will produce the following node tree: <span><b>Hello World!</b></span>). Then it will render (and validate) the <span> and <b> elements. If we returned a <div> element from the :ui:hello-world::render method, then the validation would fail. So since we can use categories with free reign in our custom components, what pattern should we use? HTML groups elements by purpose, but it is my personal opinion that the best way to use categories is to group XHP elements by the type of their returned root node from render(). Let's look at an ex