If homepage, show different title

So when making a new website in resources context I always give the website a name. It can be the brandname or my personal name. It’s the name I want to see behind every page title. Like
Pagetitle - Sitename.

But I do not want the pagename to be shown on the homepage like: ‘Home - Sitename’
Here’s how I fixed it. If you have another solution, let me know.

<title>
[[*id:eq=`[[++site_start]]`:then=`[[++site_name]]`:else=`[[*pagetitle]] | [[++site_name]]`]]
</title>
3 Likes

Nice tip.
Thanks for the share.

2 Likes
<title>
[[*id:isnot=`1`:then=`[[*pagetitle]] |`]] [[++site_name]]
</title>
1 Like

I would do this with a short snippet (conditional output modifiers take a long time to parse):

<title>
[[!GetTitle]]
</title>
/* GetTitle snippet */
$siteName = $modx->getOption('site_name');
if ($modx->resource->get('id') == $modx->getOption('site_start')) {
    return $siteName;
}  else {
    return $modx->resource->get('pagetitle') . ' | ' . $siteName;
}

It could also be done with a single line:

return ($modx->resource->get('id') == $modx->getOption('site_start')) ? $modx->getOption('site_name') : $modx->resource->get('pagetitle') . ' | ' . $modx->getOption('site_name');
2 Likes

Have you done any (semi-scientific) performance testing to reach the conclusion a snippet would be faster here?

I noticed you said something along the same lines in another thread. While too many output filters are not great, snippets are not without their overhead either, so for a simple conditional like the one in this thread I’d be interested in some data to back up what is the fastest approach.

(Of course you’d want that call to be cached regardless of snippet/output filter, or you’re throwing away all performance benefit)

1 Like

It’s a good question. I can’t really think of a good way to test it, but I say that for two reasons.

First, the output modifier has to recognize that there are multiple output modifiers. Then parse the full tag. It has to find out which modifier to call (which is basically a snippet call). It has to parse the outer tag, then parse all the inner tags (figuring out what kind of tags they are first). It has to call getOption() for all the System Settings, and get() for the others. Then it has to figure out that it’s an if/then/else structure that’s needed to “solve” the modifier (as opposed to all the other structures that might be requires) and then execute the code. I haven’t looked at it but I’m pretty sure the output modifier parsing code is fairly complex.

The snippet already knows the structure, which variables will be needed, and has essentially no parsing or deciding to do other than the single if statement.

Second, OpenGeek has written more than once about how slow conditional output modifiers are compared to snippets.

It’s quite possible, though that the time differences are trivial. I guess you could use dev. tools profiler to measure the comparative page load times, cached and uncached.

2 Likes