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 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?