# Rendering as NULL in XHP

DevFeed: [Rendering as NULL in XHP](<https://devfeed.tech/articles/rendering-as-null-in-xhp-22040.md>)

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

Author: Codebeforethehorse

Published: 2014-03-21T00:17:00Z

Content type: tutorial

Language: en

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

Topics: [Code](<https://devfeed.tech/topics/code.md>), [Security](<https://devfeed.tech/topics/security.md>)

Tags: [blog-post](<https://devfeed.tech/tags/blog-post.md>), [code](<https://devfeed.tech/tags/code.md>), [security](<https://devfeed.tech/tags/security.md>), [xhp](<https://devfeed.tech/tags/xhp.md>)

## AI overview

This article examines returning null from XHP elements, especially for conditionally rendered content. It discusses the trade-offs, explains how moving checks outside the element can create unsafe code paths, and considers CSS-based alternatives and their limitations.

## Source excerpt

There are some at Facebook who have argued against returning null when rendering an XHP element. In fact, it's actually against Facebook's standards now. Personally, I prefer to structure my code to return null in XHP, but there definitely are pros and cons. I'll cover the situations in which you might want to return null and what the alternatives could be, and then you can decide for yourself. First, let me clarify that when I say returning null, I actually mean returning an empty <x:frag> element from your :x:element-extended class - basically meaning you don't want it to render into anything. So when would you return null from an XHP element? Well, often times it is when the element should only conditionally exist. Let's contrive a simple example. class :post:edit-link extends :x:element { attribute Post post @require; protected function render() { $post = $this->getAttribute('post'); if ($post->author != get_loggedin_user() && !user_is_admin()) { return <x:frag />; } return <a href={"/post/{$post->id}/edit"}>Edit</a>; } } class :blog:post extends :x:element { attribute Post post @required; protected function render() { $post = $this->getAttribute('post'); return <div class="post"> <div class="title">{$post->title}</div> {$post->content} <div class="links"> <post:permalink post={$post} /> &middot; <post:comment-link post={$post} /> &middot; <post:edit-link post={$post} /> </div> </div>; } } So in this scenario, we have <post:edit-link> which will conditionally return a link to edit the post if you are the author. However, if you are not the author you'll see a floating &middot; underneath each blog post. We cannot tell without rendering <post:edit-link> if it will return content or not, so there's no way to conditionally control the last &middot; from inside :blog:post::render(). This is the primary reason some people feel rendering null in XHP is bad practice. To remove the null rendering from <post:edit-link> we'll have to take that check and put it somewhere e