Forum for discussing general topics related to Couch.
5 posts Page 1 of 1
I not sure is this error is going to cause any problem at the future:

Code: Select all
<script>
  $(function() {
    var availableTags = [
   <cms:pages masterpage='en/products.php'>
   "<cms:show k_page_title />",
    </cms:pages>
    ];
    $( "#s" ).autocomplete({
      source: availableTags
    });
  });
  </script>


I use autocomplete in search function, I need to show the k_page_title in autocomplete list, this line - "<cms:show k_page_title />", seems cause some syntax error, as my Dreamweaver point it as error. Is anyone has any idea, how can I fix this, or maybe I already write it in correct syntax and I can just ignore the error?
nsy wrote: Dreamweaver point it as error

I don't know much about D, though can guess that it doesn't know about Couch. So will throw errors on some unknown things in javascript. Does the page work as intended?

Edit: @KK has noticed the trailing comma, that I didn't notice. It will surely need to be corrected for the very last item.
Join COUCH:TALK channel here https://t.me/couchcms_chat
Ryazania — a framework to boost productivity with Add-ons viewtopic.php?f=2&t=13475
Support my efforts to help the community https://boosty.to/trendo/donate
@nsy, as @trendoman mentioned, Dreamweaver is not 'Couch aware', so understandably it'll balk at the unknown syntax thrown at it. You can ignore that safely.

That said, the generated JS should be of proper syntax else the script will fail.
Your approach is fine but there are two pit-falls to look out for -
1. JS arrays (unlike PHP) don't tolerate the trailing comma after the very last element
2. If the values being put into the array contain any double-quotes, they need to be escaped for disambiguation from the enclosing double-quotes (same applies for quotes if they are used for enclosing the values).

In your case, the k_page_title is guaranteed not to have quotes/double-quotes but the first point needs to be taken care of.

As an example, please see this - viewtopic.php?p=20334#p20334
@KK, Not really understand point 2...

But I just follow the example:

Code: Select all
<script>
  $(function() {
    var availableTags = [
   <cms:pages masterpage='en/products.php'>
      "<cms:addslashes><cms:show k_page_title/></cms:addslashes>"
      <cms:if "<cms:not k_paginated_bottom/>">,</cms:if>
    </cms:pages>
    ];
    $( "#s" ).autocomplete({
      source: availableTags
    });
  });
  </script>


I assume you wanted me to add <cms:addslashes></cms:addslashes>, and <cms:if "<cms:not k_paginated_bottom/>">,</cms:if>....
You are right (sorry for not being clearer) -
<cms:not k_paginated_bottom/> covers point one while
<cms:addslashes> covers point two.

The additional note was that in your case, using <cms:addslashes> is not really necessary as k_page_title can never contain quotes (will always be escaped by Couch).

Anyway, no harm in using it so now your code should run fine.
5 posts Page 1 of 1