Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2024-08-10 22:31:45

lindabb
Member
Registered: 2023-02-17
Posts: 132

evaluate error

I have this, (with help of Bloke) I got this.
<txp:evaluate class="more more-news" query='substring("<txp:body escape="tags" />", 1, 120)' escape="trim" /><a href="blogs/<txp:article_url_title />">Read more</a>
I’m trying to get first 120 characters from the article. then “read me” link to view the full article.

But I’m getting this error:

Tag error: <txp:evaluate class="more more-news" query='substring("<txp:body escape="tags" />", 1, 120)' escape="trim" /> ->  Warning: DOMXPath::evaluate(): Invalid expression Issue detected while parsing form None on page testing

Any idea what I’m doing wrong?
also, is there a tag that I can use to get first number of words/characters from any article ?

Thank you

Offline

#2 2024-08-11 08:12:36

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

Re: evaluate error

lindabb wrote #337574:

I have this … but I’m getting this error:

Tag error: <txp:evaluate class="more more-news" query='substring("<txp:body escape="tags" />", 1, 120)' escape="trim" /> -> Warning: DOMXPath::evaluate(): Invalid expression Issue detected while parsing form None on page testing...

I think your tag looks good. If you compare it with the code in this thread, where we talked about it before, all that you have changed is the number of characters and added the class attribute. PS: for the class attribute to work, you also need the wraptag attribute which the class will be applied to, e.g. wraptag="p".

I tested your code on the demo site and it worked fine. Do you perhaps have txp:evaluate somewhere else on the page? Or is there something special in your body field that it might be tripping over?

is there a tag that I can use to get first number of words/characters from any article ?

There’s a companion XPath string function call string-length that will give you the character count:

<txp:evaluate query='string-length("<txp:body escape="tags" />")' />

If you’re thinking about creating a reading time estimation, Patrick has a plugin pat_reading_time. I think you can also use it to display word counts.

And then there’s Bloke’s own smd_article_stats which you can also use to output article stats.


TXP Builders – finely-crafted code, design and txp

Offline

#3 2024-08-13 15:09:38

lindabb
Member
Registered: 2023-02-17
Posts: 132

Re: evaluate error

Thank you.

Offline

#4 2024-08-25 15:17:10

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

Re: evaluate error

To come back to this. I’ve now seen this error happening too. The message only shows in debugging mode and the code does provide the correct output, so it’s only something that bothers you as the developer.

The cause (I think) is the nested " quotes of the attribute inside the " around the string. I found a workaround in a post by etc here. If you change your code to read as follows, you get correct results without the Invalid Expression notice:

<txp:evaluate query='substring(<txp:body escape="tags, trim, quote" />, 1, 120)' escape="trim" />
<txp:evaluate query='string-length(<txp:body escape="tags, trim, quote" /> > 120)'>…</txp:evaluate>

Essentially the quotes around the txp:body string are being added by the escape function, making it possible to omit them in the tag here.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB