Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#37 2011-04-10 00:06:28

jstubbs
Member
From: Hong Kong
Registered: 2004-12-13
Posts: 2,395
Website

Re: smd_macro: Create custom virtual Txp tags that do stuff

Some tutorials or tips would be helpful for users in the usual place.

Offline

#38 2011-04-10 16:52:10

jcd
Member
Registered: 2010-04-15
Posts: 12

Re: smd_macro: Create custom virtual Txp tags that do stuff

Thanks for this amazing plugin! I have recently used it to make it easier for clients to insert an image gallery. The macro passes on the image IDs to smd_gallery, which then creates an unordered list with the images.
Upon validating the HTML, I found the problem that Textpattern / Textile always adds a <p></p> container around the code generated by the macro. This of course doesn’t validate if the macro inserts a block element like <ul> or <div>. I know you can just put a space in front of the macro call in the Textpattern article, but I think that’s not an elegant solution for the client. Is it somehow possible that smd_macro can avoid being surrounded by the <p> tag?

Offline

#39 2011-04-10 19:34:38

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

Re: smd_macro: Create custom virtual Txp tags that do stuff

jcd wrote:

Is it somehow possible that smd_macro can avoid being surrounded by the <p> tag?

Ummm, ahhhh, errrr… that’s a good point. I can’t think of a neat way offhand. The usual techniques (turn off textile, use notextile. start line with spaces, or surround tags with == signs) are, as you say, not exactly client friendly or have some shortcomings in terms of functionality.

The thing is I don’t think there’s anything smd_macros itself can do to guard against this because it just does what the macro tells it to do and the Textile parser does the rest ‘outside’ the macro’s output. The only cheating way I can think of is adding a line of jQuery to the end of your macro (or write a separate macro that you call somewhere else on your page that injects the jQuery) which scans the DOM tree for, say, the class or ID of your container and performs an unwrap on it.

That technique might have some shortcomings:

  1. it probably won’t help the validator because the manipulation is done post-DOMready but it might make you feel better that you know it is actually valid :-)
  2. you may need to guard against those situations where a client willfully or accidentally puts a space in front of the tag or is otherwise Textile-savvy; in those instances you might end up removing the whole article container (or <body>!) so you may require some defensive programming to check you’re only unwrapping paragraph markers

Aside from that I’m out of ideas, sorry. Some other TXP fox might come up with something more cunning though.


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

Online

#40 2011-04-10 21:07:13

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,726
Website

Re: smd_macro: Create custom virtual Txp tags that do stuff

How about using pax_grep or a snippet of php in your smd_macro to remove the <p> and </p> from the output as outline here? That would happen server-side prior to code output.

Last edited by jakob (2011-04-10 21:07:31)


TXP Builders – finely-crafted code, design and txp

Offline

#41 2011-04-11 07:15:10

jpdupont
Member
Registered: 2004-10-01
Posts: 752

Re: smd_macro: Create custom virtual Txp tags that do stuff

+1
I’m not very happy with those <p></p>containers …

Is it possible to modify the way textile act ???
(As an option, for compatibility)

Offline

#42 2011-04-11 08:54:08

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

Re: smd_macro: Create custom virtual Txp tags that do stuff

jpdupont wrote:

Is it possible to modify the way textile act ???

Your best place to find out that sort of thing is on the Textile repo. The conversations on the Issues tracker might give you some ideas. The various experimental dev branches add support for defining your own block level elements so I guess, in theory, you could find some way to kill them.

Last edited by Bloke (2011-04-11 08:54:33)


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

Online

#43 2011-04-11 15:15:39

jcd
Member
Registered: 2010-04-15
Posts: 12

Re: smd_macro: Create custom virtual Txp tags that do stuff

jakob wrote:

How about using pax_grep or a snippet of php in your smd_macro to remove the <p> and </p> from the output as outline here? That would happen server-side prior to code output.

Thanks jakob, but as far as I understand there is nothing you can do from within a macro to influence the surrounding <p> tags added by Textile. If I used this strategy outside of the macro this would probably work, but adding a space in front of the macro tag inside of the article seems a lot easier for the client.

Offline

#44 2011-04-11 22:18:48

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: smd_macro: Create custom virtual Txp tags that do stuff

jcd wrote:

Thanks jakob, but as far as I understand there is nothing you can do from within a macro to influence the surrounding <p> tags added by Textile.

You could do something like this in your article form I guess. Assuming the macro output is wrapped in <ul> tags (don’t know pax_grep so this is with rah_replace):

<txp:rah_replace from="<p><ul>,</ul></p>" to="<ul>,</ul>"><txp:body /></txp:rah_replace>

Offline

#45 2011-04-22 06:52:41

phuture303
Member
Registered: 2008-09-14
Posts: 127

Re: smd_macro: Create custom virtual Txp tags that do stuff

Stef, just solved my first tiny steps with this plugin. It is FUCKING BRILLIANT! Wonderful, I just made the output of image galleries (in combination with a left-sided dropdown custom field of the existing image categories) the most easy thing I’ve ever seen :-)

End Els, thanks for the tip with rah_replace for deleting the <p>-tags surrounding the gallery. Just for the noobs like I am: if you need your <ul>-tag with a class or an id, then use:

<txp:rah_replace from="<p><ul,</ul></p>" to="<ul,</ul>"><txp:body /></txp:rah_replace> just without the closing braket at the opening <ul>-tag.

Perfect, this forum made my day. Again. Textpattern rocks – and you pros do, too.

David

Offline

#46 2011-05-04 13:00:02

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

Re: smd_macro: Create custom virtual Txp tags that do stuff

By way of an example macro suite, here’s my attempt at an interface to jQuery Tools tabs. Save the link and you can import them directly into smd_macro.

On your tabbable page(s), just include the jQuery Tools .js file as normal and ensure you have obtained a reference to the API using something like this code:

<script type="text/javascript">
   var api = jQuery("#wrapper").data("scrollable");
</script>

where wrapper is the DOM element that contains your tab panes. You can use a different variable name other than api if you wish, just remember to tell the macros what you’ve called it via the api_ref attribute (or edit the defaults in the macro definitions).

You or your clients can then use the following tags to present tab navigation links:

  • <txp:prev_tab /> : jump to the previous tab (will wrap if permitted). Attributes:
    • text = the text label. Default: « Previous
    • class = the class to apply to the anchor link. Default: learnmore
    • api_ref = the name of your javascript api variable. Default: api
  • <txp:next_tab /> : jump to the next tab (will wrap if permitted). Same attributes as above (text = Next »)
  • <txp:goto_tab /> : jump to the designated tab number. Attributes:
    • number = the tab to jump to, indexed from 1. Default: 1
    • class = the class to apply to the anchor link. Default: learnmore
    • api_ref = the name of your javascript api variable. Default: api
  • <txp:tab_nav /> : display a Goooogle style nav bar. Attributes:
    • tabs = the number of tabs in your set. Default: 1
    • class = the class to apply to the navigation wrapper div. Default: tabnav
    • breakclass = the class to apply to each anchor link. Default: learnmore
    • prev_text = the text label for the ‘prev’ link. Default: Previous
    • next_text = the text label for the ‘next’ link. Default: Next
    • api_ref = the name of your javascript api variable. Default: api

If you’re using MLP you can probably edit the macros to use ##snippet## syntax (untested).

Note that the macros are totally unsupported, they’re just available as examples for you to tweak and play around with. Hope they’re useful to someone.


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

Online

#47 2011-05-08 22:08:57

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,726
Website

Re: smd_macro: Create custom virtual Txp tags that do stuff

How might I test whether a custom smd_macro attribute has been specified (as opposed to empty) within the macro code, for example using smd_if? As soon as I use txp:if_variable, I define a txpvar, even if it is empty, so I can’t do a three way test with it.

Use case example: a tag that outputs a figure tag with attributes id and caption:

  • if the attribute caption is not specified (=undefined), then output the image’s own caption
  • if the attribute caption="custom text", i.e. is specified, override and use the custom caption
  • if the attribute caption="", i.e. is explicitly set to empty (=isempty) do not show a caption
I tried smd_if with field="phpvar:caption" and field="phpvar:lAtts[caption]" but to no avail:
<figure>
  <txp:image id="{img_id}" />
  <txp:smd_if field="phpvar:caption" operator="defined">
    <txp:smd_if field="NULL" operator="isempty" value="{caption}">
    <txp:else />
      <figcaption>{caption}</figcaption>
    </txp:smd_if>
  <txp:else />
    <figcaption><txp:image_info id="{img_id}" type="caption" /></figcaption>
  </txp:smd_if>
</figure>

Last edited by jakob (2011-05-08 22:14:11)


TXP Builders – finely-crafted code, design and txp

Offline

#48 2011-05-09 00:55:55

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

Re: smd_macro: Create custom virtual Txp tags that do stuff

jakob wrote:

How might I test whether a custom smd_macro attribute has been specified (as opposed to empty) within the macro code

Along with the problem of container macros, this scenario — the detection of whether an attribute is set or not — did occur to me as I was writing the plugin but I never came up with a satisfactory solution. There are times when you simply want to completely omit an attribute inside your macro — for example, whan an empty macro attribute is passed in and you want the TXP tag inside to take on its defaults. Currently there’s no neat way of doing that, which is a shame.

To further compound things, the $atts array doesn’t behave as you’d expect, probably because the function is actually not a real function. Certainly if you simply try to dmp($atts); you get nothing back which, I suspect, is due to the way the parser works. Or that it’s late and my brain’s slowly giving up. You can get funky with PHP5’s new ReflectionFunction() but it gets messy very quickly inside a php block.

In your case, smd_if can indeed come to the rescue and do a 3-way test but you have to be cunning. Give the macro’s caption attribute a default value that you will never reasonably expect people to type — I’ve chosen SMD_NONE here. Then you can proceed as follows:

<figure>
  <txp:image id="{img_id}" />
  <txp:smd_if field="{caption}" operator="eq" value="SMD_NONE">
    <figcaption><txp:image_info id="{img_id}" type="caption" /></figcaption>
  <txp:else />
    <txp:smd_if field="NULL" operator="eq" value="{caption}">
    <txp:else />
      <figcaption>{caption}</figcaption>
    </txp:smd_if>
  </txp:smd_if>
</figure>

How’s that?

Last edited by Bloke (2011-05-09 00:57:41)


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

Online

Board footer

Powered by FluxBB