山魈合并标签不接受变量值

Posted

技术标签:

【中文标题】山魈合并标签不接受变量值【英文标题】:mandrill merge tags not accepting variable value 【发布时间】:2015-06-09 05:51:39 【问题描述】:

请原谅我的第一篇文章!我能够成功发送“硬编码” global_merge_vars

 `"autotext": "true",
  "merge": "true",
  "global_merge_vars": [
        
            "vars": 
                
                    "name": "LSMESSAGE",
                    "content": "hardcoded"
                

        
    ],

  "subject": "*|LSMESSAGE|*",
  "html": "<p> *|LSMESSAGE|* </p>"`

但我无法将名为 ctlsm 的变量传递给“内容”。

"autotext": "true",
  "merge": "true",
  "global_merge_vars": [
        
            "vars": 
                
                    "name": "LSMESSAGE",
                    "content": ctlsm
                

        
    ],

  "subject": "*|LSMESSAGE|*",
  "html": "<p> *|LSMESSAGE|* </p>"

我刚刚在我的邮件中收到了星号*|LSMESAGE|*星号。 第一个问题 - 是否可以在这里使用变量? (我确实尝试将 ctlsm 放在引号中:“ctlsm”) 如果是,我做错了什么?该变量是在山魈发布之前声明的,并且有一个值,但似乎在 $ajax 部分中失去了它的值。

// send email using mandrill and API key
      $.ajax(
type: "POST",
url: "https://mandrillapp.com/api/1.0/messages/send.json",
data:  etc etc

【问题讨论】:

【参考方案1】:

javascript中添加+号来连接字符串

"content": "+ctlsm+"   

这里是字符串变量的 concat 示例。

 var text = "hello" 
 document.write(text+" world"); /// this will write hello world

【讨论】:

如果你能描述一下就好了,为什么这样可以解决问题。 (VLQRQ) 很简单。如果你想在javascript中的任何字符串中使用变量,那么使用+运算符你可以在字符串中连接变量【参考方案2】:

感谢尼西特。现在可以了。从代码中删除了“var”,将“subject”和“html”代码移到顶部,并添加了“merge_language”:“mailchimp”,这是我在 mandril 站点上的代码 sn-p 中找到的。变量名 ctlsm 不带引号。无论如何,这成功了。这是有效的代码:

// send email using mandrill and API key$.ajax(
type: "POST",
url: "https://mandrillapp.com/api/1.0/messages/send.json",
data: 
"key": "yourapikeyhere",
"message": 
 "subject": "*|LSMESSAGE|*",
 "html": "<p>  *|LSMESSAGE|* </p>",
 "text": " *|LSMESSAGE|*",
 "from_email": "youremailhere",
  "to": [
      
        "email": "youremailhere",
        "name": "yournamehere",
        "type": "to"
      ,
      
        "email": "youremailhere",
        "name": "yournamehere",
        "type": "to"
      
    ],

  "autotext": "true",
  "merge": "true",
  "merge_language": "mailchimp",
  "global_merge_vars": [
        
           "name": "LSMESSAGE",
            "content": ctlsm

        
    ]




【讨论】:

以上是关于山魈合并标签不接受变量值的主要内容,如果未能解决你的问题,请参考以下文章

python学习列表/字典合并布尔型交换变量值列表转换字符串连接和分割

在 Laravel 中使用刀片模板在 php 标签内打印 JavaScript 变量值

如何更改内部类中的变量值[closed]

如何在 .html 标记内附加 jQuery 变量值

将 Django 变量值分配给 AngularJS

原本的变量名处在变量值的位置,如何重新提取变量名