Conditionals and Expressions
Control your theme's output with `b:if`, `b:else`, and expressions for truly dynamic layouts.
Conditional tags and loops give you fine-grained control over what content is displayed and where. You can show specific widgets only on the homepage, change layouts for specific pages, or loop through all post labels to create a tag cloud.
Conditional Logic with `<b:if>`
The <b:if> tag lets you render content only if a certain condition is true. The condition is placed in the cond attribute.
Common conditions:
cond='data:view.isHomepage': True only on the main homepage.cond='data:blog.pageType == "item"': True only on individual post pages.cond='data:post.thumbnailUrl': True if the post has a thumbnail.
Using `<b:else/>`
You can provide alternative content when a condition is false by placing an <b:else/> tag immediately after the closing </b:if> tag.
Expressions with `expr:`
The `expr:` operator allows you to dynamically set an HTML attribute. For example, instead of a static `href`, you can use `expr:href="data:blog.url"` to link to the blog's homepage URL.
Code Examples
12345<b:if cond='data:view.isHomepage'> <div class='welcome-banner'> <h2>Welcome to our Blog!</h2> </div></b:if>12345<b:if cond='data:post.thumbnailUrl'> <img expr:src='data:post.thumbnailUrl' /><b:else/> <img src='https://via.placeholder.com/150' /></b:if>Was this lesson helpful?
Have feedback, found an issue, or have a suggestion? Let us know!
