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

For questions and problems with the CMS core. This board is NOT for any 3rd party modules, addons, PHP scripts or anything NOT distributed with the CMS made simple package itself.
Post Reply
Jonny
Forum Members
Forum Members
Posts: 77
Joined: Sun Sep 24, 2006 10:49 am

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

Post 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}?
Last edited by Jonny on Mon Dec 06, 2010 7:30 pm, edited 1 time in total.
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

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

Post by Wishbone »

Code: Select all

{if isset($description)}
<title>{$description} | {sitename}</title>
{else}
<title>{title} | {sitename}</title>
{/if}
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

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

Post 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.
RonnyK
Support Guru
Support Guru
Posts: 4962
Joined: Wed Oct 25, 2006 8:29 pm

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

Post by RonnyK »

{capture assign=somevariable}{description}{/capture}
{if isset($somevariable)}
{$somevariable} | {sitename}
{else}
{title} | {sitename}
{/if}
Jonny
Forum Members
Forum Members
Posts: 77
Joined: Sun Sep 24, 2006 10:49 am

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

Post 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!
Wishbone
Power Poster
Power Poster
Posts: 1368
Joined: Tue Dec 23, 2008 8:39 pm

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

Post 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.
Jonny
Forum Members
Forum Members
Posts: 77
Joined: Sun Sep 24, 2006 10:49 am

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

Post by Jonny »

OK, thanks, marked as [solved] then.
Post Reply

Return to “CMSMS Core”