Page 1 of 1

[solved] Override {title} in <title> element if {description} has content?

Posted: Mon Dec 06, 2010 1:15 pm
by Jonny
I'm seeking a simple way to override {title} within the element of pages.

At present I have:

{title} | {sitename}

On some pages I wish to tweak titles whilst retaining the short page name in the admin pages list. I could, of course, assign a separate template using {description} in place of {title} and enter the title in the "Description (title attribute)" field in the page options. But, if possible, I'd rather use a single template.

Would it be possible to create a user defined tag that would get {description} only if it had content, else get {title}?

Re: Override {title} in <title> element if {description} has content?

Posted: Mon Dec 06, 2010 2:53 pm
by Wishbone

Code: Select all

{if isset($description)}
<title>{$description} | {sitename}</title>
{else}
<title>{title} | {sitename}</title>
{/if}

Re: Override {title} in <title> element if {description} has content?

Posted: Mon Dec 06, 2010 3:18 pm
by Wishbone
I was assuming that {description} was a smarty variable, not a smarty function, so it won't work... Search around, or look at the docs on the Smarty site to see how to capture the output of {description} to a variable, then do the above.

Re: Override {title} in <title> element if {description} has content?

Posted: Mon Dec 06, 2010 3:22 pm
by RonnyK
{capture assign=somevariable}{description}{/capture}
{if isset($somevariable)}
{$somevariable} | {sitename}
{else}
{title} | {sitename}
{/if}

Re: Override {title} in <title> element if {description} has content?

Posted: Mon Dec 06, 2010 6:24 pm
by Jonny
RonnyK wrote: {capture assign=somevariable}{description}{/capture}
{if isset($somevariable)}
{$somevariable} | {sitename}
{else}
{title} | {sitename}
{/if}
Thanks for both replies. I've tried your code, Ronny, both in my template and the Smarty data field within the page options.

When a description has been entered it is displayed. When the description field is empty the {title} is not displayed - only the {sitename}.

With no description entered, I modified the {else} fallback, to say

sometext | {sitename}

"sometext" was not displayed, which led me to think that the empty {$somevariable} was still being parsed. I switched to this, which seems to work:

Code: Select all

{capture assign=somevariable}{description}{/capture} 
{if $somevariable !=""}
<title>{$somevariable} | {sitename}</title>
{else}
<title>{title} | {sitename}</title>
{/if}
Is this likely to cause any problems? I must find time to brush up on the Smarty stuff!

Re: Override {title} in <title> element if {description} has content?

Posted: Mon Dec 06, 2010 6:27 pm
by Wishbone
It seems like the capture assigns an empty string if there is no {description}. It still is set, but empty, so "isset" isn't working as expected. If what you did works, then it should be good.

Re: Override {title} in <title> element if {description} has content?

Posted: Mon Dec 06, 2010 7:30 pm
by Jonny
OK, thanks, marked as [solved] then.