Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
How do I detect the page that the section is using?
Is there a way I can detect which page a section is using?
So for example, if I wanted an article form to output some additional info in a particular section it would be nice to use something like:
<txp:if_section_page name=“blog”>
Is there a way to do something like this?
Offline
#2 2011-09-10 10:45:23
- redbot
- Plugin Author
- Registered: 2006-02-14
- Posts: 1,410
Re: How do I detect the page that the section is using?
douglgm wrote:
…<txp:if_section_page name=“blog”>
Is there a way to do something like this?
Do I miss something? Why can’t you just use the standard <txp:if_section name=“blog”> tag?
Offline
Re: How do I detect the page that the section is using?
The reason I’m trying to do this is that I’m building a foundation template for future sites. I can’t know the names of the sections in advance and I don’t want to have to modify forms with lists of sections when I can just detect the page that’s being used for them.
I want the forms to react to the page template I apply to the section, not the section itself. If I (or a client) changes the page type it would be nice for forms to respond to this.
At the moment I’m using a variable at the top of each page to set the page name. (blog, static, fullwidth) etc. I just wondered if there was a way I can use the page type directly.
Does that make any sense?
Offline
Re: How do I detect the page that the section is using?
While I don’t really understand the advantage of knowing the page templates name, defining set of variable is the best option you have, isn’t? I would keep doing what are doing now. For example, what happens if the user changes the name of the template? Yeah, the conditionals will stop working. But when you statistically set the name, changing the name of the template doesn’t effect anything.
Anyway, if you want to dynamically fetch the templates name, you could use page_url. Despite the tag’s name, it can return current page template’s name among other non-URL related things. I.e.
<txp:variable name="template" value='<txp:page_url type="page" />' />
And then checking as normally would:
<txp:if_variable name="template" value="default">
Default page template.
<txp:else />
Something else than default.
</txp:if_variable>
Last edited by Gocom (2011-09-10 12:35:25)
Offline
Re: How do I detect the page that the section is using?
Anyway, if you want to dynamically fetch the templates name, you could use page_url. Despite the tag’s name, it can return current page template’s name among other non-URL related things. I.e.
<txp:variable name=“template” value=’<txp:page_url type=“page” />’ />
Cool. Aside from not knowing quite what to use it for, I didn’t know that. Looking at the function it can return all types of $pretext. I found some $pretext values here, all of which have a corresponding txp tag, but page
is not among them. What other ones are there that could be useful?
TXP Builders – finely-crafted code, design and txp
Offline
Re: How do I detect the page that the section is using?
jakob wrote:
What other ones are there that could be useful?
On a non-error page the tag can return following values:
array(
[id] =>
[s] => default
[c] =>
[context] => article
[q] =>
[m] =>
[pg] =>
[p] =>
[month] =>
[author] =>
[request_uri] => /
[qs] =>
[subpath] => \/
[req] => /
[status] => 200
[page] => default
[css] => default
[path_from_root] => /
[pfr] => /
[path_to_site] => /home/example.com/public_html
[permlink_mode] => section_title
[sitename] => My Site
[secondpass] =>
);
On error pages not all are populated. For example page
isn’t available on an error page. From those context
, pg
, month
, author
, status
, request_uri
, permlink_mode
, path_to_site
, path_from_root
, page
and css
can be useful. Others, likely not, especially as for the rest there are dedicated tags.
Last edited by Gocom (2011-09-10 13:46:01)
Offline
Re: How do I detect the page that the section is using?
Many thanks Gocom, that’s exactly what I needed.
The reason for using the page template as a conditional is that my set of standard templates are less likely to change than the names/number of sections that a customer’s site might require.
Thanks again.
Offline
#8 2011-09-10 14:53:27
- uli
- Moderator
- From: Cologne
- Registered: 2006-08-15
- Posts: 4,304
Re: How do I detect the page that the section is using?
Go test them all for yourself:
id = <txp:page_url type="id" /><br />
s = <txp:page_url type="s" /><br />
c = <txp:page_url type="c" /><br />
context = <txp:page_url type="context" /><br />
q = <txp:page_url type="q" /><br />
m = <txp:page_url type="m" /><br />
pg = <txp:page_url type="pg" /><br />
p = <txp:page_url type="p" /><br />
month = <txp:page_url type="month" /><br />
author = <txp:page_url type="author" /><br />
request_uri = <txp:page_url type="request_uri" /><br />
qs = <txp:page_url type="qs" /><br />
subpath = <txp:page_url type="subpath" /><br />
req = <txp:page_url type="req" /><br />
status = <txp:page_url type="status" /><br />
page = <txp:page_url type="page" /><br />
css = <txp:page_url type="css" /><br />
path_from_root = <txp:page_url type="path_from_root" /><br />
pfr = <txp:page_url type="pfr" /><br />
path_to_site = <txp:page_url type="path_to_site" /><br />
permlink_mode = <txp:page_url type="permlink_mode" /><br />
sitename = <txp:page_url type="sitename" /><br />
secondpass = <txp:page_url type="secondpass" /><br />
page = <txp:page_url type="page" />
Thanks, Jukka! Can turn out to be quite handy!
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Offline
Re: How do I detect the page that the section is using?
Something for the docs?
Offline
#11 2011-09-10 15:08:28
- uli
- Moderator
- From: Cologne
- Registered: 2006-08-15
- Posts: 4,304
Re: How do I detect the page that the section is using?
Not as nicely formatted but supplies even more info I might find valuable later on: prev/next_id etc. Coool :)
In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links
Offline
Re: How do I detect the page that the section is using?
douglgm wrote:
Something for the docs?
I don’t know if it is good to mention any of this in the docs. The behavior seems to be a kind of side product, just like that the custom_field tag can return any article field’s raw output.
Not all the values are necessarily “secured”, some of them are truly just raw input and not all them are available in every context, some which might return inconsistent values, and could be modified by plugins.
When you made the question, and I wrote my reply, I did seriously thought about updating the page_url’s page, but I’m not just sure if any of this should be mentioned there. As it’s part of low-level core, any of it could be gone the next day. Some of the items in the pretext are not really even used, and could be considered deprecated.
uli wrote:
Not as nicely formatted
Hit browser’s view source, or try to add print('<pre>');
and print('</pre>');
around it. It’s actually pretty nicely intended and all that ;-)
but supplies even more info I might find valuable later on: prev/next_id etc. Coool :)
Yep. $pretext
contains different values based on the contexts. When returning dynamic list of values, you can accurately see what is available and where, and how the behaviors changes, and the list changes with core updates.
Last edited by Gocom (2011-09-10 15:26:54)
Offline