最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

convert smarty array to javascript array - Stack Overflow

programmeradmin2浏览0评论

I have a smarty array

$promoFormData  Smarty_Variable Object (3)
                        ->value = Array (1)
                            deliveryDates => Array (3)
                                 0 => Array (2)
                                      startDate => "2013/06/05"
                                      endDate => "2013/06/28"
                                1 => Array (2)
                                      startDate => "2013/07/05"
                                      endDate => "2013/07/28"
                                2 => Array (2)
                                      startDate => "2013/08/05"
                                      endDate => "2013/08/28"

I want to use this array deliveryDates as available dates in datepicker. So trying to convert the above as the following Javascript array

var ranges = [ { start: new Date(2013, 06, 05), end: new Date(2013, 06, 28) },
               { start: new Date(2013, 07, 05), end: new Date(2013, 07, 28) },
               { start: new Date(2013, 08, 05), end: new Date(2013, 07, 28) } ];

I have tried using below code:

 <script>
    var js_array = new Array();
    {{foreach from=$promoFormData.deliveryDates item=array_item key=id}}
        {{foreach from=$array_item item=sub_array_item key=index}}
            js_array['{{$id}}']['{{$index}}'] = '{{$sub_array_item}}';
        {{/foreach}}
    {{/foreach}}

    console.log(js_array);
</scirpt>

And I am getting the below error

     TypeError: js_array[0] is undefined
     js_array['0']['startDate'] = '2013/06/05';

Anyone please guide me to the right way.

I have a smarty array

$promoFormData  Smarty_Variable Object (3)
                        ->value = Array (1)
                            deliveryDates => Array (3)
                                 0 => Array (2)
                                      startDate => "2013/06/05"
                                      endDate => "2013/06/28"
                                1 => Array (2)
                                      startDate => "2013/07/05"
                                      endDate => "2013/07/28"
                                2 => Array (2)
                                      startDate => "2013/08/05"
                                      endDate => "2013/08/28"

I want to use this array deliveryDates as available dates in datepicker. So trying to convert the above as the following Javascript array

var ranges = [ { start: new Date(2013, 06, 05), end: new Date(2013, 06, 28) },
               { start: new Date(2013, 07, 05), end: new Date(2013, 07, 28) },
               { start: new Date(2013, 08, 05), end: new Date(2013, 07, 28) } ];

I have tried using below code:

 <script>
    var js_array = new Array();
    {{foreach from=$promoFormData.deliveryDates item=array_item key=id}}
        {{foreach from=$array_item item=sub_array_item key=index}}
            js_array['{{$id}}']['{{$index}}'] = '{{$sub_array_item}}';
        {{/foreach}}
    {{/foreach}}

    console.log(js_array);
</scirpt>

And I am getting the below error

     TypeError: js_array[0] is undefined
     js_array['0']['startDate'] = '2013/06/05';

Anyone please guide me to the right way.

Share Improve this question edited May 30, 2013 at 22:57 Swathi asked May 30, 2013 at 22:49 SwathiSwathi 3231 gold badge4 silver badges11 bronze badges 2
  • I don't know what you are asking. The array literal "works" (be aware that in javascript new Date(2013, 06, 05) is 5 July 2013). The script you have posted is not javascript. Are you trying to use Smarty on the server to create an array literal for javascript? So is this really just a Smarty question? – RobG Commented May 30, 2013 at 23:22
  • @RobG Yes I am trying to create array literal in javascript using smarty array. Here Iam using smarty to loop through the array – Swathi Commented May 31, 2013 at 0:31
Add a ment  | 

1 Answer 1

Reset to default 9

If your Smarty security settings allow it (specifically, it is in $php_modifiers), you could use PHP's json_encode function:

var js_array = {$promoFormData.deliveryDates|json_encode};

Since JSON is a strict subset of actual JavaScript object literal syntax, this should give you a valid JS declaration.

发布评论

评论列表(0)

  1. 暂无评论