doulou1970 2014-09-23 17:53
浏览 40
已采纳

如何使用Smarty将带有新行的文本转换为JavaScript新行

I have a Template that uses Smart in a SugarCRM Module I am building.

The smarty code in question is below, it basically iterates over a PHP Array calling a JavaScript function for each Array Item...

{foreach from=$TASKS key=key item=task}
    add_task_row("{$task.task_id}","{$ID}","{$task.name}","{$task.description}","{$task.status}","{$task.priority}","{$task.type}","{$task.sort_order}","{$task.heading}");
{/foreach}

And here is the JavaScript Function shown above...

function add_task_row(taskid,projectid,name,description,status,priority,type,sort,heading){

    var ii = document.getElementById("project_tasks");
    var num = document.getElementById("tasks_count").value;

    // If TaskID is EMPTY, then assign it a new one from the row count
    var taskIDTimeStamp = new Date().getTime();
    //console.log('ID1: '+id);
    if(taskid){
        var taskid = taskid;
    }else{
        var taskid = taskIDTimeStamp;
    }

    num++;

    //var prioritySelHtml = createOptions(priority);

    var e = document.createElement("div");
    e.setAttribute('id','task_'+num);
    e.setAttribute('class','task_row');

    e.innerHTML =  '<span class="handle"></span>';

    e.innerHTML +=  '<input name="taskid_'+num+'" id="taskid_'+num+'" size=15 type="hidden" value="'+taskid+'">';

    e.innerHTML +=  '<input name="projectid_'+num+'" id="projectid_'+num+'" size=15 type="hidden" value="'+projectid+'">';

    e.innerHTML +=  '<input name="sort_order_'+num+'" id="sort_order_'+num+'" class="sort_order" size=15 type="hidden" value="'+sort+'">';

    e.innerHTML +=  '<input name="heading_'+num+'" id="heading_'+num+'" class="heading" size=15 type="hidden" value="'+heading+'">';


    if(heading == 1){

        // Add a Task Heading
        e.innerHTML +=  '<input name="name_'+num+'" id="name_'+num+'" class="task-heading" size=45 type="text" value="'+name+'" placeholder="Type a Project Task List Heading Here...">';
        e.innerHTML += '<div style="display:none; float: left; width:  400px;"><textarea name="description_'+num+'" id="description_'+num+'" rows="4" cols="50">'+description+'</textarea></div>';
        e.innerHTML += '<div style="display:none; float: left; width:  100px;"><select name="status_'+num+'" id="status_'+num+'" class="status">'+buildFormSelection(statusArray, status)+'</select></div>';
        e.innerHTML += '<div style="display:none; float: left; width:  90px;"><select name="priority_'+num+'" id="priority_'+num+'" class="priority">'+buildFormSelection(prioritiesArray, priority)+'</select></div>';
        e.innerHTML += '<div style="display:none; float: left; width:  100px;"><select name="type_'+num+'" id="type_'+num+'" class="type">'+buildFormSelection(typesArray, type)+'</select></div>';
    }else{


    // Add a Task
    e.innerHTML +=  '<input name="name_'+num+'" id="name_'+num+'" size=45 type="text" value="'+name+'">';

        //e.innerHTML += '<input name="description_'+num+'" id="description_'+num+'" size=45 type="text" value="'+description+'">';
        e.innerHTML += '<textarea name="description_'+num+'" id="description_'+num+'" class="edit_description" rows="1" cols="50">'+description+'</textarea>';
        e.innerHTML += '<select name="status_'+num+'" id="status_'+num+'" class="status">'+buildFormSelection(statusArray, status)+'</select>';
        e.innerHTML += '<select name="priority_'+num+'" id="priority_'+num+'" class="priority">'+buildFormSelection(prioritiesArray, priority)+'</select>';
        e.innerHTML += '<select name="type_'+num+'" id="type_'+num+'" class="type">'+buildFormSelection(typesArray, type)+'</select>';
    }

    //e.innerHTML += '<button type="button" onclick="remove_item_row('+num+')"><img src="index.php?entryPoint=getImage&imageName=id-ff-clear.png"></button>';
    e.innerHTML += '<button type="button" onclick="remove_item_row('+num+')"><img src="./modules/apoll_Web_Projects/assets/images/cross.png"></button>';

    e.innerHTML += '<br style="clear:both;">';

    document.getElementById("tasks_count").value = num;
    ii.appendChild(e);

}

Everytime my JavaScript function add_task_row() is called, it inserts a new Project Tasks Div into the DOM and populates form fields in each Row.

This works great most of the time, however I just saved a different value to the Database and now when it loads this value, it causes JavaScript Errors like this... Uncaught SyntaxError: Unexpected token ILLEGAL

When I view the page source I can see that it is 1 database record causing the problem in my source code....

add_task_row("1411445999407","205e34c6-7381-92eb-e6ab-54125429cd2a","ghjfghjh","dfg dhfjhg dfgosdjkfgosdfk hgdjfhdhikfgj gsidgisfdgh
fh
dfgh
d tgh
tgj
hfghj
fg hjgh0dgoh igkoiiuwidth: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;width: 679px;","In Progress","Low","Other","3","0");

So in this above code, you can see that this long database value is not wrapped in Quotes for some reason.

Any idea how to fix this issue that only happens when the DB value has like special characters it seems?


UPDATE, upon further inspection, I see that the long value is wrapped in Quotes, and with more testing, it is not the special characters that caused the problem, it is the line breaks!

add_task_row("1411445999407","205e34c6-7381-92eb-e6ab-54125429cd2a","ghjfghjh","gdf;dgdfdfg 679px;width: 679px;width: 679px;
dsgdfgsdfg


dfgsdfgsdfg


dfgsdfg","In Progress","Low","Other","3","0");

Any ideas how I can make strings with these line breaks not cause JavaScript errors?

  • 写回答

1条回答 默认 最新

  • duanchigeng4313 2014-09-23 18:28
    关注

    This is how you put JavaScript strings into source. You cannot simply go to new line.

    For example the following code is working

    alert('1 2 3 4');
    

    but the following does not:

    alert('1
    2 3 4');
    

    and the following again works:

    alert('1' +
    '2 3 4');
    

    If you want to make any changes in Smarty you could do it this way:

    {assign var="string" value="1
    2
    3
    4"}
    <script>
    
        alert ("{$string|replace:array("
    ",""):array(' ', '')}");
    </script>
    

    using replace modifier you change and into space - it makes small content change but if you don't care it should work.

    The other solution is to convert new line endings into plain string that makes JavaScript to treat them as new line so you could change above example into:

    {assign var="string" value="1
    2
    3
    4"}
    <script>
    
    alert ("{$string|replace:array("
    ",""):array('
    ', '
    ')}");
    </script>
    

    and now in alert you will show new lines.

    So in your case you should probably change line:

    add_task_row("{$task.task_id}","{$ID}","{$task.name}","{$task.description}","{$task.status}","{$task.priority}","{$task.type}","{$task.sort_order}","{$task.heading}");
    

    into

    add_task_row("{$task.task_id}","{$ID}","{$task.name}","{$task.description|replace:array("
    ",""):array('
    ', '
    ')}","{$task.status}","{$task.priority}","{$task.type}","{$task.sort_order}","{$task.heading}");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Matlab在app上输入带有矩阵形式的初始条件发生错误
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器