---
title: "Writing the <g:if> tag in a tag library"
author: "boogie"
date: "2008-06-24T11:51:52Z"
tags: []
slug: "writing-the-g-if-tag-in-a-tag-library"
draft: false
lastmod: "2008-06-24T11:51:52Z"
id: 207404
url: "https://boogie.iteye.com/blog/207404"
---

The internal if/else **tags** that come with Grails translate directly  
into if(..) {} else {} at the syntax level to improve performance of  
such core **tags** . But you can quite easily write if/else **tags** using the  
**pageScope** object and Grails' standard **tags**

for example:

def ifSomething = { attrs, body -&gt;  
     pageScope.doElse = !attrs.condition  
     if(attrs.condition) out &lt;&lt; body()  
}  
def elseSomething = { attrs, body -&gt;  
    if(pageScope.doElse)  out &lt;&lt; body()  
}

Then

&lt;g:ifSomething condition="${1==2}"&gt;yes&lt;/g:ifSomething&gt;  
&lt;g:elseSomething&gt;no&lt;/g:elseSomething&gt;