Smarty Conditionals - HL Vanilla Community
<main> <article class="userContent"> <div class="embedExternal embedImage display-large float-none"> <div class="embedExternal-content"> <a class="embedImage-link" href="https://us.v-cdn.net/6030677/uploads/C2KJIF78XQHM/microsoftteams-image-288-29.png" rel="nofollow noreferrer noopener ugc" target="_blank"> <img class="embedImage-img" src="https://us.v-cdn.net/6030677/uploads/C2KJIF78XQHM/microsoftteams-image-288-29.png" alt="MicrosoftTeams-image (8).png" height="108" width="1356" loading="lazy" data-display-size="large" data-float="none"></img></a> </div> </div> <p>Smarty includes a base amount of logic that you can insert into your template. One of these is a conditional statement. A couple of basic examples should give you an idea of the syntax and format of Smarty conditionals.</p><p>This example uses the user data from the controller’s data array to print out a welcome message if the user is signed in and a generic message if the user is not signed in. <a href="https://docs.vanillaforums.com/developer/smarty/#accessing-controller-data-with-smarty" rel="nofollow noreferrer ugc">Learn more about accessing the controller’s data array using Smarty.</a></p><pre class="code codeBlock" spellcheck="false" tabindex="0">{if $User.SignedIn} <h3>Welcome Back!</h3> {else} <h3>Hello Stranger!</h3> {/if} </pre><p>This example checks to see if the locale matches ‘fr’ and if so, inserts the contents a pocket named ‘French Pocket’. <a href="https://docs.vanillaforums.com/functions/pocket.html.md" rel="nofollow noreferrer ugc">Learn more about the Smarty pocket function.</a></p><pre class="code codeBlock" spellcheck="false" tabindex="0">{if $CurrentLocale.Lang === 'fr'} {pocket name='French Pocket'} {/if} </pre><p>The following PHP internal functions are available for use in Smarty if statements: array, list, isset, empty, count, sizeof, in_array, is_array, true, false, and null. We’ve also whitelisted some PHP functions in Vanilla that can be used in Smarty if statements. The most useful of these functions are outlined below.</p><h2 data-id="conditional%3A-category">Conditional: Category</h2><p>Returns an array representing the current category.</p><h3 data-id="example">Example</h3><p>This example prints out a message if the user is browsing a page in the “General” category. Also uses the GetValue function, outlined below.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">{if GetValue('Name', Category()) === 'General'} <p>You're checking out content in the General category!</p> {/if} </pre><h2 data-id="conditional%3A-checkpermission">Conditional: CheckPermission</h2><p>Tests whether the current user has the passed permission or array of permissions and returns true if the current user has one of the given permission(s).</p><h3 data-id="example-1">Example</h3><p>This example checks whether the user has either the “Garden.Settings.Manage” or “Garden.Settings.View” permission and if so, adds a link to the dashboard.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">{if CheckPermission(['Garden.Settings.Manage', 'Garden.Settings.View'])} {dashboard_link} {/if} </pre><h2 data-id="conditional%3A-homepage">Conditional: Homepage</h2><p>The variable <code class="code codeInline" spellcheck="false" tabindex="0">$Homepage</code> is set to true if (and only if) the user is on your homepage.</p><h3 data-id="example-2">Example</h3><p>This example checks whether the user is on the homepage to display one message or another.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">{if $Homepage} <p>Welcome home!</p> {/if} <p>Intermediary text that is always displayed.</p> {if !$Homepage} <p>Far away from home.</p> {/if} </pre><h2 data-id="conditional%3A-insection">Conditional: InSection</h2><p>Returns true if the user is in a section or one of an array or sections. You can see what section a Vanilla page belongs to by inspecting the body tag using your browser tools and checking its CSS class. There will be an Section-* class, where * is the current section.</p><h3 data-id="example-3">Example</h3><p>This example checks whether the user is in either the Profile, Conversation or ActivityList section and if so, adds the panel asset.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">{if InSection(array('Profile', 'Conversation', 'ActivityList'))} {asset name="Panel"} {/if} </pre><h2 data-id="conditional%3A-incategory">Conditional: InCategory</h2><p>Given a category’s url code, returns true if the user is on a page in that category. You can manage the url codes of your forum’s categories from the Manage Categories page in the dashboard.</p><h3 data-id="example-4">Example</h3><p>This example prints out a message if the user is browsing a page in a category with the url code ‘general-category’.</p><pre class="code codeBlock" spellcheck="false" tabindex="0">{if InCategory('general-category')} <p>You're checking out content in the General category!</p> {/if} </pre><h2 data-id="conditional%3A-getvalue">Conditional: GetValue</h2><p>Given an array key or property name, returns its value from an associative array or an object.</p><h3 data-id="example-5">Example</h3><p>See the <a href="https://success.vanillaforums.com/kb/articles/234-smarty-conditionals#conditional%3A-category" rel="nofollow noreferrer ugc">Category example</a>.</p><h2 data-id="other-conditional-functions">Other conditional functions</h2><p>There are a few other (less useful) functions that can be used in conditional statements in Vanilla’s Smarty implementation. These are MultiCheckPermission, SetValue and Url. These function declarations are in <code class="code codeInline" spellcheck="false" tabindex="0">/library/core/functions.general.php</code>. To understand how to use these functions, refer to the source code there.</p> </article> </main>