We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3177
    • 137 Posts
    Hello,
    This seems like a simple Javascript question, but it’s not, since it’s typical for MODX and TV’s

    I want to pass a multiline TV (RichText) to a Javascript function.
    It’s done in one of the Wayfinder Template Chunks. And the TV holds additional information on the menu item. Together with the Balloon Javascript (see http://gmod.org/wiki/Popup_Balloons) I want to pass the TV to the balloon function which generates the popup when a visitor hovers over the menu item.
    - My WayFinder innerRowTpl looks like this:
    [tt]<div class="menu3">
    <h3 id="menuid[+wf.docid+]" onmouseover="plainBox.showTooltip(event,’[+menuText+]’);" ><a href="[+wf.link+]">[+wf.linktext+] </a></h3>
    </div>[/tt]
    - The TV is called menuText and it holds a piece of HTML (including newlines)

    The problem:

    When I want to pass the TV to a Javascript function it gives me the error "unterminated string literal"
    because the [+menuText+] is put between quotes and the closing quote is on another line.

    Why put this in the MODX forums and not hassle some Javascript forum:
    Because all Javascript forums tell you of the "\" line continuation option. But I cannot use that here
    because I would need to preprocess the TV.

    Is there a solution to PreProcess a TV? Either at the moment it is saved to the database, or at the moment
    it is used in a chunk? Then I can remove all the newlines in PHP.

    Thanks in advance for your help.

    Bert Catsburg
      • 28042 ☆ A M B ☆
      • 24,524 Posts
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 3177
        • 137 Posts
        Yes, that worked. Actually a very simple and straightforward solution. Why did I not think of that smiley

        Small thing I had to solve, and which I want to share with the community:

        The call in innerRowTpl is:
        <div class="menu3">
           <h3 id="menuid[+wf.docid+]" onmouseover="plainBox.showTooltip(event,'[[showMenuText? &docid=`[+wf.docid+]`]]');" ><a href="[+wf.link+]">[+wf.linktext+]</a></h3>
        </div>


        But the showMenuText wants to return a surrounding <div class="MyMenuText"></div>.
        The double quote spoils things since that’s also the surrounding quote of the onmouseover.
        Also, when a double quote is typed in the menuText TV, than you get the same problem.
        I had to replace the double quotes with %22.
        Final snippet:

        <?php 
        	$o = ""; // The Output
        	if (!isset($docid)) return $o;
           
        	$tv = $modx->getTemplateVar('menuText', "", $docid);
        	$menuText = $tv["value"];
        	$menuText = preg_replace("/[\n\r]/"," ",$menuText); 
        	$menuText = preg_replace("/\"/","%22",$menuText); 
        	
        	$o = '<div class=' . urlencode("\"") . 'popupMenuText' . urlencode("\"") . '>' . $menuText . '</div>';
        
        	return $o;
        ?>