Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2014-11-11 10:44:27

dorka
Member
Registered: 2012-10-04
Posts: 90

How do I wrap articles in a list in separate html tags by category?

Hello, my txp guard angels :-),

here is my code:

<txp:if_article_list>
   <h1><txp:section title="1" /></h1>
       <txp:article status="5" sort="Category1 asc, Title desc" limit="999" form='portfolio_form' />
<txp:else />
        <txp:article status="5" limit="999" form='<txp:section/>_article'/>
</txp:if_article_list>

Here is the content of “portfolio_form”:

<txp:if_different>
      <div class="cat_title">
         <h2 ><txp:category1 title="1" /></h3>
      </div>
</txp:if_different>
<div class="article">
     <a href="<txp:permlink />" itemprop="url">
          <div class="video">
               <txp:article_image width="0"/>    
               <h6><small ><txp:title /></small></h6>
          </div>
     </a>
</div>

What I want to achieve is wrapping the list of articles in each category in a separate html tag. How does one do that in text pattern!?
Thanks

Offline

#2 2014-11-11 12:13:50

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

Re: How do I wrap articles in a list in separate html tags by category?

It’s not easy with native tags, but how about this hacked-together approach:

Page:

<txp:if_article_list>
   <h1><txp:section title="1" /></h1>
   <txp:variable name="first_item" value="1" />
   <txp:article status="5" sort="Category1 asc, Title desc" limit="999" form='portfolio_form' />
   </div><!-- /last article_list -->
<txp:else />
   <txp:article status="5" limit="999" form='<txp:section/>_article'/>
</txp:if_article_list>

Form:

<txp:variable name="cat_title"><txp:if_different><txp:category1 title="1" /><txp:else /></txp:if_different></txp:variable>

<txp:if_variable name="cat_title" value="">
   <txp:if_variable name="first_item" value="1">
      <div class="article_list">
   </txp:if_variable>
   <txp:variable name="catlist" value="0" />
<txp:else />
   <txp:if_variable name="first_item" value="0">
      </div><!-- /article_list -->
   </txp:if_variable>

   <div class="cat_title">
      <h2><txp:variable name="cat_title" /></h2>
   </div>
   <txp:variable name="catlist" value="1" />
</txp:if_variable>

<txp:variable name="first_item" value="0" />

<txp:if_variable name="catlist" value="1">
   <div class="article_list">
   <txp:variable name="catlist" value="0" />
</txp:if_variable>

<div class="article">
     <a href="<txp:permlink />" itemprop="url">
          <div class="video">
               <txp:article_image width="0"/>    
               <h6><small ><txp:title /></small></h6>
          </div>
     </a>
</div>

It may be able to simplify it further with a little rejigging, I haven’t tried. With something like smd_if, it might be simpler still as you could maybe roll some of the tests into an ‘or’ condition to simplify things.

This solution is based on flip-flopping flags, a bit like a state machine might work, but there are edge cases such as the start and end of the lists which need special dispensation, hence the extra <txp:variable> and <txp:if_variable> shenanigans. I didn’t bother building in any extra logic for the very end of the list, just assumed that there would be a list and hard-coded it in the Page template. If that’s not the case, you may need some extra logic to conditionally display the final closing div.

Alternatively you could probably use etc_query to retrospectively wrap the contents in one line of plugin awesomeness, but I’ll leave that to etc to explain :-)


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

#3 2014-11-11 15:15:39

dorka
Member
Registered: 2012-10-04
Posts: 90

Re: How do I wrap articles in a list in separate html tags by category?

Bloke,
you are genius! I took you an hour to answer and it took me 2.5 hours to understand what you have done. I got it finally! :-) I could’ve cut&paste and go on of course, but it just doesn’t feel right!
Thanks a lot and have a nice day.

Offline

#4 2014-11-11 15:43:59

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

Re: How do I wrap articles in a list in separate html tags by category?

dorka wrote #285674:

took you an hour to answer and it took me 2.5 hours to understand what you have done.

Hehe. Just glad it worked. As I say, there’s probably a way to optimise it or use a plugin to make it clearer what’s going on. That was just the first approach off the top of my head.


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

#5 2014-11-11 17:30:24

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

Re: How do I wrap articles in a list in separate html tags by category?

That’s more a curiosity, but it’s shorter, indeed:

<txp:etc_query data='<txp:article status="5" sort="Category1 asc, Title desc" limit="999" form="portfolio_form" />'
	query="div[@class='cat_title']"
>
	{.@@data-count=1}
	<div class="shadow-box">
		{following-sibling::div[@class='article'][preceding-sibling::div[@class='cat_title'][1]/@data-count]}
	</div>
</txp:etc_query>

This will leave your <div class="cat_title" /> with data-count="1" attribute, but it should be harmless.

Offline

Board footer

Powered by FluxBB