Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#76 2012-02-16 11:23:27

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,452
Website GitHub

Re: smd_wrap: conditionally wrap stuff with tags and labels

maniqui

One for the scrapbook / plugin examples! Brilliant, thanks.


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

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#77 2012-02-16 15:09:15

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

Re: smd_wrap: conditionally wrap stuff with tags and labels

If you do heavy regex stuff, like Textile parsing, consider/remember to cache the results. Such a task can have bigger hit to overall performance than you may expect.

Offline

#78 2012-02-16 15:54:48

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

Bloke a écrit:

Do you have an example please? I can then try and replicate what you’re seeing so I can investigate. Thanks.

I tried smd_macros with a vevent (hcalendar microformat).
My Macro definition :

<div class="vevent">
   <txp:smd_wrap transform="textile" trim="0" wraptag="h3" class="summary" item=" {title}" />
	<div class="meta meta-event">

		<p class="date_duration">			
			  <txp:smd_wrap label="Start" labeltag="strong" wraptag="abbr" class="dtstart"  attr="title|{start}" transform="date|%b %Y" item="{start}"/><br />
			  <txp:smd_wrap label="End" labeltag="strong" wraptag="abbr" class="dtend"  attr="title|{end}" transform="date|%b %Y" item="{end}"/>				
		</p>
		<p class="location">
          <a class="include" href='#<txp:smd_wrap transform="sanitize|url_title" item="{lieu}" />-hcard'>{lieu}</a>
		</p>
	</div>   
   <txp:smd_wrap transform="textile" wraptag="div" class="description">{description}</txp:smd_wrap>
</div> <!-- .vevent -->

And if i write (in write tab)

notextile.. <txp:vevent 
            lieu="Bloke's Home" 
            start="2012-02-30" 
            end="2012-03-01" 
            title="Textpattern Drinks"
            description="

Cool, fiesta _chez Bloke_ !!

            "/>

Résultat :

<div class="vevent">


	<div class="meta meta-event">

		<p class="date_duration">			
			  <br />

		</p>
		<p class="location">
          <a class="include" href='#-hcard'>Bloke's Home</a>
		</p>
	</div>   

</div> <!-- .vevent -->

{remplacment} tags are still considered empty.
With smd_wrap_all everything works.

Offline

#79 2012-02-16 16:20:20

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 12,452
Website GitHub

Re: smd_wrap: conditionally wrap stuff with tags and labels

sacripant

Thanks for the example.

With smd_wrap_all everything works.

Yes, it will. By default, <txp:smd_wrap> only works if the item/form/container has <txp:...> tags in it. If you’re just wrapping a {replacement} you need <txp:smd_wrap_all> because it treats data as a raw stream of info, not a collection of tags.

Essentially:

  • <txp:smd_wrap> behaves like <txp:chh_if_data>
  • <txp:smd_wrap_all> behaves like <txp:smd_wrap> did in v0.10

Shout at maniqui if this is wrong (he convinced me change it!), or — probably more productive — let me know if you think this situation can be improved by better naming or altering some of the default attributes.

Last edited by Bloke (2012-02-16 16:21:35)


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

Hire Txp Builders – finely-crafted code, design and Txp

Offline

#80 2012-02-16 17:14:54

sacripant
Plugin Author
From: Rhône — France
Registered: 2008-06-01
Posts: 479
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

I started my tests with “smd_wrap 0.1”. Until arriving at {lieu} (place):

<p class="location">
   <a class="include" href='#<txp:smd_wrap transform="sanitize|url_title" item="{lieu}" />-hcard'>{lieu}</a>
</p>

How to wrap {lieu} with a and p ?

<txp:smd_wrap wraptag="p" class="location">
   <a class="include" href='#<txp:smd_wrap transform="sanitize|url_title" item="{lieu}" />-hcard'>{lieu}</a>
</txp:smd_wrap>

Don’t work, if {lieu} is empty, p and a are displayed. Then I update smd_wrap to 0.2b. but {lieu} are not recognized.
The solution is to ship {replacement} tag in <txp:variable /> :

<txp:variable name="lieu" value="{lieu}" />
<txp:smd_wrap wraptag="p" class="location">
   <a class="include" href='#<txp:smd_wrap_all transform="sanitize|url_title" item="{lieu}" />-hcard'><txp:variable name="lieu" /></a>
</txp:smd_wrap>

OK.

if you think this situation can be improved by better naming or altering some of the default attributes.

Mmmmh,
Never used {tokken} in your plugins. Only <txp/> tags :¬D

Offline

#81 2012-02-24 18:56:11

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

[Edit 2012-02-27-0811—the below has been improved upon

Hi Stef & fellow TXPers,

I wanted the ability to easily add subtitles to section names. And I wanted to do it by adding copy to the Section title like this My section name _my section subtitle_.

I achieved this with the following code:

<txp:smd_wrap transform="replace||</p>|">
    <txp:smd_wrap transform="replace||<p>|">
        <txp:smd_wrap transform="textile">
            <txp:section title="1" link="1" />
        </txp:smd_wrap>
    </txp:smd_wrap>
</txp:smd_wrap>

The multiple smd_wraps are because adding Textile support wraps the content in a <p></p> tag and I don’t want a <p></p> tag.

Is there a more elegant way of doing this with smd_wrap? I checked the docs and tried a few methods but this is the only way I’ve been able to work out (not complaining, I LOVE that I can do this!).

Thanks for the plugin Stef, cheers all, -Alan

Last edited by alanfluff (2012-02-27 13:12:21)


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#82 2012-02-24 20:32:26

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,202
Website GitHub

Re: smd_wrap: conditionally wrap stuff with tags and labels

Yes, you can chain transforms inside the tag and do the opening and closing p-tag replace in one operation:

<txp:smd_wrap transform="textile,replace|regex|'<\/?p>'|"><txp:section title="1" link="1" /></txp:smd_wrap>

TXP Builders – finely-crafted code, design and txp

Offline

#83 2012-02-24 20:53:34

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

Thanks jakob!

I didn’t realize I could do that, excellent. I’ll go and read up (again) on regex ;) Cheers! -Alan


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#84 2012-02-24 20:57:25

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

Re: smd_wrap: conditionally wrap stuff with tags and labels

alanfluff wrote:

And I wanted to do it by adding copy to the Section title like this My section name _my section subtitle_.

That’s clever thinking! I like this kind of duplicated use of text inputs :) Thanks for sharing this, Alan! We’ve now three new methods for enriching section information gathered here in one day.


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

Offline

#85 2012-02-24 21:02:51

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

You’re most welcome Uli, thanks for the links too and I’m glad this idea worked (courtesy Stef+plugin and jakob) and also that it might be helpful to someone else :) Cheers, -Alan


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#86 2012-02-26 21:14:52

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

@Jukka When you said:

If you do heavy regex stuff, like Textile parsing, consider/remember to cache the results…

were you thinking of a specific TXP method for caching results?


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#87 2012-02-27 13:11:00

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

[Moderator: If this post is too conversational or otherwise wrong for this thread please move/delete it and let me know my transgression, tks :)]

I’ve improved the section subtitle code but I am sure the regex and steps could be simplified.

Before I was adding Textile in order to differentiate what was the subtitle. This meant an author had to write [Section title text] [delimeter] [startTextile] [subtitle text] [endTextile], for example Home / _is where the heart is_.

Having used textile to identify the subtitle as the part that was <em></em> wrapped, I then needed to strip the <p></p> tags that Textile was dutifully wrapping the entire title in—a waste of time (but a valuable step for my small brain en-route to this improvement).

The revised code checks if the title has a subtitle at all and also only requires the author to write in the format [Section title text] [delimeter] [subtitle text].

Simplification suggestions welcomed. Cheers, -Alan
PS: Thanks to Stef, again, for the mighty smd_if which I am now also using.

This Section title text:

Home @ is where the heart is

produces this output:

<li>
	<a href="http://example.com/">
		Home <span class="visuallyhidden">&mdash;</span><span class="subtitle"> is where the heart is</span>
	</a>
</li>

and this is the code:

<li>
	<txp:section title="0" link="1">
		<txp:smd_if field='<txp:section title="1" link="0" />' operator="contains" value="@">
			<txp:smd_wrap transform="replace|regex|'\/?@'|">
				<txp:smd_wrap transform="replace|regex|'[^@]*$'|">
					<txp:section title="1" link="0" />
				</txp:smd_wrap>
			</txp:smd_wrap>
			<span class="visuallyhidden">&mdash;</span>
			<span class="subtitle">
				<txp:smd_wrap transform="replace|regex|'^((?!([@])).)*@(?)'|">
					<txp:section title="1" link="0" />
				</txp:smd_wrap>
			</span>
		<txp:else />
			<txp:section title="1" link="0" />
		</txp:smd_if>
	</txp:section>
</li>

Last edited by alanfluff (2012-02-27 13:13:24)


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#88 2012-02-27 14:49:04

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 5,202
Website GitHub

Re: smd_wrap: conditionally wrap stuff with tags and labels

Wow, that makes my head hurt. Let me get that straight: you want to split the section title at the @ sign. Could you not do a simple replace operation without all that regex in there.

<li>
  <a href="<txp:site_url /><txp:section />">
    <txp:smd_if field='<txp:section title="1" />' operator="contains" value="@">
      <txp:smd_wrap transform="replace|string| @ | <span class='visuallyhidden'>–</span> <span class='subtitle'>,add|after|</span>"><txp:section title="1" /></txp:smd_wrap>
    <txp:else />
      <txp:section title="1" />
    </txp:smd_if>
  </a>
</li>

Yet another approach would be to use the “split” transform to get the first part and then the split transform again for the second (last) part and then to add the relevant code before and after each bit.

When Jukka said…

If you do heavy regex stuff, like Textile parsing, consider/remember to cache the results…

… he was referring to the fact that textiling is a relatively processor-intensive search replace operation that is costly to do on every page. For this reason txp saves a pre-textiled version of the body, title and excerpt rather than textiling every page on the fly. If you have a lot of on-the-fly textiled stuff on a page, you can use something like aks_cache to save rendered sections of your page that don’t change often (i.e. only when someone changes the section’s heading).


TXP Builders – finely-crafted code, design and txp

Offline

#89 2012-02-27 15:34:30

alanfluff
Member
From: Ottawa, Canada
Registered: 2008-09-15
Posts: 222
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

THANKS Jakob, that example you provided is much more simple and works perfectly.

I have learnt more about how powerfully smd_wrap can be used from your example.

Thanks for the comment about textile processing, I understood the fact that processing textile would add processor load but didn’t know or had forgotten that TXP:

saves a pre-textiled version of the body, title and excerpt rather than textiling every page on the fly

and I have no experience with cache plugins so thank you for pointing out aks_cache.


At LAST I’ve cheerfully donated to the core devs at #TXP. I only wish I were able to give more. Thanks to the devs and ALL fellow TXPers. -A

Offline

#90 2012-03-07 14:59:34

Mats
Member
From: Sweden
Registered: 2011-01-16
Posts: 30
Website

Re: smd_wrap: conditionally wrap stuff with tags and labels

Hello!

Thanks for another great plugin Stef!

I’m trying to create events in Google calendar from articles using urls. The problem is when trying to convert the letters Å,Ä,Ö. These letters are converted like this by google (the letter Å): %C3%85 . When using convert the plugin prints out %C3%85 as a Å. I haven’t had any success trying both with replace and sanitize. Is this doable with smd_wrap?

/Mats

Offline

Board footer

Powered by FluxBB