{"_id":"575007bf4630d30e0018385f","category":{"_id":"569743020b09a41900b2446d","__v":3,"pages":["56975be30b09a41900b24494","56a15cfd470ae00d00c3061f","56e356004dc30f0e0051b3ed"],"project":"5668fab608f90021008e882f","version":"5668fab608f90021008e8832","sync":{"url":"","isSync":false},"reference":true,"createdAt":"2016-01-14T06:41:06.893Z","from_sync":false,"order":2,"slug":"javascript-sdk","title":"JavaScript SDK"},"project":"5668fab608f90021008e882f","version":{"_id":"5668fab608f90021008e8832","__v":19,"project":"5668fab608f90021008e882f","createdAt":"2015-12-10T04:08:22.769Z","releaseDate":"2015-12-10T04:08:22.769Z","categories":["5668fab708f90021008e8833","569740f124490c3700170a64","569742b58560a60d00e2c25d","569742bd0b09a41900b2446c","569742cd69393517000c82b3","569742f459a6692d003fad8f","569743020b09a41900b2446d","5697430b69393517000c82b5","56a17776470ae00d00c30642","56a2c48a831e2a0d0069b1ad","56b535757bccae0d00e9a1cd","56e1ff6aa49fdc0e005746b5","57e1c88115bf6522002a5e4e","57fa65275ba65a17008b988f","57fbeea34002550e004c032e","58474584889b6c2d00fb86e9","58475dcc64157f0f002f1907","587e7b5158666c2700965d4e","58a349fc30852819007ba083"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.18.0","version":"1.18"},"user":"5668fa9755e4b32100935d41","githubsync":"","__v":14,"parentDoc":null,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-06-02T10:17:35.029Z","link_external":false,"link_url":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":true,"order":12,"body":"`$p(\"render\"): (String, Object, Function, [String]) -> undefined`\n[block:callout]\n{\n  \"type\": \"info\",\n  \"title\": \"Technical documentation\",\n  \"body\": \"This page includes technical documentation around the render function, used as part of the code to render LiftIgniter recommendations. For broader context around rendering, see our documentation on [Rendering Widgets](doc:rendering-widgets).\"\n}\n[/block]\n$p(\"render\") takes in the template, item (to render on widget), callback, and array of strings to return. For example, let's say that our recommendation returns:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var item = {\\n\\turl: \\\"www.google.com\\\",\\n  title: \\\"google\\\",\\n  description: \\\"search engine\\\",\\n  thumbnail: \\\"www.google.com/favicon.ico\\\"\\n}\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nAnd we want it to render it in the following form:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"<div>\\n<a href=\\\"www.google.com\\\"><img src=\\\"www.google.com/favicon.ico\\\"><p>Click here to go to Google</p></a>\\n<p>search engine</p>\\n</div>\",\n      \"language\": \"html\"\n    }\n  ]\n}\n[/block]\nYou can see the specific use case in [this documentation](https://liftigniter.readme.io/docs/rendering-widgets). But you can define a Mustache template and run our `render` function along with the `item` defined above:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var template = '<div>' +\\n\\t'<a href=\\\"{{url}}\\\"><img src=\\\"{{thumbnail}}\\\"><p>{{title}}</p></a>' +\\n\\t'<p>{{description}}</p>' +\\n\\t'</div>'\\n  \\nvar item = {\\n\\turl: \\\"www.google.com\\\",\\n  title: \\\"google\\\",\\n  description: \\\"search engine\\\",\\n  thumbnail: \\\"www.google.com/favicon.ico\\\"\\n}\\n\\n// If there is no callback, then the function returns the result string.\\n// In this case it prints result string.\\n$p(\\\"render\\\", template, item, function(result){return result})\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n`render` function will return what we wanted.\n\nHowever, you may not want to use `{{}}` brackets due to collision with Google Tag Manager or other sort of template you have on your site. In that case, you can configure the brackets to an arbitrary field by passing in an array containing string expression for custom left bracket and right bracket.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"// Note that we are using '<%' and '%>' as a substitute to '{{' and '}}'\\nvar template = '<div>' +\\n\\t'<a href=\\\"{%url%}\\\"><img src=\\\"{%thumbnail%}\\\"><p><%title%></p></a>' +\\n\\t'<p>{%description%}</p>' +\\n\\t'</div>'\\n  \\nvar item = {\\n\\turl: \\\"www.google.com\\\",\\n  title: \\\"google\\\",\\n  description: \\\"search engine\\\",\\n  thumbnail: \\\"www.google.com/favicon.ico\\\"\\n}\\n\\n// You can configure the left and right bracket by passing in array of string in which first element and second element correspond to left and right brackets respectviely.\\n$p(\\\"render\\\", template, item, function(result){return result}, ['{%','%}'])\\n\\n// if you don't want to define the callback,\\n$p(\\\"render\\\", template, item, undefined, ['{%','%}'])\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"prender","type":"fn","title":"$p(\"render\")"}
`$p("render"): (String, Object, Function, [String]) -> undefined` [block:callout] { "type": "info", "title": "Technical documentation", "body": "This page includes technical documentation around the render function, used as part of the code to render LiftIgniter recommendations. For broader context around rendering, see our documentation on [Rendering Widgets](doc:rendering-widgets)." } [/block] $p("render") takes in the template, item (to render on widget), callback, and array of strings to return. For example, let's say that our recommendation returns: [block:code] { "codes": [ { "code": "var item = {\n\turl: \"www.google.com\",\n title: \"google\",\n description: \"search engine\",\n thumbnail: \"www.google.com/favicon.ico\"\n}", "language": "javascript" } ] } [/block] And we want it to render it in the following form: [block:code] { "codes": [ { "code": "<div>\n<a href=\"www.google.com\"><img src=\"www.google.com/favicon.ico\"><p>Click here to go to Google</p></a>\n<p>search engine</p>\n</div>", "language": "html" } ] } [/block] You can see the specific use case in [this documentation](https://liftigniter.readme.io/docs/rendering-widgets). But you can define a Mustache template and run our `render` function along with the `item` defined above: [block:code] { "codes": [ { "code": "var template = '<div>' +\n\t'<a href=\"{{url}}\"><img src=\"{{thumbnail}}\"><p>{{title}}</p></a>' +\n\t'<p>{{description}}</p>' +\n\t'</div>'\n \nvar item = {\n\turl: \"www.google.com\",\n title: \"google\",\n description: \"search engine\",\n thumbnail: \"www.google.com/favicon.ico\"\n}\n\n// If there is no callback, then the function returns the result string.\n// In this case it prints result string.\n$p(\"render\", template, item, function(result){return result})", "language": "javascript" } ] } [/block] `render` function will return what we wanted. However, you may not want to use `{{}}` brackets due to collision with Google Tag Manager or other sort of template you have on your site. In that case, you can configure the brackets to an arbitrary field by passing in an array containing string expression for custom left bracket and right bracket. [block:code] { "codes": [ { "code": "// Note that we are using '<%' and '%>' as a substitute to '{{' and '}}'\nvar template = '<div>' +\n\t'<a href=\"{%url%}\"><img src=\"{%thumbnail%}\"><p><%title%></p></a>' +\n\t'<p>{%description%}</p>' +\n\t'</div>'\n \nvar item = {\n\turl: \"www.google.com\",\n title: \"google\",\n description: \"search engine\",\n thumbnail: \"www.google.com/favicon.ico\"\n}\n\n// You can configure the left and right bracket by passing in array of string in which first element and second element correspond to left and right brackets respectviely.\n$p(\"render\", template, item, function(result){return result}, ['{%','%}'])\n\n// if you don't want to define the callback,\n$p(\"render\", template, item, undefined, ['{%','%}'])", "language": "javascript" } ] } [/block]