samedi 25 avril 2015

If statements with jQuery variables in an x-tmpl script

so I have a x-tmpl script at the bottom of my page. This script prints out a table of rows for each record in a file. This is part of the script:

<script id="template-download" type="text/x-tmpl">
<td>

            {% if (file.deleteUrl) { %}
                <button class="btn btn-danger delete" data-type="{%=file.deleteType%}" data-url="{%=file.deleteUrl%}"{% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
                    <i class="glyphicon glyphicon-trash"></i>
                    <span>Delete</span>
                </button>
                <input type="checkbox" name="delete" value="1" class="toggle">
            {% } else { %}
                <button class="btn btn-warning cancel">
                    <i class="glyphicon glyphicon-ban-circle"></i>
                    <span>Cancel</span>
                </button>
            {% } %}
</td>
</script>

I want to check if the variable in my URL (?user=xxx) is not equal to a text value to not show the delete button in the script. The values I am trying to compare are defined in my javascript:

var phpuser = '<?php echo $tuser; ?>'; //where $tuser is the GET variable
var tusername= $('#myname').text();

I have tried to to the following:

<script id="template-download" type="text/x-tmpl">
var phpuser = '<?php echo $tuser; ?>';
var tusername= $('#myname').text();

<td>
        {% if ((file.deleteUrl)  && (phpuser==tusername)) { %}
            <button class="btn btn-danger delete" data-type="{%=file.deleteType%}" data-url="{%=file.deleteUrl%}"{% if (file.deleteWithCredentials) { %} data-xhr-fields='{"withCredentials":true}'{% } %}>
                <i class="glyphicon glyphicon-trash"></i>
                <span>Delete</span>
            </button>
            <input type="checkbox" name="delete" value="1" class="toggle">
        {% } else { %}
            <button class="btn btn-warning cancel">
                <i class="glyphicon glyphicon-ban-circle"></i>
                <span>Cancel</span>
            </button>
        {% } %}
</td>
</script>

However, the console comes up with "phpuser is not defined". Can anyone give any insight please? Cheers.

Aucun commentaire:

Enregistrer un commentaire