سؤال

I want to use a variable in the if clause which is in jQuery template. Console log says:

Uncaught Syntax Error: Unexpected token {

Here is my code:

 var isActive = true;
                    var isPasive = false;
                    var isGuest = false;
        
                      var tmp = '<script>' +

            '{{each hastalar}}' +
                '<a href="#" class="patientRow" data-name="${$value.M_AdiSoyadi}" data-tc="${$value.M_TcKimlikNo}" data-tahlilgunu="${$value.M_TahlilGunu}"> ${M_AdiSoyadi}' +
                '{{if $value.M_HastaBulunmaDurumu == "1" && ${isActive} }}' +
                    '<img id="imgMember_${$value.M_TcKimlikNo}" src="images/greenmember.png" title="Hasta klinik ve DYOB kayıtlarıyla örtüşüyor." style="width:15px;height:15px;"/>' +

                '{{else  $value.M_HastaBulunmaDurumu == "2" }}' +
                   '<img id="imgMember_${M_TcKimlikNo}" src="images/bluemember.png" title="Hasta kliniğinizde mevcut fakat DYOB sisteminde sizin kliniğinizde görünmüyor. Lütfen DYOB sistmine hasta kaydını yapınız." style="width:15px;height:15px;"/>' +
                '{{else $value.M_HastaBulunmaDurumu == "3"}}' +
                    '<img id="imgMember_${M_TcKimlikNo}" src="images/redmember.png" title="Hasta kliniğinizde mevcut değil fakat DYOB sisteminde sizin kliniğinizde görünüyor. Lütfen kan tahlili yapılan hastaların listesini kontrol ediniz." style="width:15px;height:15px;"/>' +
                '{{/if}}' +
                '<img id="imgResult_${$value.M_TcKimlikNo}"/>' +
                '<img id="imgInfo_${$value.M_TcKimlikNo}"/>' +

                '</a>' +
            '{{/each}}' +
            '</script>';

I use jquery.tmpl.min.js. What should I do to use the variable in if clause?

هل كانت مفيدة؟

المحلول

For a generic approach of replacing your variables in JavaScript strings you could you the following snippet;

var tmp = '<script>' +
          ... +
          '<\/script>'
          .replace('${isActive}', isActive)
          .replace('${isPassive}', isPassive)
          .replace('${isGuest}', isGuest)

For a more detailed solution on your problem, we need information about the framework and template engine which you are using.

EDIT:

I noticed that the closing script tag causes an error. YOu might want to escape the closing script tag at the like so:

   <\/script>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top