Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#151 2019-12-21 10:32:39

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 106
Website

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

Inside my macro I have some php. I test it with a simple

<txp:php>
echo ‘hello world’;
</txp:php>

to make sure it is not the php itself.

When I use the macro inside of my <txp:body/> all works fine.
When I try to use it just inside of <txp:article/> it works also but not the php-part.
In my peferences of the textpattern installation I have “allow php” in articels and pages checked.

Any idea why this is happening?

Last edited by demoncleaner (2019-12-21 10:32:50)

Offline

#152 2019-12-21 10:42:18

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,639
Website

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

demoncleaner wrote #320636:

When I use the macro inside of my <txp:body/> all works fine. When I try to use it just inside of <txp:article/> it works also but not the php-part.

That’s to do with Txp 4.7’s parser disallowing PHP from running on anything secondpass or higher (for security). See this discussion for more info and workarounds. If you can wait a day or two, the 4.8 beta will hopefully be out, where this restriction has been lifted.

And, btw you shouldn’t need this plugin any more. I’m retiring it. Textpattern can do everything it could, far cleaner and faster and safer using special shortcode forms. Check ‘em out and if you need any help migrating to the core solution, just holler on the forum.


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

#153 2019-12-21 11:01:59

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 106
Website

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

Thanks Stef,

actually I am still using smd_macro a lot. Did not realize that it can be done better and cleaner without it. I will dig into shortcode forms.

Apart from that I can wait for 4.8.

Thanks again for your fast reply and Merry Christmas!

Last edited by demoncleaner (2019-12-21 11:02:39)

Offline

#154 2020-02-06 17:09:59

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,639
Website

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

Way back in the mists of time, I posted a little macro that used a hunk of PHP to output a series of images scattered within your article.

The premise is that, after attaching a bunch of images to your article image field via whatever means (typing, plugin, …) it’d be handy to drop them into your article – inline – in the order given in the article image field. So you could swap the order around – even change the image IDs in the list – and the article would render them in the given order, without you needing to change anything else.

I’m just here to report that it’s now a cinch to do natively in core with no PHP in 4.8+. Here’s how:

  1. Establish a Txp Form (of any type) named ‘nextimg’.
  2. Put the code below in it.
  3. Stick your article image IDs as a comma-separated list in your article image field. Even use the new ranges feature if you like.
  4. Anywhere you like – body text, excerpt, other pages, forms, wherever there’s article context – put in <txp::nextimg /> and the next image in the list will be displayed right there.

It uses a variable to keep track of which image you’re up to, per article, so it can be used safely in lists if you want to display thumbnails alongside your teaser.

Here’s the shortcode:

<txp:if_article_image>
   <txp:if_variable not name='imgoff_<txp:article_id />'><txp:variable name='imgoff_<txp:article_id />' value="0" /></txp:if_variable>
    <txp:images offset='<txp:variable name=''imgoff_<txp:article_id />'' />' limit="1" form='<txp:yield name="render" default="images" />' />
    <txp:variable name='imgoff_<txp:article_id />' add />
</txp:if_article_image>

There might be an even more efficient way to do this but I’m covering my bases in the initial variable setup to ensure they are initialized to 0. That might not be strictly necessary.

I’m bouncing the actual img tag rendering out to another form called images here. That could be whatever you want.

You can pass into the shortcode the ‘class’ attribute to set that (see <txp:yield /> below) and I’ve also set it to permit you to override the form too via the render attribute.

Anyway, my images Form uses smd_thumbnail to set up a simple srcset but you don’t have to go that far. Just an example:

<txp:variable name="caption" value='<txp:image_info />' />
<txp:if_variable name="caption">
    <figure itemprop="image" itemscope itemtype="https://schema.org/ImageObject" class="<txp:yield name='class' />">
        <img itemprop="url contentUrl" src="<txp:image_url link='0' />" srcset="<txp:smd_thumbnail type="Mobile" display="url" /> 480w, <txp:image_url /> 1280w" alt="<txp:image_info type='alt' />" class="article-image">
        <figcaption itemprop="caption">
            <txp:variable name="caption" />
        </figcaption>
    </figure>
<txp:else />
    <p itemprop="image" itemscope itemtype="https://schema.org/ImageObject" class="<txp:yield name='class' />">
        <img itemprop="url contentUrl" src="<txp:image_url link='0' />" alt="<txp:image_info type='alt' />">
    </p>
</txp:if_variable>

Enjoy!

Last edited by Bloke (2020-02-06 18:38:43)


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

#155 2020-02-06 17:18:17

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 8,169
Website

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

That would be an excellent tip for txp.tips!


Yiannis
——————————
neme.org | hblack.net | LABS | State Machines | NeMe @ github | Covid-19; a resource
I do my best editing after I click on the submit button.

Offline

#156 2020-02-06 17:19:27

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,639
Website

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

Yeah, good idea. When it’s ironed out and made as efficient as possible, I’ll pass it on to Julian for inclusion.


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

#157 2020-06-10 11:59:21

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 106
Website

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

I was using smd_macro before like this:

<txp:macroname>
{smd_container}
</txp:macroname>

Now this can be done nicely with an ouputform <txp::macroname>
But how could I achieve the “containering”
Am I missing something? What would be the substitution of {smd_macro} in a form setup inside and article?

Last edited by demoncleaner (2020-06-10 11:59:35)

Offline

#158 2020-06-10 12:05:40

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,639
Website

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

demoncleaner wrote #323685:

how could I achieve the “containering”

Wrap your content with <txp::form_name>your container</txp::form_name> and then access the contained content via the yield tag without attributes (just note there’s a bug in 4.8.1, so please use 4.8.2-dev if you’re doing this, as that’s been patched and will be out very soon).

You can also pass parameters in via attributes:

<txp::form_name size="big" colour="yellow" />

And then access those inside your form, with the yield tag and <txp:if_yield> if you like:

<ul>
<li>Chosen size: <txp:yield name="size" default="medium" /></li>
<li>Chosen colour: <txp:yield name="colour" default="red" /></li>
<txp:if_yield name="width">
   <li>Chosen width: <txp:yield name="width" /></li>
</txp:if_yield>
</ul>

Hope that helps.


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

#159 2020-06-10 12:16:07

demoncleaner
Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 106
Website

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

Damn! I knew it was too simple… So far I was using yield always with a name attribute and I thought it needed it. The rest I knew already but thanks for pointing it out again.

So <txp:yield/> (without a parameter) = {smd_container}

I just installed 4.8.2 dev version and it works perfect. Thanks Stef.

Last edited by demoncleaner (2020-06-10 12:16:44)

Offline

#160 2020-06-10 12:23:58

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 9,639
Website

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

demoncleaner wrote #323687:

So <txp:yield/> (without a parameter) = {smd_container}

Yes, and <txp:yield else /> gives you access to any ‘false’ (else) branch in your form.


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

Board footer

Powered by FluxBB