"use strict";(self.webpackChunkdocs=self.webpackChunkdocs||[]).push([["2252"],{3762:function(e,t,s){s.r(t),s.d(t,{config:()=>c,config2:()=>h,default:()=>g,frontMatter:()=>a,metadata:()=>n,assets:()=>d,params:()=>u,toc:()=>p,contentTitle:()=>l});var n=JSON.parse('{"id":"languages/liquid","title":"Liquid","description":"LiquidJS: A simple, expressive and safe template engine.","source":"@site/docs/languages/liquid.mdx","sourceDirName":"languages","slug":"/languages/liquid","permalink":"/docs/languages/liquid","draft":false,"unlisted":false,"editUrl":"https://github.com/live-codes/livecodes/tree/develop/docs/docs/languages/liquid.mdx","tags":[],"version":"current","frontMatter":{},"sidebar":"docsSidebar","previous":{"title":"Lightning CSS","permalink":"/docs/languages/lightningcss"},"next":{"title":"LiveScript","permalink":"/docs/languages/livescript"}}'),i=s("5893"),r=s("65"),o=s("3365");let a={},l="Liquid",d={},c={markup:{language:"liquid",content:"Hello, {{ name | capitalize }}!"},customSettings:{template:{data:{name:"livecodes"}}}},u={compiled:"open"},h={markup:{language:"liquid",content:"Hello, {{ name | capitalize }}!"},script:{language:"javascript",content:'window.livecodes.templateData = { name: "livecodes" };'},customSettings:{template:{prerender:!1}},activeEditor:"script"},p=[{value:"Usage",id:"usage",level:2},{value:"Pre-rendered (Default)",id:"pre-rendered-default",level:3},{value:"Dynamic",id:"dynamic",level:3},{value:"Language Info",id:"language-info",level:2},{value:"Name",id:"name",level:3},{value:"Extensions",id:"extensions",level:3},{value:"Editor",id:"editor",level:3},{value:"Compiler",id:"compiler",level:2},{value:"Version",id:"version",level:3},{value:"Code Formatting",id:"code-formatting",level:2},{value:"Custom Settings",id:"custom-settings",level:2},{value:"Example Usage",id:"example-usage",level:2},{value:"Pre-rendered",id:"pre-rendered",level:3},{value:"Dynamic",id:"dynamic-1",level:3},{value:"Links",id:"links",level:2}];function m(e){let t={a:"a",code:"code",h1:"h1",h2:"h2",h3:"h3",header:"header",li:"li",p:"p",pre:"pre",strong:"strong",ul:"ul",...(0,r.a)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(t.header,{children:(0,i.jsx)(t.h1,{id:"liquid",children:"Liquid"})}),"\n",(0,i.jsxs)(t.p,{children:[(0,i.jsx)(t.a,{href:"https://liquidjs.com/",children:"LiquidJS"}),": A simple, expressive and safe template engine."]}),"\n",(0,i.jsx)(t.h2,{id:"usage",children:"Usage"}),"\n",(0,i.jsx)(t.p,{children:"There are 2 modes for rendering:"}),"\n",(0,i.jsx)(t.h3,{id:"pre-rendered-default",children:"Pre-rendered (Default)"}),"\n",(0,i.jsxs)(t.p,{children:["The values of the expressions are evaluated and added to the template during compilation of the ",(0,i.jsx)(t.a,{href:"/docs/features/result",children:"result page"}),"."]}),"\n",(0,i.jsxs)(t.p,{children:["The values of all expressions should be supplied in advance using ",(0,i.jsx)(t.a,{href:"/docs/advanced/custom-settings",children:"custom settings"})," to the property ",(0,i.jsx)(t.code,{children:"template.data"})," which accepts an object of key-value pairs."]}),"\n",(0,i.jsxs)(t.p,{children:["Example: This provides the value of the expression ",(0,i.jsx)(t.code,{children:"name"})]}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-json",metastring:'title="Custom Settings"',children:'{\n "template": {\n "data": {\n "name": "LiveCodes"\n }\n }\n}\n'})}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.a,{href:"#pre-rendered",children:"Full example below"})}),"\n",(0,i.jsx)(t.h3,{id:"dynamic",children:"Dynamic"}),"\n",(0,i.jsxs)(t.p,{children:["To use this mode, the property ",(0,i.jsx)(t.code,{children:"template.prerender"})," in ",(0,i.jsx)(t.a,{href:"/docs/advanced/custom-settings",children:"custom settings"})," should be set to ",(0,i.jsx)(t.code,{children:"false"}),"."]}),"\n",(0,i.jsx)(t.p,{children:"Example:"}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-json",metastring:'title="Custom Settings"',children:'{\n "template": {\n "prerender": false\n }\n}\n'})}),"\n",(0,i.jsxs)(t.p,{children:["In this mode, in addition to values supplied in custom settings (see above), expressions can have values that are evaluated during the ",(0,i.jsx)(t.a,{href:"/docs/features/result",children:"result page"})," runtime."]}),"\n",(0,i.jsxs)(t.p,{children:["This can be achieved in JavaScript (or any ",(0,i.jsx)(t.a,{href:"/docs/languages/",children:"language"})," that compiles to it) by assigning ",(0,i.jsx)(t.code,{children:"window.livecodes.templateData"})," to an object with the data."]}),"\n",(0,i.jsxs)(t.p,{children:["Please note that template rendering occurs on ",(0,i.jsx)(t.a,{href:"https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event",children:"page load"}),", so the assignment must occur before that."]}),"\n",(0,i.jsx)(t.p,{children:"Example:"}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-js",metastring:'title="Script Editor (JS)"',children:"window.livecodes.templateData = { name: 'LiveCodes' };\n"})}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.a,{href:"#dynamic-1",children:"Full example below"})}),"\n",(0,i.jsx)(t.h2,{id:"language-info",children:"Language Info"}),"\n",(0,i.jsx)(t.h3,{id:"name",children:"Name"}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.code,{children:"liquid"})}),"\n",(0,i.jsx)(t.h3,{id:"extensions",children:"Extensions"}),"\n",(0,i.jsxs)(t.p,{children:[(0,i.jsx)(t.code,{children:".liquid"}),", ",(0,i.jsx)(t.code,{children:".liquidjs"})]}),"\n",(0,i.jsx)(t.h3,{id:"editor",children:"Editor"}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.code,{children:"markup"})}),"\n",(0,i.jsx)(t.h2,{id:"compiler",children:"Compiler"}),"\n",(0,i.jsxs)(t.p,{children:["The official ",(0,i.jsx)(t.a,{href:"https://www.npmjs.com/package/liquidjs",children:"LiquidJS compiler"}),"."]}),"\n",(0,i.jsx)(t.h3,{id:"version",children:"Version"}),"\n",(0,i.jsxs)(t.p,{children:[(0,i.jsx)(t.code,{children:"liquidjs"}),": v10.14.0"]}),"\n",(0,i.jsx)(t.h2,{id:"code-formatting",children:"Code Formatting"}),"\n",(0,i.jsxs)(t.p,{children:["Using ",(0,i.jsx)(t.a,{href:"https://prettier.io/",children:"Prettier"}),"."]}),"\n",(0,i.jsx)(t.h2,{id:"custom-settings",children:"Custom Settings"}),"\n",(0,i.jsxs)(t.p,{children:[(0,i.jsx)(t.a,{href:"/docs/advanced/custom-settings",children:"Custom settings"})," added to the property ",(0,i.jsx)(t.code,{children:"liquid"})," are passed as a JSON object to the ",(0,i.jsx)(t.a,{href:"https://liquidjs.com/api/classes/Liquid.html",children:"Liquid constructor"}),". Please check the ",(0,i.jsx)(t.a,{href:"https://liquidjs.com/tutorials/options.html",children:"documentation"})," for full reference."]}),"\n",(0,i.jsx)(t.p,{children:"Please note that custom settings should be valid JSON (i.e. functions are not allowed)."}),"\n",(0,i.jsx)(t.p,{children:(0,i.jsx)(t.strong,{children:"Example:"})}),"\n",(0,i.jsx)(t.pre,{children:(0,i.jsx)(t.code,{className:"language-json",metastring:'title="Custom Settings"',children:'{\n "liquid": {\n "outputDelimiterLeft": "<%=",\n "outputDelimiterRight": "%>"\n }\n}\n'})}),"\n",(0,i.jsx)(t.h2,{id:"example-usage",children:"Example Usage"}),"\n","\n",(0,i.jsx)(t.h3,{id:"pre-rendered",children:"Pre-rendered"}),"\n","\n","\n",(0,i.jsx)(o.Z,{config:c,params:u}),"\n",(0,i.jsx)(t.h3,{id:"dynamic-1",children:"Dynamic"}),"\n","\n",(0,i.jsx)(o.Z,{config:h}),"\n",(0,i.jsx)(t.h2,{id:"links",children:"Links"}),"\n",(0,i.jsxs)(t.ul,{children:["\n",(0,i.jsx)(t.li,{children:(0,i.jsx)(t.a,{href:"https://liquidjs.com/",children:"Official website"})}),"\n",(0,i.jsx)(t.li,{children:(0,i.jsx)(t.a,{href:"https://liquidjs.com/tutorials/intro-to-liquid.html",children:"Liquidjs tutorials"})}),"\n"]})]})}function g(e={}){let{wrapper:t}={...(0,r.a)(),...e.components};return t?(0,i.jsx)(t,{...e,children:(0,i.jsx)(m,{...e})}):m(e)}},3365:function(e,t,s){s.d(t,{Z:()=>f});var n=s("5893"),i=s("4200"),r=s("7294"),o=s("8294");function a(e){let t=(0,r.useRef)(null),[s,i]=(0,r.useState)(e.className||""),[a,l]=(0,r.useState)(e.style||{}),[d,c]=(0,r.useState)(e.height),[u,h]=(0,r.useState)(),[p,m]=(0,r.useState)(JSON.stringify(e.config||"")),[g,f]=(0,r.useState)("");return(0,r.useEffect)(()=>{if(!t.current)return;let{className:s,style:n,height:r,sdkReady:a,config:d,...v}=e;if(i(s||""),l(n||{}),c(r),u&&g===JSON.stringify(v)){if(p===JSON.stringify(d))return;m(JSON.stringify(d)),"string"==typeof d?fetch(d).then(e=>e.json()).then(e=>{u?.setConfig(e)}):d&&u.setConfig(d)}else f(JSON.stringify(v)),u?.destroy(),(0,o.T)(t.current,{config:d,...v}).then(e=>{h(e),"function"==typeof a&&a(e)})},[e]),(0,r.useEffect)(()=>()=>{u?.destroy()},[]),(0,n.jsx)("div",{ref:t,className:s,style:a,"data-height":d})}var l=s("1858"),d=s("3262"),c=s("1705"),u=s("7645"),h=s("8168"),p=s("8228"),m=s("5050");function g(e){let[t,s]=(0,r.useState)(e.js),[i,o]=(0,r.useState)(e.ts),[a,l]=(0,r.useState)(e.react),[g,f]=(0,r.useState)(e.vue),[v,j]=(0,r.useState)(e.svelte),x="3.7rem",[y,w]=(0,r.useState)(!0),[b,S]=(0,r.useState)(x),C=(0,r.useRef)(null),E=()=>{setTimeout(()=>{S(`calc(${C.current.offsetHeight}px + ${x})`)},5),setTimeout(()=>{S(`calc(${C.current.offsetHeight}px + ${x})`)},255)};return(0,r.useEffect)(()=>{if(d.Z.canUseDOM){let e=function(e){let t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:"js";try{return window.prettier?.format(e,{parser:"html"===t?"html":"babel",plugins:window.prettierPlugins})}catch{return e}};s(e(t,"js")),o(e(i,"ts")),l(e(a,"jsx")),f(e(g,"html")),j(e(v,"html"))}},[]),(0,n.jsxs)("details",{className:`alert alert--info ${m.Z.details} ${p.Z.details}`,"data-collapsed":y,style:{height:y?x:b,overflow:"hidden",willChange:"height",transition:`height ${y?"250ms":"265ms"} ease-in-out 0s`,margin:"1em 0"},children:[(0,n.jsx)("summary",{onClick:()=>{w(!y),E()},children:"show code"}),(0,n.jsx)("div",{ref:C,style:{display:"block",overflow:"hidden"},children:(0,n.jsx)("div",{className:m.Z.collapsibleContent,children:(0,n.jsxs)(h.Z,{groupId:"sdk-code",children:[(0,n.jsx)(u.Z,{value:"js",label:"JS",attributes:{onMouseDown:E},children:(0,n.jsx)(c.Z,{language:"js",children:t})}),(0,n.jsx)(u.Z,{value:"ts",label:"TS",attributes:{onMouseDown:E},children:(0,n.jsx)(c.Z,{language:"ts",children:i})}),(0,n.jsx)(u.Z,{value:"react",label:"React",attributes:{onMouseDown:E},children:(0,n.jsx)(c.Z,{language:"jsx",children:a})}),(0,n.jsx)(u.Z,{value:"vue",label:"Vue",attributes:{onMouseDown:E},children:(0,n.jsx)(c.Z,{language:"html",children:g})}),(0,n.jsx)(u.Z,{value:"svelte",label:"Svelte",attributes:{onMouseDown:E},children:(0,n.jsx)(c.Z,{language:"html",children:v})})]})})})]})}function f(e){let{className:t,style:s,showCode:r,height:o,...d}=e,{colorMode:c}=(0,i.I)(),u=e=>JSON.stringify(e,null,2),h=`
import { createPlayground } from 'livecodes';
const options = ${u(d)};
createPlayground('#container', options);
`.trimStart(),p=`
import { createPlayground, type EmbedOptions } from 'livecodes';
const options: EmbedOptions = ${u(d)};
createPlayground('#container', options);
`.trimStart(),m=`
import LiveCodes from 'livecodes/react';
export default function App() {
const options = ${u(d)};
return (