Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2012-06-06 09:33:40

ff0000.it
New Member
From: Italy
Registered: 2012-03-14
Posts: 9

How to add an attribute to *_list tags to output items count?

Hi all,

I’m new to TXP and while playing around to produce this complete file list:

 <txp:smd_each type="fixed"
               include='<txp:category_list type="file"
                                           children="0"
                                           break=","
                        ><txp:category />:1</txp:category_list>'>
 <h2 class="file-download-list-category">
  {smd_var_name}
 </h2>
 <txp:file_download_list category="{smd_var_name}"
                         limit="9999"
                         wraptag="ul"
                         break="li"
                         class="file-download-list" />
 </txp:smd_each>

I met a problem: if a category has no files It’s displayed in any case…
So, first idea was to add an attribute to “file_download_list” tag in
order to output just the files count (the same way attribute “pgonly” works
on “article” and “article_custom tags”).

How could I achieve that? Or is there a best way to do this?

Cheers,
ff0000.it

Offline

#2 2012-06-06 10:49:48

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

Re: How to add an attribute to *_list tags to output items count?

Maybe replace


 <h2 class="file-download-list-category">
  {smd_var_name}
 </h2>
 <txp:file_download_list category="{smd_var_name}"
                         limit="9999"
                         wraptag="ul"
                         break="li"
                         class="file-download-list" />

by

 <txp:file_download_list category="{smd_var_name}"
                         lablel="{smd_var_name}"
                         lableltag="h2"
                         limit="9999"
                         wraptag="ul"
                         break="li"
                         class="file-download-list" />

Offline

#3 2012-06-06 11:24:44

ff0000.it
New Member
From: Italy
Registered: 2012-03-14
Posts: 9

Re: How to add an attribute to *_list tags to output items count?

Hi etc,

thanks for the hint, It works fine… my final version is:

<div class="file-download-list">
  <txp:smd_each type="fixed"
                include='<txp:category_list type="file"
                                            children="0"
                                            break=","
                         ><txp:category />:1</txp:category_list>'>
  <txp:file_download_list category="{smd_var_name}" 
                          labeltag="h2"
                          label='<txp:category name="{smd_var_name}"
                                               type="file"
                                               title="1"
                                               link="0"
                                               url="0" />'
                          limit="9999"
                          wraptag="ul"
                          break="li">
   <a class="file-download-list-link"
      href="<txp:file_download_link />"
      title="<txp:file_download_name />">
    <txp:file_download_description />
   </a>
   <span class="file-download-list-size">
    [<txp:file_download_size decimals="0" 
                             format="kb" />]
   </span>
  </txp:file_download_list>
  </txp:smd_each>
 </div>

The only issue is that I can’t set the label class :-/… if I don’t use It and try a wraptag solution as:

<txp:file_download_list category="{smd_var_name}" 
                          label='<txp:category name="{smd_var_name}"
                                               type="file"
                                               title="1"
                                               link="0"
                                               url="0" 
                                               wraptag="h2"
                                               class="file-download-list-category" />'
                          limit="9999"
                          wraptag="ul"
                          break="li">

It puts a ‘<br />’ between the label and the list… I can’t understand why TXP inserts those ‘<br />’.

Anyway, thanks for helping me through this :-),
ff0000.it

Offline

#4 2012-06-06 12:38:43

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

Re: How to add an attribute to *_list tags to output items count?

Glad to help. If you set the class for styling, why not use .file-download-list>h2 selector? And I do not know how smd_each works, but why not use category_list itself as container:

<div class="file-download-list">
  <txp:category_list type="file" children="0">
    <txp:file_download_list category='<txp:category />' 
                          labeltag="h2"
                          label='<txp:category 
                                               type="file"
                                               title="1"
                                               link="0"
                                               url="0" />'
                          limit="9999"
                          wraptag="ul"
                          break="li">
     <a class="file-download-list-link"
        href="<txp:file_download_link />"
        title="<txp:file_download_name />">
      <txp:file_download_description />
     </a>
     <span class="file-download-list-size">
      [<txp:file_download_size decimals="0" 
                             format="kb" />]
     </span>
    </txp:file_download_list>
  </txp:category_list>
 </div>

Offline

#5 2012-06-06 12:42:48

uli
Moderator
From: Cologne
Registered: 2006-08-15
Posts: 4,304

Re: How to add an attribute to *_list tags to output items count?

If you still need to display a file count here’s a snippet of PHP that does so:
<txp:php>echo safe_count("txp_file", "category = 'your_category'");</txp:php>


In bad weather I never leave home without wet_plugout, smd_where_used and adi_form_links

Offline

#6 2012-06-06 12:51:49

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

Re: How to add an attribute to *_list tags to output items count?

Hi uli,

where ‘your_category’ will come from inside a category list?

Offline

#7 2012-06-07 08:04:32

ff0000.it
New Member
From: Italy
Registered: 2012-03-14
Posts: 9

Re: How to add an attribute to *_list tags to output items count?

Hi all,

finally I’ve found a working solution merging your suggestions :-)

 <div class="file-download-list">
  <txp:smd_each type="fixed"
                include='<txp:category_list type="file"
                                            children="0"
                                            break=","
                         ><txp:category />:1</txp:category_list>'>
  <txp:php>
   global $_sc;
   $_sc = safe_count("txp_file", 
                     "category = '{smd_var_name}'");
  </txp:php>
  <txp:smd_if field="phpvar:_sc" 
              operator="gt:NUM"
              value="0">
  <h2 class="file-download-list-category">
   <txp:category name="{smd_var_name}"
                 type="file"
                 title="1" />
  </h2>
  <txp:file_download_list category="{smd_var_name}" 
                          limit="9999"
                          wraptag="ul"
                          break="li">
   <a class="file-download-list-link"
      href="<txp:file_download_link />"
      title="<txp:file_download_name />">
    <txp:file_download_description />
   </a>
   <span class="file-download-list-size">
    [<txp:file_download_size decimals="0" 
                             format="kb" />]
   </span>
   </txp:file_download_list>
  </txp:smd_if>
  </txp:smd_each>
 </div>

… and It works as expected… It seems like I’m addicted to smd_* plugins :-)

Thanks and see you,
ff0000.it

Offline

#8 2012-06-07 11:09:09

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: How to add an attribute to *_list tags to output items count?

Most of that seems very much unneeded. If I’m guessing correctly, you are using those smd plugin’s just for their own curly tag parsing. Which you don’t need. Using those curly tags can actually cause security issues as they are injected to the markup prior to actual markup parsing, but that whole other story.

You can use use normal tags in tag tag attributes. See Parsing Tag Attributes article. Textpattern also comes it own set of variable and conditional tags, variable and if_variable which can do comparisons and store values.

As the SQL query in the PHP goes, it would best if the category name was escaped properly with Textpattern’s doSlash() function before used in a query, to prevent (accidental) SQL injections.

In other words;

<div class="file-download-list">
<txp:category_list type="file" children="0" break="">
	<txp:php>
		global $variable;
		$variable['file_count'] = safe_count('txp_file', "category='".doSlash(category(array()))."'");
	</txp:php>

	<txp:if_variable name="file_count" value="0">
	<txp:else />
		<h2 class="file-download-list-category"><txp:category type="title" /></h2>

		<txp:file_download_list  category='<txp:category />' limit="9999" wraptag="ul" break="li">
			<a class="file-download-list-link" href="<txp:file_download_link />" title="<txp:file_download_name />">
				<txp:file_download_description />
			</a>
			<span class="file-download-list-size">
				[<txp:file_download_size decimals="0" format="kb" />]
			</span>
		</txp:file_download_list>
	</txp:if_variable>
</txp:category_list>
</div>

Last edited by Gocom (2012-06-07 11:12:29)

Offline

#9 2012-06-07 22:21:51

ff0000.it
New Member
From: Italy
Registered: 2012-03-14
Posts: 9

Re: How to add an attribute to *_list tags to output items count?

Hi Gocom,

Being not familiar with TXP’s tags system take me to have more confidence with
smd_{if,each} plugins that reflect a programmer’s perspective (conditional if,
“for each” looping), but your solution works like a charm :-)
There’s only a part I don’t understand:

$variable['file_count'] = safe_count('txp_file', "category='" . doSlash(category(array())) . "'");

what’s the job of the doSlash(...) part? Or better where in the sources
is category()?
Anyway I learn something new: global $variable[] refers to txp:variable
tag… Glad to know that! ;-)

I’m using TXP in a few months and I really don’t understand why It’s not
massively followed such as mainstream CMSes; It’s simple, clean, flexible and
It has a fast admin side… who knows? :-)

Gocom thanks for helping me on this one.

Cheers,
ff0000.it

Offline

#10 2012-06-08 02:00:11

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: How to add an attribute to *_list tags to output items count?

ff0000.it wrote:

what’s the job of the doSlash(…) part?

Quoting my previous post: As the SQL query in the PHP goes, it would best if the category name was escaped properly with Textpattern’s doSlash() function before used in a query, to prevent (accidental) SQL injections. This is for (mandatory) security.

Or better where in the sources is category()?

Every Textpattern tag has a matching function. The mentioned category() is <txp:category />, called from PHP.

Offline

#11 2012-06-08 10:54:11

ff0000.it
New Member
From: Italy
Registered: 2012-03-14
Posts: 9

Re: How to add an attribute to *_list tags to output items count?

Hi Gocom,

thanks again for the explanations :-)

Cheers,
ff0000.it

Offline

Board footer

Powered by FluxBB