Strange behaviour with TV and output modifier

I have a template with many TVs.
For subtitles I use a text TV, the output ist

[[*mytexttv:notempty='<h2>[[*mytexttv]]</h2>']]
(in original code there are backticks, not apostrophes)

It works with regular text, but a line ending with a question mark kills the output. On the frontend I see only a backtick and the following output is gone. Question mark is the only character which produces this behaviour. I tried other modifiers to test if the TV is empty or not but the result is always the same.

Any ideas?

Environment

MODX 2.8.1, Apache 2.4.43 (Unix), MySQL 5.7.32-log, PHP 7.4.13

I can’t reproduce it. Is the line

[[*mytexttv:notempty=`<h2>[[*mytexttv]]</h2>`]]

nested in another output modifier?

No, it isn’t nested.

Tried it with

[[IF? &subject=`[[*mytexttv]]` &operator=`notempty` &then=`<h2>[[*mytexttv]]</h2>`]]

Same result. Ok without a question mark, failure with the question mark at the end.

Does the value work if you just use [[*mytexttv]] with the ? in the value but fails when used inside an OM or in the If snippet? Are you able to look in the DB and see what’s in the field for the TV? It feels to me like there could be some strange character there.

It works stand-alone without a wrapping snippet or an output modifier.
In the DB the field is filled with the correct value “Test?”, no strange characters, no more characters than these.

This is the complete code, reduced to the essential components. The TV (type text) is nested in an IF snippet call:

  	[[IF? &subject=`[[*texthead]][[*textblock]]` &operator=`notempty` &then=`<div class="text">
		[[*texthead:notempty=`<h2>[[*texthead]]</h2>`]]
		[[*textblock]]
	</div>`]] <!-- shows nothing but a backtick

This construct crashes the content of this block itself and all following content. (There are more components, but this is the relevant part.)

If I set the texthead TV alone in an additional line I can see the correct output in frontend but not the output of the TV with output modifier.

  	[[IF? &subject=`[[*texthead]][[*textblock]]` &operator=`notempty` &then=`<div class="text">
		[[*texthead]]
		[[*texthead:notempty=`<h2>[[*texthead]]</h2>`]]
		[[*textblock]]
	</div>`]] <!-- shows "Test?" from the stand-alone TV followed by nothing -->

And now: VOODOO!
I tried this (isolated TV with output modifier before the IF snippet):

  	[[*texthead:notempty=`<h2>[[*texthead]]</h2>`]]
  	[[IF? &subject=`[[*texthead]][[*textblock]]` &operator=`notempty` &then=`<div class="text">
		[[*texthead]]
		[[*texthead:notempty=`<h2>[[*texthead]]</h2>`]]
		[[*textblock]]
	</div>`]]

And? I got the texthead-Value twice and correct as “Test?” No chrashes, no missing content. Seems there is a conflict with the IF snippet.

Is there an other solution to test if multiple TVs are not empty to put out a content construct? I use the snippet to test if one of the elements is not empty, then the whole block may be output. If all elements are empty, nothing should be output.

It seems the parser gets confused with the ? (probably because the ? character is used to separate the snippet-name from the properties).

I’d just write my own snippet. Something like:

<?php
$output = '';	
if (!empty($texthead) || !empty($textblock)){
    $output .= '<div class="text">';
    if (!empty($texthead)){
        $output .= '<h2>' . $texthead . '</h2>';
    }
    $output .= $textblock;
    $output .= '</div>';
}
return $output;

and call it with

[[mySnippet? &texthead=`[[*texthead]]` &textblock=`[[*textblock]]`]]
1 Like

It seems the parser gets confused with the ? (probably because the ? character is used to separate the snippet-name from the properties).

Maybe but it shouldn’t do that. It’s a regular text character within the TV.

I’d just write my own snippet.

Thanks for the hint. I prefer to use on-board resources if possible. To many self-written snippets are hard to maintain and I’m not a not a gifted programmer :smiley:

Now I have found a solution that works.

At first I replaced the IF snippet with the if/input modifier (it’s not documented well but it works). And I have split the headline:

[[if:input=`[[*texthead]][[*textblock]]`:notempty=`<div class="text">
	[[*texthead:notempty=`<h2>]][[*texthead]][[*texthead:notempty=`</h2>`]]
	[[*textblock]]
</div>`]]

Don’t know why splitting the headline is neccessary but it’s a solution for me.

could also be done with a MIGX TV
Configure a texthead and textblock field for the TV

Then they can add null or multiple such Blocks easyli

[[getImageList? 
&tvname = `textblocks`
&tpl=`@CODE:<div class="text">{{+texthead:notempty=`<h2>{{+texthead}}</h2>`}}{{+textblock}}</div>`
]]
1 Like

@bruno17 Thanks but the block is more complex in real configuration (plus image, alt text, optional image legend, image position, width of the block and an additional textarea TV for optional chunks/snippet calls).

Yes, it would be possible to build the blocks (5 optional blocks on every page) with MIGX.
Nice idea … :thinking:

What about the render performance with a MIGX construct? Is it fast enough compared to my solution for 5 blocks with all the TVs an the output modifiers?

I would say, it is faster, than having 5 conditional blocks

I would try to avoid conditionals like that, where possible

1 Like

its also possible to call different chunks for each item

other possible options are ContentBlocks or Fred

Thanks again. MIGX might be the possible and final solution. ContentBlocks is too expensive for this site owner (a midwifery practice).

This topic was automatically closed 2 days after discussion ended and a solution was marked. New replies are no longer allowed. You can open a new topic by clicking the link icon below the original post or solution and selecting “+ New Topic”.