Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Jquery的append函数中添加Smarty函数?_Jquery_Smarty - Fatal编程技术网

如何在Jquery的append函数中添加Smarty函数?

如何在Jquery的append函数中添加Smarty函数?,jquery,smarty,Jquery,Smarty,我需要包括一些具有Jquery功能的代码,但我还需要在其中插入Smarty的if。我完全被困在这里了 以下是我现在掌握的代码: HTML 标题 Jquery $("#body").append("<tr>" + "<td>{if $data.number eq 3}<a href="http://www.google.com">Google</a>{/if}</td>" +

我需要包括一些具有
Jquery
功能的代码,但我还需要在其中插入
Smarty
if
。我完全被困在这里了

以下是我现在掌握的代码:

HTML


标题
Jquery

$("#body").append("<tr>" +
                  "<td>{if $data.number eq 3}<a href="http://www.google.com">Google</a>{/if}</td>" +
                  "</tr>");
$(“#body”).append(“'+
“{if$data.number eq 3}{/if}”+
"");
但它将
Smarty
if
检测为文本,将no检测为Smarty函数。我确信我走错了路,但我找不到其他解决办法

我还尝试在
{if}
语句之前使用
{literal}{/literal}
,但其行为是相同的


提前谢谢

Smarty在服务器上运行,jQuery在客户端上运行,所以您不能在客户端上运行Smarty函数。然而,你可以这样做

// at first append empty row with data-number ($data-number should be echoed in php)
var currentRow = $('<tr data-number="$data.number"><tr />').appendTo('#body');

// then append Your link if condition is met
if(currentRow.attr('data-number') == 3) {
    currentRow.append('<a href="http://www.google.com">Google</a>')
}
//首先用数据号追加空行($php中应回显数据号)
var currentRow=$('').appendTo('#body');
//如果条件满足,则附加链接
if(currentRow.attr('data-number')==3){
currentRow.append(“”)
}

然而,它要求您在客户端上有可用的数据。是这样吗?

Smarty在服务器上运行,jQuery在客户端运行。所以,不,这不是它的工作原理。@Tomalak,我怎样才能或多或少地得到类似的行为呢?还是不可能?是的。有许多HTML模板引擎可用。我会选择一个具有客户端(即JS)和服务器端(即PHP)实现的,这样您就不需要切换模板方言,甚至可以重用模板。我想到了,或者它的表亲。对于这两个版本,JS和PHP都是可用的,只需稍加努力,您就可以摆脱Smarty。@Tomalak是否真的有必要只为一条if语句实现整个模板引擎?也就是说,您可以通过使用jQuery和自定义JS逻辑来实现相同的效果(动态生成的HTML),但是,如果您不仅仅是生成最基本的HTML,我建议您使用模板引擎将逻辑和表示分离开来。
$data.number
的数据来自PHP,所以我不这么认为:那么为什么不在后端进行检查呢?
// at first append empty row with data-number ($data-number should be echoed in php)
var currentRow = $('<tr data-number="$data.number"><tr />').appendTo('#body');

// then append Your link if condition is met
if(currentRow.attr('data-number') == 3) {
    currentRow.append('<a href="http://www.google.com">Google</a>')
}