Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How to execute volt syntax in js file

Hello phalcon community. I want to know the solution how to execute volt syntax in js file . if attach javascript syntax in file like index.volt it's work fine

example. I have index.volt in this file. I write javascript like this

$('#price_card').val('');
$('#support_card').val('');
$('#package_card').val('');
$('#description_card').val('');

$('#groupCardId').val('');
$('#groupCardPath').val('');

$('#groupCardId').val(groupId);
$('#showPreviewBtn').empty();

$('.chk_box').attr('checked', false);
$.uniform.update('.chk_box');

var html = '';
var path = '{{url()}}';
$.ajax({
    url: '{{url("groupusers/card")}}',
    type: 'GET',
    data: {value: groupId},
    dataType:'json',
})
.done(function(res) {
    if(res != 'new'){
        var path = '{{url("groupusers/previewcard/'+groupId+'")}}';
        var btn = '<a href="'+path+'" class="btn btn-warning" target="_blank" id="previewCard">Preview</a>';
        $('#showPreviewBtn').html(btn);

        $('#price_card').val(res.card_price);
        $('#support_card').val(res.card_support);
        $('#package_card').val(res.card_package);
        $('#description_card').val(res.card_description );

        $("input[name='check_btn'][value='" + res.card_path + "']").attr('checked', true);

    }
});

it can work when I write in index.volt but I try to save this script to script.js and attach in to index.volt it can't interpreter variable

var path = '{{url()}}';

Please give me an introduction or solution to resolv this problem. thank you p.s. sorry for my bad english



7.2k
Accepted
answer

I found the solution to resolv this problem. I declare global variable in javascript because I want to use only url() variable in volt template so this solution is works to me . thank you

 var $gp =  window.location.pathname.split( '/' );
 var $path = '/'+gp[1]+'/';