Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2015-03-05 16:13:40

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Check for homepage (not category list, search list, author list)

Hi there,

Is there a way to use conditional tags to check if this is the homepage and ONLY the homepage. i.e., not a category list, search result, etc.

If I use…

<txp:if_section name="">
   This is homepage only
</txp:if_section>

…then that is also output if the context is a category listing and suchlike too, which I don’t want.

I know I could nest in a load of conditionals like this but seems horribly clunky and a waste of resources:

<txp:if_search>
<txp:else />
    <txp:if_category>
    <txp:else />
        <txp:if_author>
        <txp:else />
            This is homepage only
        </txp:if_author>
    </txp:if_category>
</txp:if_search>

Or maybe another angle, could I have a section for homepage and another section for category listings and suchlike? I’m sure I’m missing something obvious here.

Offline

#2 2015-03-05 17:27:36

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: Check for homepage (not category list, search list, author list)

Hi phil

There is of course this plugin which does what you need but if you need to only use native tags, I am wondering if page_url could somehow be put in a variable so as to detect if the page is actually the front one. Something like

<txp:variable name="front" value='<txp:page_url />'>
<txp:if_variable name="front" value='<txp:site_url />'>
You are on the front page
<txp:else />
You are not on the front page
</txp:if_variable>

I guess the only way that the code would work is if you have an htaccess rule to forward the www to a non www domain and the same goes for index.php. That’s all I can think of after 10 hours of meetings today.

> Edited to add that forward slashes might also cause the code to fail

Last edited by colak (2015-03-05 17:33:24)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#3 2015-03-05 17:50:04

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Check for homepage (not category list, search list, author list)

Thanks colak, great idea but that is too risky for me to use in production. I’ll resort to the nasty conditional nest as it’s probably more performant than the plugin which does much more than I need.

It’s a bit crazy that there is no core conditional tag to check whether the user is viewing homepage or not. :(

Offline

#4 2015-03-05 17:58:38

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Check for homepage (not category list, search list, author list)

Hmmm, thinking about it, I could use this hack…

<txp:if_search>
<txp:else />
    <txp:if_category>
    <txp:else />
        <txp:if_author>
        <txp:else />
            <txp:variable name="homepage" value="1" />
        </txp:if_author>
    </txp:if_category>
</txp:if_search>

Then every time I need to check if homepage I can use…

<txp:if_variable name="homepage">
    You are on the front page
<txp:else />
    You are not on the front page
</txp:if_variable>

At least I only have to call that large conditional nest once, and then I can just use the variable for other times I need that check in the default page template.

Obviously I’d include my first load of homepage conditional stuff along with loading the variable so as to optimise performance. Just for example purposes:

<txp:if_search>
    <body id="top" itemscope itemtype="http://schema.org/SearchResultsPage">
<txp:else />
    <txp:if_category>
        <body id="top" itemscope itemtype="http://schema.org/CollectionPage">
    <txp:else />
        <txp:if_author>
            <body id="top" itemscope itemtype="http://schema.org/CollectionPage">
        <txp:else />
            <body id="top" itemscope itemtype="http://schema.org/WebPage">
            <txp:variable name="homepage" value="1" />
        </txp:if_author>
    </txp:if_category>
</txp:if_search>

Offline

#5 2015-03-05 18:26:38

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,011
Website GitHub Mastodon Twitter

Re: Check for homepage (not category list, search list, author list)

That Is actually very good and looks that it will work!

I am wondering if a feature request could be posted for the page_url type to have a home value which could recognise the 3 conditions that the home url can have.

Woops:) you already did the request.

Last edited by colak (2015-03-05 19:39:28)


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#6 2015-03-05 20:51:41

michaelkpate
Moderator
From: Avon Park, FL
Registered: 2004-02-24
Posts: 1,379
Website GitHub Mastodon

Re: Check for homepage (not category list, search list, author list)

I actually posted something similar here in the forums a while back:

<txp:variable name="task" value="frontpage" />

<txp:if_category><txp:variable name="task" value="category" /></txp:if_category>

<txp:if_search><txp:variable name="task" value="search" /></txp:if_search>

<txp:if_author><txp:variable name="task" value="author" /></txp:if_author>

Offline

#7 2015-03-05 21:35:54

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Check for homepage (not category list, search list, author list)

michaelkpate wrote #288776:

I actually posted something similar here in the forums a while back

Nice! That’s a good solution as well.

Offline

#8 2015-03-05 23:57:50

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,271
Website GitHub

Re: Check for homepage (not category list, search list, author list)

colak wrote #288768:

I am wondering if page_url could somehow… detect if the page is actually the front one

There really ought to be a way to do this natively. It’s crazy to rely on a plugin for something so basic.

That said, one person’s definition of “front page” might well differ to another person’s, whereas the other states are very black and white. It’s definitely a category list, or an author list, or an image list, even though they may all be processed by the front page template.

Jeff’s plugin offers the option of treating the first page in a list as “front page”, for example. Purists might consider that’s merely a “landing page”, not necessarily “front” (as in “home page”) so it’s good he has it as an option to cater for both camps. But a similar core feature wouldn’t have that luxury. We need to make it black and white and define what exactly “front page” should mean.

If that requires a few new items added to page_url tag (“I’m on the first page of a list”, and “I’m actually on the home page and NOT in any list”, etc) so be it.

Thoughts?


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#9 2015-03-06 07:23:18

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Check for homepage (not category list, search list, author list)

Personally I think a homepage definition for this should just be the page at root domain not viewed in any other state such as list. Keep it simple – Jeff’s plugin can cater for any other more esoteric choices if a designer needs to harness that.

Offline

#10 2015-03-06 08:41:53

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Check for homepage (not category list, search list, author list)

You could provide flexible homepage definition replacing conditionals by shorter and faster (atm)

<txp:variable name="homepage" value='<txp:page_url type="q" /><txp:page_url type="c" /><txp:page_url type="author" /><txp:page_url type="s" />' />

<txp:if_variable name="homepage" value="">
	homepage
<txp:else />
	other
</txp:if_variable>
philwareham wrote #288772:

I’ll resort to the nasty conditional nest as it’s probably more performant than the plugin which does much more than I need.

That’s debatable, the following is much faster than the page_url combo above:

<txp:etc_query name="homepage" globals="pretext" data="{?q,c,author,s}" />

Offline

#11 2015-03-06 09:11:44

philwareham
Core designer
From: Haslemere, Surrey, UK
Registered: 2009-06-11
Posts: 3,564
Website GitHub Mastodon

Re: Check for homepage (not category list, search list, author list)

Hi Oleg, how would I use that query to show some text on a homepage, can you provide a usage example?

Offline

#12 2015-03-06 09:23:33

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Check for homepage (not category list, search list, author list)

Hi Phil, if you mean

<txp:etc_query name="homepage" globals="pretext" data="{?q,c,author,s}" />

it sets a <txp:variable name="homepage" />, empty if none of q,c,author,s (i.e. search, category, author, section) url parameters is specified. You use it in the usual if_variable way. I’m not sure for section, though, its value could be is default on the home page, but you could use the following:

<txp:variable name="homepage" value='<txp:page_url type="q" />-<txp:page_url type="c" />-<txp:page_url type="author" />-<txp:page_url type="s" />-<txp:page_url type="pg" />' />

<txp:if_variable name="homepage" value="---default-">
	homepage
<txp:else />
	something else
</txp:if_variable>

Anyway, we talk about like 0.0002s absolute difference, I only wanted to stress that core solutions are not always optimal.

Last edited by etc (2015-03-06 09:41:22)

Offline

Board footer

Powered by FluxBB