Comparing a string length

110 views
Skip to first unread message

Neil

unread,
Jun 12, 2015, 4:32:41 AM6/12/15
to reddot-c...@googlegroups.com
Hi

I'm struggling to compare the length of a (string) text element.

The following works to get a simple output, of, say, '20':

Test: <%!! Escape:Text(<%txt_content%>).Length !!%> // Outputs '20'.

But comparing below just outputs nothing:

<reddot:cms>
    <if>
        <query valuea="Escape:Text(<%txt_content%>).Length" operator="&gt;" valueb="Int:10">
            <htmltext>------ Over 10 ------</htmltext>
        </query>
        <query type="else">
            <htmltext>----------Less than 10---------</htmltext>
        </query>
    </if>
</reddot:cms>

I've tried changing the operator between '>', 'gt' and '&gt;', but no joy - am I missing something obvious here?

Thanks

Neil

Mark Radford

unread,
Jun 12, 2015, 11:01:08 AM6/12/15
to reddot-c...@googlegroups.com
Hi Neil,

I have a feeling the output type of the .Length method might actually be a string, rather than an integer.  

I have a feeling this is a long shot, and I haven't tried it but maybe you can prefix the "Escape:Text().Length" call with "Int:" to cast it that way?  So ...

<query valuea="Int:Escape:Text(<%txt_content%>).Length" operator="&gt;" valueb="Int:10">

Might be worth a shot?

Mark

Hilmar Bunjes

unread,
Jun 14, 2015, 4:34:16 AM6/14/15
to reddot-c...@googlegroups.com
Hi Neil,
which version are you using? In 11.0.1.642  it works just as you wrote (with gt or >) without the "Int:" Mark suggested. 

However, in some version it could be required to add the "Int:" to trigger the CMS to handle the return value of the Length property is integer.

Best,
Hilmar

Neil

unread,
Jun 15, 2015, 4:42:02 AM6/15/15
to reddot-c...@googlegroups.com
Hi all, thanks for the replies (including those off-list).

This is 11.2 Build 11.2.2.844 (11.2 SP2 HF6).

I got an off-list suggestion to change '...(<%txt_content%>)...' to just '...(txt_content)...', which got the comparison to at least render the output message.

It returned 'Less than 10' as the message, despite being over 2,000 chars, so something still not right:

<if>
        <query valuea="Escape:Text(txt_content).Length" operator="&gt;" valueb="Int:10">
            <htmltext>------ Over 10 ------</htmltext>
        </query>
        <query type="else">
            <htmltext>------ Less than 10 ------</htmltext>
        </query>
    </if>


Hilmar Bunjes

unread,
Jun 15, 2015, 11:12:24 AM6/15/15
to reddot-c...@googlegroups.com
Hi Neil,
if you write this:

Escape:Text(txt_content).Length

you get the length of the String "txt_content" which should result in 11 characters. However, I'm not sure why you get "Less then 10" (which is in fact "Less than or equal 10").

A slightly better solution for this problem could be to not insert the text placeholder into the page but to grab it from the RenderTag directly. This way you do not need any Escaping and do not run into problems with characters in the element that break the RenderTag. You can get an element of the current page this way (this should print out the length of the element txt_teaser):

<%!! Context:CurrentPage.GetElementByName(txt_teaser).Value.Length !!%>

If you are using an element transfered via list/dyn anchor you need to get the page first and then the element (the inf_... is an info element to get the guid of the page which is iterated in the list):

<%!! Context:Pages.GetPage(Guid:<%inf_PageGUID%>).GetElementByName... !!%>

Best,
Hilmar

-- 
www.smartapi.de - RedDot RQL via .NET - object-oriented, well-tested and open source

Jian Huang

unread,
Jun 15, 2015, 1:10:59 PM6/15/15
to reddot-c...@googlegroups.com
Hi Neil,

The new line or carriage return within txt_content is probably breaking the format of your rendertag.


Try this instead.

<%!! Escape:HtmlEncode(<%txt_body%>).Length !!%>

Richard Hauer

unread,
Jun 16, 2015, 3:52:10 AM6/16/15
to reddot-c...@googlegroups.com

HTML Encoding the string will likely alter its length.

If you were expecting the content to be 100 characters that change will probably not be significant, but when you’re looking for 10 characters encoding an “&” to “&amp;” arbitrarily removes 40% of your capacity.

 

It should be obvious by now that using a render tag is not the way to solve this problem.

Are you publishing to PHP or ASPX? Use the runtime language support to alter the display on the fly.

Are you publishing to HTML? Use JavaScript.

 

You’re breaking a walnut with explosives.

--
You received this message because you are subscribed to the Google Groups "RedDot CMS Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reddot-cms-use...@googlegroups.com.
To post to this group, send email to reddot-c...@googlegroups.com.
Visit this group at http://groups.google.com/group/reddot-cms-users.
For more options, visit https://groups.google.com/d/optout.

Hilmar Bunjes

unread,
Jun 16, 2015, 3:59:07 AM6/16/15
to reddot-c...@googlegroups.com
Richard,
getting the element content via RenderTag should not alter the content of the string, does it?

Don't you think <%!! Context:CurrentPage.GetElementByName(txt_teaser).Value.Length !!%> should solve this issue? 

I think the explosives are server-side or client-side programming instead of using RenderTags :-)

Best,
Hilmar

Richard Hauer

unread,
Jun 16, 2015, 4:54:26 AM6/16/15
to reddot-c...@googlegroups.com

Well, getting the element content via the RenderTag will not alter the length, but HTML encoding it definitely will.

 

Observe…

 

"UP & DOWN"   - 11 characters

&quot;UP&amp; DOWN&quot; - 24 characters

 

Your solution is much better than Jian’s, though I suspect measuring the length of the string is not really the goal of Neil’s task at all.  I expect there is an element on the page that is quite small (physically speaking) and can only accommodate a small amount of content.  Measuring the length of the content is a hack often used to swap out a small display element with a larger one that can handle longer text.  This is a bad idea for lots of reasons, but chief among them is that characters are not all the same width, so measuring the number of characters does not actually meet the requirement at all, which is (and I’m guessing) to measure the *width* of the characters.

 

Both 10 characters:

IIIIIIIIII

vs

WWWWWWWWWW

 

This type of problem is handled much better with CSS (and the client-side code you are clearly not fond of) as the browser knows exactly how wide that content is going to be for the given device/orientation/zoom/font and can make adjustments as appropriate.

 

At any rate all that is moot as the actual question was about measuring the length of the string.

Neil, if it *HAS* to be done in a render tag, do it Hilmar’s way (assuming you haven’t already).

 

/R

Hilmar Bunjes

unread,
Jun 16, 2015, 5:28:32 AM6/16/15
to reddot-c...@googlegroups.com
Hi Richard,
Ok, now I get what you mean and I totally agree: if the problem is displaying too much content in an HTML element this is not the right way to do. 

I've made very good experience with this JavaScript to shorten the content to the size of an element: http://dotdotdot.frebsite.nl The CSS wrapping never has worked the way I wanted it to work but this does. 

Best
Hilmar



Sent via mobile phone
You received this message because you are subscribed to a topic in the Google Groups "RedDot CMS Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/reddot-cms-users/LkHGVcA8uNY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to reddot-cms-use...@googlegroups.com.

Jian Huang

unread,
Jun 16, 2015, 10:13:01 AM6/16/15
to reddot-c...@googlegroups.com
Hi guys,

My bad, I lost sight of the question.

-Jian

To post to this group, send email to reddot-...@googlegroups.com.

--
You received this message because you are subscribed to a topic in the Google Groups "RedDot CMS Users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/reddot-cms-users/LkHGVcA8uNY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to reddot-cms-us...@googlegroups.com.
To post to this group, send email to reddot-...@googlegroups.com.

Neil

unread,
Jun 16, 2015, 10:20:44 AM6/16/15
to reddot-c...@googlegroups.com
Hi everyone

Thanks for taking the time to discuss this.

For the record, I was having trouble with <!IoRangeConditional> tags on text elements, as the older RadEditor had left some rogue '<p>&nbsp;</p>' content. As the conditional tag was unreliable, I thought that if the text element has at least, say, 20 characters, then it won't be the rogue issue above and will be proper content, hence show it.

So, really just wanted to check for a minimum length.

Hilmar's suggestion of Context:CurrentPage.GetElementByName(txt_teaser).Value.Length worked fine, by the way.

Thanks
Reply all
Reply to author
Forward
0 new messages