Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2006-09-07 09:32:53

dingoboy
Member
Registered: 2006-09-07
Posts: 48

How to echo txp section with PHP?

My problem:

How do I echo the equal to Textpattern’s <txp:section /> with PHP?
I’ve searched and read all I could find on the forum, and I tried a few things – which failed to work.

Please help me out here ASAP! :-)

Details:

The reason I need this PHP echo of a txp section is that I’m not able to use txp tags within txp tags.
Example: <txp:linklist category"<txp:section />_submenu" /> (the tag is closed off too soon with the first />).

I want Textpattern to automatically spit out submenus, prefixed with the current section name.

Example of miserably failing attempt to echo the section name in PHP:

<code>
<txp:php>
echo $atts[‘section’];
</txp:php>
</code>

(Doesn’t do the trick).

Last edited by dingoboy (2006-09-07 09:47:38)

Offline

#2 2006-09-07 09:47:25

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: How to echo txp section with PHP?

Probably something like this (not tested):
<del><code><txp:php>echo section(array());</txp:php></code></del>

This only makes sense if you want to use the section as an attribute in another TXP tag… in other cases, the txp:section tag is much easier.

Last edited by ruud (2006-09-07 10:12:06)

Offline

#3 2006-09-07 09:56:28

dingoboy
Member
Registered: 2006-09-07
Posts: 48

Re: How to echo txp section with PHP?

Thanks ruud.

Your code example works fine, when placed outside the relevant txp tag — it echoes the section name, as supposed. But when I place your code within my txp tag, it fails. – Weird!

This is my actual code, containing your solution:

<code>
<ul>
<txp:linklist category=”<txp:php>echo section(array());</txp:php>_submenu” form=“page_menu” />
</ul>

</code>

This returns nothing. Any suggestions?

I should note that when I type in the prefix manually, everything works fine, but that doesn’t solve my problem, sadly…

Last edited by dingoboy (2006-09-07 10:21:55)

Offline

#4 2006-09-07 10:03:37

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: How to echo txp section with PHP?

My fault. You can’t use TXP tags (including txp:php) as an attribute for another TXP tag. Try this:
<del><code><?php echo section(array()); ?></code> as an attribute</del>

or, to avoid using the raw php opening/close tags:
<code><txp:php>echo linklist(array(‘category’ => section(array()).’_submenu’, ‘form’ => ‘page_menu’));</txp:php></code>

Last edited by ruud (2006-09-07 10:13:00)

Offline

#5 2006-09-07 10:09:07

dingoboy
Member
Registered: 2006-09-07
Posts: 48

Re: How to echo txp section with PHP?

ruud wrote:

My fault. You can’t use TXP tags (including txp:php) as an attribute for another TXP tag. Try this:
<code><?php echo section(array()); ?></code>

Same result: Works outside the category attribute of the txp tag, but not within.

My updated code:
<txp:linklist category="<?php echo section(array()); ?>_submenu" form="page_menu" />

Offline

#6 2006-09-07 10:14:25

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: How to echo txp section with PHP?

See second suggestion in my second post. Third time is a charm ;)

Offline

#7 2006-09-07 10:17:10

dingoboy
Member
Registered: 2006-09-07
Posts: 48

Re: How to echo txp section with PHP?

Thanks ruud, you’re the best! :-)

The problem was solved using your last, updated code-example.

It’s kind of buggy that you can’t use txp tags in parent txp tags. It would rule to be able to escape the nested txp tag, so the parent tag isn’t closed at the first random occurence of "/>". (Which I assume our competent developers have already considered).

Anyway, I’ll live just fine with this solution. Thanks again.

Last edited by dingoboy (2006-09-07 10:19:27)

Offline

#8 2006-09-07 10:57:30

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,323
Website Mastodon

Re: How to echo txp section with PHP?

Post pimp. Looks like I’ve acquired a bad habit ;-)

Offline

#9 2006-09-09 14:32:11

Dragondz
Moderator
From: Algérie
Registered: 2005-06-12
Posts: 1,529
Website GitHub Twitter

Re: How to echo txp section with PHP?

More I discover from Txp more I love it!

Thanks wet and ruud the problem asked by dingoboy is the same that I have, then thanks both.

Offline

#10 2006-09-21 00:18:42

mwillse
Member
From: brooklyn, ny
Registered: 2006-06-26
Posts: 83
Website

Re: How to echo txp section with PHP?

For any non-PHP folks out there looking to do something similar. Here’s what’s happening as I understand it. I’m not a coder so this is in plain language and might be slightly inaccurate. But I’ve adapted their code without any trouble.

If you want to identify the current section (or another attribute), and then use it in your txp tag, you have to turn the whole tag into php. You can’t embed one in the other.

So, your fantasy to do this:

<txp:article_custom section="CURRENT-SECTION" form="your-form" />

Becomes this instead:

<txp:php>echo article_custom(array('section' => section(array()), 'form' => 'your-form'));</txp:php>

  • <txp:php> :: tell txp you’re going to use php
  • echo :: this writes the results of what follows
  • article_custom( :: this is the txp tag you would have used if you weren’t trying to be so fancy )
  • array( :: says we going to list article_custom’s attributes )
  • 'form' => 'your-form', :: just like form=“your-form”
  • 'limit' => '5', :: you can add a limit or other attributes
  • 'section' => 'name', :: if you wanted to name the section, but instead…
  • 'section' => section(array()), :: grabs the list of current sections. since there is only one, then section=“CURRENT-SECTION

careful with parenthesis and commas!
they’re nested like so:

tag_name(
array(
‘attribute’ => ‘variable’,
‘attribute’ => attribute-to-lookup(array()),
‘attribute’ => ‘variable’,
)
)

dingoboy uses his current section to restrict articles to a particular category. which is why he has 'category' => section(array()).

he added the text “_submenu” to his section name with this: .'_submenu' after the array like so: 'category' => section(array()).'_submenu',. that way, he’ll get category=“CURRENT-SECTION_submenu”.

you could put anything there
'category' => section(array()).'-any_thing' to get category=“CURRENT-SECTION-any_thing.

let me know if any of this is wrong and needs to be edited!
thanks dingoboy and ruud!

Offline

#11 2006-09-21 04:35:04

wet
Developer Emeritus
From: Schoerfling, Austria
Registered: 2005-06-06
Posts: 3,323
Website Mastodon

Re: How to echo txp section with PHP?

In case you wanted to avoid the use of PHP for tags as attributes at all, see Sencer’s plugin asy_wondertags.

Offline

Board footer

Powered by FluxBB