Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#37 2012-09-14 05:43:25

photonomad
Member
Registered: 2005-09-10
Posts: 290
Website

Re: Article thumbnail image from YouTube/Flickr code found in Body

Hi etc! I didn’t see your edit above until now. Thanks!

Question: is it possible to use etc_query to find the first <txp:images /> tag in an article body? I would like to find it and grab the id from it. I don’t think the <txp:images /> tag is parsed into an actual image tag first. I tried using parse=“before” with etc_query (with code above), but didn’t get a result. Any suggestions? Thanks!

Offline

#38 2012-09-14 07:32:04

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

Re: Article thumbnail image from YouTube/Flickr code found in Body

Hi!

I have not implemented namespaces (like txp:) for security reasons, so <txp:image id="1" /> becomes <image id="1"></image>. Parsing would fix this, you are quite right, but data is not parsed by etc_query, only content is. So

  • either query for string(//image[1]/@id) with your code
  • or, if you use this code in article form (not in article body), replace data="{?body}" by data='<txp:body />'. Mind, however, that in this case body will be parsed twice if you use <txp:body /> somewhere else in the form. To avoid it, you can firstly put it in some <txp:variable name="body" value='</txp:body >' />, then use <txp:variable name="body" /> everywhere instead of <txp:body />.

Offline

#39 2012-09-15 01:30:12

photonomad
Member
Registered: 2005-09-10
Posts: 290
Website

Re: Article thumbnail image from YouTube/Flickr code found in Body

Thanks, etc! I understand most of what you said above, but I’m still not able to find a way to get only the image ID. However, after playing around with your suggestion and reading a little xpath tutorial, I figured out how to get just the part of the image src that I needed (rather than the image ID from the txp:images tag).

This is what I’m using now and it works:

<txp:etc_query data='<txp:variable name="body" />' query="substring-before(//img[1]/@src, '&')" />

Edit:
I do have one question: is possible with xpath to find the string in the src after “=” and before “&”? In the src below, that would return only the image’s original URL.

Ok, I found the answer. This returns only the image’s original url (without the timthumb stuff):

<txp:etc_query data='<txp:variable name="body" />' query="substring-before(substring-after(//img[1]/@src,'='),'&')" />

Full backstory:

I have tags like the following in my article body: <txp:images id=“4” form=“image_large_center” /> (using Bloke’s still-in-development image selector plugin to easily select and add images into the body)

When the body is parsed (in the body variable as etc suggested above), the output of the images tag becomes this (I’m using timthumb.php to resize the image):
<img src="http://www.mydomain.com/timthumb.php?src=http://www.mydomain.com/images/64.jpg&w=568&q=100&s=1" alt="caption text here" class="center caption" />

The following is successful at getting the entire first img src:

<txp:etc_query data='<txp:variable name="body" />' query="string(//img[1]/@src)" />

Since I needed to resize the image differently in an article list, I had to change the string function to get the src up until the “&”.

If anyone found this useful, then great. Otherwise, sorry for such a long and rambling post!

Last edited by photonomad (2012-09-15 02:17:51)

Offline

#40 2012-09-15 09:49:32

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

Re: Article thumbnail image from YouTube/Flickr code found in Body

photonomad wrote:

I’m still not able to find a way to get only the image ID.

Sorry, I have somehow zapped on this. You should be able to do it with unparsed body:

<txp:etc_query data="{?body}" query="string(//image[1]/@id)" />

To see precisely how etc_query has imported data, call it with debug="h" attribute (in txp debug mode). You should see few warnings about invalid txp:image tags, that will be converted to <image id="64"></image>. You can then extract id attribute as above.

You can also cheat by adding some data-id attribute to img tag in your “image_large_center” form:

<img ... data-id='<txp:image_info type="id" />' />

Edit: or, if you are looking for src

<img ... data-id='<txp:image_url />' />

Then query for

<txp:etc_query data='<txp:variable name="body" />' query="string(//img[1]/@data-id)" />

Anyway, you have found a solution, and I hope learning xpath was not waste of time.

Last edited by etc (2012-09-15 10:36:33)

Offline

#41 2012-09-19 19:56:06

photonomad
Member
Registered: 2005-09-10
Posts: 290
Website

Re: Article thumbnail image from YouTube/Flickr code found in Body

Learning xpath wasn’t a waste of time! I like it and your plugin is very useful. Thanks!

Offline

Board footer

Powered by FluxBB