Output links with double and single quotes k#

Ron Rainey asked on January 16, 2019 19:16

I have a foreach loop in k# that outputs a series of links. (not using built in repeater for technical reasons) Works great until I have to include a google analytics event tag which requires both a single and double quote in its syntax. See example. How do I output this in such a way as to allow the quotes? Code that works peachy: {% foreach(title in headlines) { x++; print("<a href=" + links[x] +">"+title+"</a><hr style='margin: .25em 0 .5em 0;'/>")}; "ignored" } #%}

Breaks when I try to add a Google tag to that link such as: onclick="ga('send', 'event', 'MONKEY2', 'click', 'Banana2');"

I've tried several permutations to escape and work around the " and ' required. Example: print("<a href=" + links[x] +" onclick="ga('send', 'event', 'MONKEY2', 'click', 'Banana2');" >"+title+"</a><hr style='margin: .25em 0 .5em 0;'/>")

Really scratching my head on how to get this syntax right. Wondering if I have to break out of k# altogether? Thanks in advance for any help.

Recent Answers


Zach Perry answered on January 16, 2019 19:25

Did you try to escape the double quote with a backslash?

{% "onclick=\"ga('send', 'event', 'MONKEY2', 'click', 'Banana2');" %}

0 votesVote for this answer Mark as a Correct answer

Eric Dugre answered on January 16, 2019 19:38 (last edited on December 10, 2019 02:31)

As Zach stated, you can escape double-quotes in the macro. I would aslo recommend that you try to use open loops when possible. For example:

{% foreach(s in "a,b,c".Split(",")) { %}
    <a href="{%s%}">{%s%}</a><hr style="margin: .25em 0 .5em 0;"/>
{% } |(identity)GlobalAdministrator%}

This removes the need to escape or otherwise parse the output.

0 votesVote for this answer Mark as a Correct answer

Ron Rainey answered on January 16, 2019 20:32

Thanks guys those are both great answers. I didn't try the \ because I had read elsewhere in the forums that there was no way to escape characters in k#. I should have have tried it regardless.

Also thanks for the suggestion on open loops. I had seen some examples like that and was unsure about how the syntax was working. Very different than what I'm used to in PHP.

Cheers!

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.