Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2022-12-13 20:25:16

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Using evaluate to do division, with image width

I’m just learning about the evaluate tag, and it seemed like I could use this to replace adi_calc when using 4.8.8. But maybe I’m wrong.

Here’s a stripped-down example of one use of adi_calc:

<txp:images limit="1">
  <txp:variable name="this_image_width"><txp:adi_calc div="1.5"><txp:image_info type="thumb_w"/></txp:adi_calc></txp:variable>
  <!--
  image_info['thumb_w'] = 100
  variable[this_image_width] = 66
  -->
</txp:images>

When I try the same thing with txp:evaluate, this_image_width returns an empty string:

<txp:variable name="this_image_width" '<txp:evaluate query=''<txp:image_info type="thumb_w"/> / 1.5''/>

It seems like evaluate has a problem with division. No problem: I can just multiply by the reciprocal, right?

<txp:variable name="this_image_width" '<txp:evaluate query=''<txp:image_info type="thumb_w"/> * .66''/>

For some reason, this returns the same value as the thumb_w of the original image, as if I didn’t multiply it.

Ok, so I tested it with straight numeric values without nesting the image_info tag:

<pre><txp:evaluate query="100 * .66"/></pre>

This returns the expected value—in this case, 66.

Is this something I should not use txp:evaluate for? Are there quirks not covered in the tag documentation?

Thanks in advance!

Offline

#2 2022-12-13 21:27:20

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

Re: Using evaluate to do division, with image width

You’re close but two things are preventing it working. Firstly, not assigning the evaluate tag’s output to the variable correctly (you need value=, or use it as a container). Secondly, your division operator needs to be div not a slash.

The mix of quotes is making things murky so I’d take that out completely and go with the simpler assignment via container:

<txp:variable name="this_image_width" trim>
    <txp:evaluate query='<txp:image_info type="thumb_w" /> div 1.5' />
</txp:variable>

That should get you on the right track.


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 2022-12-13 22:24:23

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: Using evaluate to do division, with image width

Thank you, @bloke!

This works.

Using variable as a container tag was the way I did it on my first attempt, but when that produced an empty string, I switched to the way it was in the example in the tag documentation.

I must say, the trim attribute is the bees knees and will save me a lot of antics.

Thank you again!

Offline

#4 2022-12-13 22:50:16

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

Re: Using evaluate to do division, with image width

No problem. I struggled to get it to work with various different permutations of apostrophe, double quotes, etc, even when I added the missing value= attribute. So I just cheated and went back to the container method which sidesteps it fairly neatly.

And yeah, trim is awesome. The global attributes are pretty sweet and can be used in seriously inventive ways to achieve stuff that used to require a bunch of plugins.


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 2022-12-13 23:00:56

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

Re: Using evaluate to do division, with image width

johnstephens wrote #334297:

the example in the tag documentation.

Gosh, it’s fairly outdated, the modern way is

<txp:variable name="counter" add />

Offline

#6 2022-12-13 23:08:43

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

Re: Using evaluate to do division, with image width

Bloke wrote #334298:

The global attributes are pretty sweet and can be used in seriously inventive ways to achieve stuff that used to require a bunch of plugins.

We could actually make add and multiply global, so it would just be

<txp:image_info type="thumb_w" multiply="0.67" />

Of limited use, perhaps?

Offline

#7 2022-12-13 23:15:19

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

Re: Using evaluate to do division, with image width

etc wrote #334300:

We could actually make add and multiply global… Of limited use, perhaps?

Perhaps. What else would they be applicable to? Only tags that output numeric data, and we don’t have many of those. Errrr, do we?


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

#8 2022-12-13 23:19:22

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

Re: Using evaluate to do division, with image width

Bloke wrote #334301:

Perhaps. What else would they be applicable to? Only tags that output numeric data, and we don’t have many of those. Errrr, do we?

We don’t. It would just replicate multiply times the tags output for non-numeric values, but this looks of no use at all, and is better done with some <txp:list /> tag anyway.

They might be handy for custom fields, though. But it will always be time to add them if needed.

Actually, there is a global evaluate attribute, so this could work:

<txp:image_info type="thumb_w" evaluate="0.67*<+>" />

Offline

#9 2022-12-14 00:43:33

phiw13
Plugin Author
From: Japan
Registered: 2004-02-27
Posts: 3,220
Website

Re: Using evaluate to do division, with image width

etc wrote #334302:

Actually, there is a global evaluate attribute, so this could work:

<txp:image_info type="thumb_w" evaluate="0.67*<+>" />...

Oh, that looks nice and clean! I might want to use that for some of my image shortcodes


Where is that emoji for a solar powered submarine when you need it ?
Sand space – admin theme for Textpattern

Offline

Board footer

Powered by FluxBB