ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

Template If - Else

July 23, 2011 6:12am

Subscribe [3]
  • #1 / Jul 23, 2011 6:12am

    Guddu

    11 posts

    Hi All,

    I want to evaluate a variable and if the variable is set then I want to use that as the Title and if the Variable is not Set then i want to supply a default value to the variable.

    Something like this

    If A != ‘’
      Title = A
    Else:
      Title = ‘Default Title’
    End If

    How do I write this piece of code in Tenmplate Language?

    Please guide.

    Regards,
    Guddu

  • #2 / Jul 23, 2011 11:36am

    Mark Bowen

    12637 posts

    You’re actually very close with your pseudo code there.

    Try this :

    {if my_variable != ""}
    {my_variable}
    {if:else}
    Default Title
    {/if}

    Hope that helps a bit.

    Best wishes,

    Mark

  • #3 / Jul 23, 2011 2:16pm

    Guddu

    11 posts

    Thanks for the response Mark

    I did something like this but it is not working. Am I missing something here?

    {if {page_title} != “”}
          <title>{embed:page_title}</title>
    {if:else}
          <title>Default Title</title>
    {/if}


    The title getting displayed is always “Default Title”

    Please Guide.

    Regards,
    Guddu

  • #4 / Jul 27, 2011 9:46pm

    smartpill

    456 posts

    Thanks for the response Mark

    I did something like this but it is not working. Am I missing something here?

    {if {page_title} != “”}
          <title>{embed:page_title}</title>
    {if:else}
          <title>Default Title</title>
    {/if}


    The title getting displayed is always “Default Title”

    Please Guide.

    Regards,
    Guddu

    You have the variable as an embed in one place and not an embed in the other. Plus I think you need to quote the variable if you use the {if:else} conditional. Try:

    {if "{embed:page_title}" != ''}
          <title>{embed:page_title}</title>
    {if:else}
          <title>Default Title</title>
    {/if}
.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases