### Install Squirrelly in Browser via CDN Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/get-started/install.mdx HTML script tags to include the Squirrelly library directly in a web page using either Unpkg or JSDelivr Content Delivery Networks. This makes Squirrelly available globally as the `Sqrl` variable. ```html ``` ```html ``` -------------------------------- ### Install Squirrelly via npm or Yarn Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/get-started/install.mdx Instructions for installing the Squirrelly templating library in a Node.js project using either npm or Yarn package managers. The `--save` flag ensures the dependency is added to `package.json`. ```sh npm install squirrelly --save ``` ```sh yarn add squirrelly ``` -------------------------------- ### Complete Squirrelly.js Template Definition and Rendering Example Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/get-started/first-template.md This comprehensive snippet combines the template definition and rendering steps into a single block, providing a full, runnable example of how to use Squirrelly.js to generate dynamic content. ```js var myTemplate = 'My favorite template engine is {{it.favorite}}.' var result = Sqrl.render(myTemplate, { favorite: 'Squirrelly, definitely' }) ``` -------------------------------- ### Install Project Dependencies with Yarn Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/README.md This command installs all necessary project dependencies using Yarn, preparing the Squirrelly documentation website for development or build operations. It ensures that all required packages are available locally. ```Shell $ yarn ``` -------------------------------- ### Example of Squirrelly Helper with Nested Blocks Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/helpers.md This example demonstrates a practical application of Squirrelly helpers, showcasing how to pass parameters and structure content using nested helper blocks. It illustrates a 'portfolio' helper containing 'tagline', 'hobbies', and 'img' sub-blocks. ```Squirrelly {{@portfolio( {userID: 3838357} )}} Joe Edrick {{#tagline}} Cool Coder Person {{#hobbies}} Eating delicious food {{#img}} {{@user.img}} {{/portfolio}} ``` -------------------------------- ### Start Local Development Server with Yarn Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/README.md This command initiates a local development server for the Squirrelly documentation website. It automatically opens a browser window and supports live reloading for most changes, eliminating the need to restart the server manually. ```Shell $ yarn start ``` -------------------------------- ### Squirrelly.js File Partial Example with Template and JavaScript Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/partials.md Provides a comprehensive example of using file-based partials in Squirrelly.js for Node.js. It includes both the content of a `.sqrl` partial file and the JavaScript code to render a main template that incorporates this file partial, demonstrating how data is passed and accessed. ```Squirrelly Template {{! /* src/partial.sqrl */}} This is a partial speaking: "My name is {{it.name}}" ``` ```JavaScript // src/index.js Sqrl.render("{{@includeFile('./partial', it) /}}", { name: 'Ben', }) // This is a partial speaking: "My name is {{it.name}}" ``` -------------------------------- ### Import Squirrelly in Node.js Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/get-started/install.mdx Demonstrates how to import Squirrelly using ES Modules (`import`) or CommonJS (`require`) syntax in a Node.js environment. Squirrelly is packaged as a UMD module, supporting various module loading options. ```js import * as Sqrl from 'squirrelly' // or var Sqrl = require('squirrelly') ``` -------------------------------- ### Example Usage of Sqrl.compile Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/compilation.md Demonstrates how to use `Sqrl.compile` to compile a simple template string and then execute the compiled function with data. It shows the input template, the structure of the resulting compiled function, and the final rendered output. ```js var myTemplate = "Hi, my name is {{it.name}}"; var compiled = Sqrl.compile(myTemplate); // Returns a function: // function anonymous(it,c,cb ) { var tR='';tR+='Hi, my name is ';tR+=c.l('F','e')(it.name);if(cb){cb(null,tR)} return tR } compiled({ name: "Johnny Appleseed" }, Sqrl.defaultConfig); //Returns "Hi, my name is Johnny Appleseed" ``` -------------------------------- ### Example of Self-Closing Squirrelly Helper for Partials Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/helpers.md This example provides a common use case for a self-closing Squirrelly helper: including a partial template. It demonstrates how to call the 'include' helper with a partial name as a parameter without requiring a closing tag or content block. ```Squirrelly {{@include("mypartial")/}} ``` -------------------------------- ### Squirrelly.js Registered Partial Definition and Usage Example Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/partials.md Demonstrates the complete workflow for using registered partials in Squirrelly.js. It shows how to compile a partial string, define it globally using `Sqrl.templates.define`, and then render a main template that includes this partial, passing the current context (`it`) as data. ```JavaScript let mypartial = `My name is {{it.name}}` Sqrl.templates.define('mypartial', Sqrl.compile(mypartial)) Sqrl.render("This is a partial: {{@include('mypartial', it) /}}", { name: 'Ben', }) // This is a partial: My name is Ben ``` -------------------------------- ### Render Squirrelly.js Template with Data Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/get-started/first-template.md This example illustrates how to use the `Sqrl.render` method to process a Squirrelly.js template string with a given data object. The method returns the final rendered string. ```js var result = Sqrl.render(myTemplate, data) ``` -------------------------------- ### Squirrelly If/Else Conditional Helper Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/overview.md Demonstrates the use of Squirrelly's native `if`, `elif`, and `else` helpers for implementing conditional logic within templates. These helpers compile directly into native JavaScript code, ensuring efficient execution. The example shows how to check numerical conditions and render different content based on the outcome. ```Squirrelly {{ @if (it.number === 3) }} Number is three {{ #elif (it.number === 4) }} Number is four {{ #else }} Number is five {{ /if}} ``` -------------------------------- ### Squirrelly v7 Inconsistent Data Reference Example Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/blog/2020-02-22-squirrelly-v8.md This snippet illustrates a common issue in Squirrelly v7 where data references often required an explicit 'options.' prefix, leading to inconsistent syntax depending on the variable's position or context. ```Squirrelly {{val1 + options.val2}} ``` -------------------------------- ### Render Squirrelly Template with JavaScript Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/rendering.md This example demonstrates how to render a Squirrelly template string using the `Sqrl.render()` method. It shows passing a data object to the template and the resulting string output after compilation. ```js var myTemplate = "Hi, my name is {{it.name}}"; Sqrl.render(myTemplate, { name: "Johnny Appleseed" }); //Returns "Hi, my name is Johnny Appleseed" ``` -------------------------------- ### Define and manage Squirrelly cache items for templates and filters Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/containers.md This JavaScript example demonstrates how to interact with Squirrelly's internal caching mechanisms for templates and filters. It shows how to define a new template or filter using `Sqrl.templates.define` and `Sqrl.filters.define`, retrieve a defined template with `Sqrl.templates.get`, and clear all defined filters using `Sqrl.filters.clear`. ```js Sqrl.templates.define("my-partial", Sqrl.compile("This is a partial speaking")); console.log(Sqrl.templates.get("my-partial")); Sqrl.filters.define("capitalize", function(str) { return str.toUpperCase(); }); Sqrl.filters.clear(); ``` -------------------------------- ### Squirrelly Native Code Console Logging Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/native-code.md Shows an example of performing console logging from within a Squirrelly native code block. This is useful for debugging template logic. It's crucial to include semicolons after function calls like `console.log()` to ensure correct JavaScript execution. ```sqrl {{! console.log("Hi"); }} ``` -------------------------------- ### Squirrelly v7 String Parsing Failure Example Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/blog/2020-02-22-squirrelly-v8.md This example demonstrates a limitation in Squirrelly v7's parser, which could fail to correctly parse template strings containing specific characters (like '}}') due to its reliance on a single, large regular expression. ```Squirrelly {{ val1 + "closing tag is }}, right"}} ``` -------------------------------- ### Compile Squirrelly Template with Custom Tags using getConfig Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/configuration.md This example demonstrates how to compile a Squirrelly template string (`myTemplate`) using `Sqrl.compileToString` while applying custom tag delimiters (`<%` and `%>`). The `Sqrl.getConfig` function is used to retrieve a configuration object with the specified tags, ensuring the template engine parses the template with these custom delimiters. ```js Sqrl.compileToString(myTemplate, Sqrl.getConfig({ tags: ["<%", "%>"] })); ``` -------------------------------- ### Squirrelly Whitespace Control Example: Trim Before Tag Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/whitespace-trimming.md Demonstrates trimming one character of whitespace immediately preceding a Squirrelly tag using the '-' modifier. This is useful for precise layout control. ```sqrl Hi {{- it.myname }} ``` -------------------------------- ### Parse Squirrelly Template into Syntax Tree Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/parsing.md This JavaScript example demonstrates how to parse a Squirrelly template string into its internal syntax tree representation using the `Sqrl.parse()` method. The output is a structured array representing the template's components, useful for advanced scenarios like developing custom helpers or plugins. ```js var myTemplate = 'Hi, my name is {{it.name}}' var compiled = Sqrl.parse(myTemplate) //Returns a Squirrelly syntax tree (like an AST): // ['Hi, my name is ', { f: [], c: 'it.name', t: 'r' }] ``` -------------------------------- ### Squirrelly Whitespace Control Example: Trim All Whitespace Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/whitespace-trimming.md Illustrates trimming all whitespace before and after a Squirrelly tag using the '_' modifier. This is commonly applied to control flow tags like 'if' to prevent unwanted newlines or spaces. ```sqrl {{_ ~if (it.num) _}} {{/if}} ``` -------------------------------- ### Disable Auto-Escaping for Specific References in Squirrelly.js Templates Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/auto-escaping.md These examples illustrate how to selectively bypass auto-escaping for individual references within Squirrelly.js templates. This can be achieved by applying the `safe` filter or by preceding the reference with an asterisk (`*`) immediately after the opening delimiters, useful for rendering pre-sanitized or trusted content. ```squirrelly {{someref | safe}} {{* someref}} {{_ * someref}} ``` -------------------------------- ### Defining a Native 'if' Helper in Squirrelly.js Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/native-helpers.md This JavaScript code defines a custom 'if' native helper for Squirrelly.js templates. It processes conditional logic, including 'else' and 'elif' blocks, by compiling template content within the respective scopes. This example demonstrates how to extend Squirrelly's templating capabilities with custom control flow, though native helpers are noted as complex. ```javascript Sqrl.nativeHelpers.define('if', function (buffer, env) { // buffer.d is buffer content, in AST form var returnStr = 'if(' + buffer.p + '){' + Sqrl.compileScope(buffer.d, env) + '}' if (buffer.b) { // b stands for blocks // Loop through each helper block for (var i = 0; i < buffer.b.length; i++) { var currentBlock = buffer.b[i] if (currentBlock.n === 'else') { returnStr += 'else{' + Sqrl.compileScope(currentBlock.d, env) + '}' } else if (currentBlock.n === 'elif') { returnStr += 'else if(' + currentBlock.p + '){' + Sqrl.compileScope(currentBlock.d, env) + '}' } } } return returnStr }) ``` -------------------------------- ### Build Static Website Content with Yarn Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/README.md This command generates static content for the Squirrelly documentation website into the `build` directory. The resulting static files can then be served using any standard static content hosting service, preparing the site for production deployment. ```Shell $ yarn build ``` -------------------------------- ### Deploy Website to GitHub Pages with Yarn Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/README.md This command facilitates the deployment of the Squirrelly documentation website to GitHub Pages. It automates the process of building the website and pushing the generated content to the `gh-pages` branch, requiring a specified GitHub username and optionally using SSH for authentication. ```Shell $ GIT_USER= USE_SSH=true yarn deploy ``` -------------------------------- ### Sqrl.compile Method Syntax Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/compilation.md Illustrates the basic syntax for `Sqrl.compile`, showing its parameters and return type. It takes a template string and an optional configuration object, returning a function that can be invoked with data. ```js Sqrl.compile (str, options) // returns a function that can be called with (data, options, [cb]) // note: options must be a valid config object ``` -------------------------------- ### Render Squirrelly.js Template with Alternative Data Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/get-started/first-template.md This snippet demonstrates the flexibility of Squirrelly.js by rendering the same template with a different data object directly passed to the `render` method, showcasing how dynamic content can be generated. ```js var result2 = Sqrl.render(myTemplate, { favorite: 'Squirrelly, definitely' }) ``` -------------------------------- ### Applying Filters with and Without Parameters in Squirrelly Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/filters.md Illustrates the application of basic filters like `reverse` and `capitalize`, and how to pass parameters to filters such as `join` in Squirrelly templates. ```sqrl {{! /* Basic filters */}} {{mystring | reverse | capitalize}} {{! /* With Parameters */}} {{it.someArray | join(", ")}} ``` -------------------------------- ### Implementing Asynchronous Helpers and Rendering in Squirrelly.js Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/learn/async.md This snippet illustrates how to define an asynchronous helper (`async-test`) in Squirrelly.js that returns a Promise. It then shows two methods for rendering a template that uses this async helper: one using `async`/`await` syntax and another using a traditional callback function, both demonstrating how Squirrelly.js waits for the helper's promise to resolve before outputting the result. ```js function resolveAfter2Seconds () { return new Promise(resolve => { setTimeout(() => { resolve('HI FROM ASYNC') }, 2000) }) } Sqrl.helpers.define('async-test', resolveAfter2Seconds) async function doAsyncStuff () { console.log( await Sqrl.render( '{{@async async-test()/}}', {}, { async: true, asyncHelpers: ['async-test'] } ) ) // logs 'HI FROM ASYNC' after 2 seconds } // ALTERNATIVELY, WITH CALLBACKS: Sqrl.render( '{{@async async-test()/}}', {}, { async: true, asyncHelpers: ['async-test'] }, function (err, res) { console.log(res) // logs 'HI FROM ASYNC' after 2 seconds } ) ``` -------------------------------- ### Defining and using Squirrelly.js partials Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/templates.md This snippet demonstrates how to define partials using `Sqrl.templates.define` and compile them with `Sqrl.compile`. It also shows how to include partials in a template using `{{@include(...)}}` and pass data to them. ```js Sqrl.templates.define("my-partial", Sqrl.compile("This is a partial speaking")); Sqrl.render('... {{@include("my-partial")/}}', {}); // ... This is a partial speaking // To call a partial w/ data: Sqrl.templates.define("my-partial-2", Sqrl.compile("Name: {{it.name}}")); Sqrl.render( '... {{@include("my-partial", {name: it.name})/}}', // The 2nd argument passed to `include` is the data. You could also pass `it` to forward all data { name: "Ben" } ); // ... Name: Ben ``` -------------------------------- ### Prepare Data for Squirrelly.js Template Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/get-started/first-template.md This code snippet shows how to create a simple JavaScript object that will serve as the data context for rendering a Squirrelly.js template. The object's properties correspond to the placeholders in the template. ```js var data = { favorite: 'Squirrelly' } ``` -------------------------------- ### Squirrelly Configuration Options Reference Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/configuration.md Defines the various options available to customize Squirrelly's behavior, including their type, description, default value, and whether they are required. This section also details specific behaviors for delimiters, auto-trimming, and plugin structures. ```APIDOC Squirrelly Configuration Options: async Description: Whether to generate async templates Type: boolean Default: false Required: Yes autoEscape Description: Whether to automatically XML-escape Type: boolean Default: (none specified) Required: Yes autoTrim Description: Configure automatic whitespace trimming Type: "nl" | "slurp" | boolean | ["nl" | "slurp" | boolean, "nl" | "slurp" | boolean] Default: [false, "nl"] Required: Yes Options: "nl": trims first character "slurp": trims all leading/trailing whitespace true: equivalent to "slurp" Note: When an array is passed, Squirrelly uses the equivalent options on the left or right side of the string. cache Description: Cache templates by `name` or `filename` Type: boolean Default: (none specified) Required: Yes defaultFilter Description: Pass all interpolates through a function Type: false | Function Default: false Required: Yes filename Description: Absolute filepath of template (for caching) Type: string Default: undefined Required: No l Description: Function that returns helpers. Type: Function Default: defaultConfig.l Required: Yes Signature: (container: "H" | "F", name: string) => Function name Description: Template name (for caching) Type: string Default: undefined Required: No plugins Description: Plugins object Type: object Default: defaultConfig.plugins Required: Yes Properties: processAST: Description: List of functions that manipulate Squirrelly syntax tree Type: Array Default: [] processFnString: Description: List of functions that manipulate Squirrelly template function Type: Array Default: [] root Description: Base filepath. Defaults to "" internally Type: string Default: undefined Required: No storage Description: Object containing templates, helpers, filters Type: object Default: defaultConfig.storage Required: Yes tags Description: Template delimiters. Type: [string, string] Default: ["{{", "}}"] Required: Yes Caveats: - Closing delimiters (like `{{`) can't have any of `(`, `)`, `|`, or `=>`. - Delimiters must be RegExp-escaped. useWith Description: Use `with(){}` to have data scope as global Type: boolean Default: undefined Required: No varName Description: Name of data object Type: string Default: "it" Required: Yes view cache Description: Overrides `cache` Type: boolean Default: undefined Required: No views Description: Absolute filepath to views directory Type: string Default: undefined Required: No ``` -------------------------------- ### Basic Squirrelly Reference Syntax Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/interpolate.md Demonstrates the fundamental syntax for outputting data into a Squirrelly template using double curly braces. This is the simplest form of interpolation. ```sqrl {{ reference }} ``` -------------------------------- ### Define Basic Squirrelly Helper Syntax Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/helpers.md This snippet illustrates the fundamental syntax for defining and using helpers in Squirrelly templates. It shows how to declare a helper with parameters, define variables, and enclose content within its block, including nested blocks. ```Squirrelly {{@helpername(parameters) => [var1, var2]}} Content goes here {{#helperblock}} {{/helpername}} ``` -------------------------------- ### Basic Filter Syntax in Squirrelly Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/filters.md Demonstrates the fundamental syntax for applying filters in Squirrelly templates, using the pipe (`|`) operator to chain filters together. ```sqrl {{somereference | somefilter |anotherfilter}} ``` -------------------------------- ### Squirrelly.js Configuration Management Functions Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/configuration.md This section describes `defaultConfig` and `getConfig` functions in Squirrelly.js. `defaultConfig` provides the library's default settings, while `getConfig` is used to merge custom options with these defaults, optionally allowing an initial merge with a third parameter. These functions are crucial for configuring template parsing and rendering behavior. ```APIDOC defaultConfig: object - Returns: The default configuration object for Squirrelly.js. getConfig(options: object, initialMergeOptions?: object): object - Description: Merges provided configuration options with the default Squirrelly.js configuration. It can optionally take a third parameter for initial merging. - Parameters: - options: object - Configuration options to merge. - initialMergeOptions: object (optional) - An object to merge with defaults before 'options'. - Returns: A merged configuration object. ``` -------------------------------- ### Asynchronously Include a Partial in Squirrelly Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/async.md Shows how to asynchronously include a partial template using the `include` helper in Squirrelly. This is useful for loading partials that might involve asynchronous data fetching or rendering. ```Squirrelly {{@async include("mypartial") /}} ``` -------------------------------- ### Squirrelly.js File Partial Inclusion Syntax Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/partials.md Illustrates the syntax for including a partial directly from a file path in Squirrelly.js templates. This helper is specifically for Node.js environments and allows referencing partials by their file location, with optional data passing. ```Squirrelly Template {{@includeFile('path-to-partial', data) /}} ``` -------------------------------- ### Squirrelly v8 Include Partial File Syntax Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/blog/2020-02-22-squirrelly-v8.md This snippet showcases the new syntax introduced in Squirrelly v8 for including partial template files. This feature allows users to modularize their templates and reuse components across different files. ```Squirrelly {{@includeFile("./file.sqrl") /}} ``` -------------------------------- ### Squirrelly if/else Helper for Conditional Rendering Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/builtin-helpers.md Demonstrates the conditional rendering logic using the built-in `if/else` helper in Squirrelly templates. This helper allows displaying content based on a boolean condition, providing a simple way to control template output flow. ```sqrl {{@if(it.somevalue === 1)}} Display this {{#else}} Display this {{/if}} ``` -------------------------------- ### Basic Squirrelly Native Code Syntax Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/native-code.md Illustrates the fundamental syntax for embedding native JavaScript code within Squirrelly templates. Any valid JavaScript code placed between these delimiters will be evaluated during template rendering. ```sqrl {{! ... }} ``` -------------------------------- ### Define and use a simple custom filter in Squirrelly.js Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/filters.md Demonstrates how to define a custom filter named 'reverse' that reverses a string, and then applies it within a Squirrelly.js template. Shows the template definition, filter registration using `Sqrl.filters.define`, and the rendering output. ```javascript var myTemplate = "Hi, my name is {{it.name | reverse}}"; Sqrl.filters.define("reverse", function(str) { return str .split("") .reverse() .join(""); }); Sqrl.render(myTemplate, { name: "Ben" }); // Hi, my name is neB ``` -------------------------------- ### Define a Squirrelly.js Template String Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/get-started/first-template.md This snippet demonstrates how to define a basic Squirrelly.js template as a JavaScript string. The template uses `{{it.favorite}}` as a placeholder for data injection. ```js var myTemplate = 'My favorite template engine is {{it.favorite}}.' ``` -------------------------------- ### Squirrelly.js Registered Partial Inclusion Syntax Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/partials.md Illustrates the syntax for including a pre-registered partial within a Squirrelly.js template. This helper allows dynamic content insertion by referencing a partial's name and optionally passing a data object. ```Squirrelly Template {{@include('mypartial', data) /}} ``` -------------------------------- ### Squirrelly Conditional Logic Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/cheatsheet.md Demonstrates how to use `if` and `else` blocks for conditional rendering in Squirrelly templates, allowing content to be displayed based on a boolean expression. ```SQRL {{@if(it.someval === "someothervalue")}} Display this! {{#else}} They don't equal {{/if}} ``` -------------------------------- ### Squirrelly Native Code Comments Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/native-code.md Demonstrates how to include comments within Squirrelly native code blocks. Comments follow standard JavaScript comment syntax and are ignored during template execution, useful for documentation within the template itself. ```sqrl {{! /* this is a comment */}} ``` -------------------------------- ### Integrate Squirrelly.js with Express.js Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/file-handling.md This snippet demonstrates how to configure Express.js to use Squirrelly.js as a templating engine. It shows two common approaches: registering Squirrelly to render '.html' files or setting '.squirrelly' as the default view engine for Express. ```js app.engine('html', require('squirrelly').renderFile) // Or, if you want to use the .squirrelly file extension app.set('view engine', 'squirrelly') ``` -------------------------------- ### Define Squirrelly.js 'foreach' helper Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/helpers.md This helper defines the `foreach` functionality in Squirrelly.js, enabling iteration over objects or arrays within templates. It iterates through the first parameter, executing the helper's content for each item and passing the key and value. ```js Sqrl.helpers.define("foreach", function(content) { var res = ""; var param = content.params[0]; // the first param is the object we want to loop over for (var key in param) { if (!hasOwnProp(param, key)) continue; res += content.exec(key, param[key]); } return res; }); ``` -------------------------------- ### Default `l` Function Implementation in JavaScript Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/configuration.md The default implementation of the `l` function, used internally by Squirrelly to fetch registered helpers ('H') or filters ('F') by name. It throws an error if the requested helper or filter is not found, ensuring template rendering fails gracefully when dependencies are missing. ```JavaScript function (container, name) { if (container === 'H') { var hRet = helpers.get(name) if (hRet) { return hRet } else { throw SqrlErr("Can't find helper '" + name + "'") } } else if (container === 'F') { var fRet = filters.get(name) if (fRet) { return fRet } else { throw SqrlErr("Can't find filter '" + name + "'") } } } ``` -------------------------------- ### Squirrelly try-catch Helper for Error Handling Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/builtin-helpers.md Demonstrates error handling within Squirrelly templates using the built-in `try-catch` helper. This allows catching and displaying error messages (`err.message`) that occur during template execution, improving robustness. ```sqrl {{@try}} This won't work: {{ *it.hi | validate}} {{#catch => err}} Uh-oh, error! Message was '{{err.message}}' {{/try}} ``` -------------------------------- ### Define and use a custom filter with parameters in Squirrelly.js Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/filters.md Illustrates how to define a custom filter named 'replace' that accepts additional parameters (search and replace strings), and then applies it within a Squirrelly.js template. Shows the template definition, filter registration, and the rendering output with parameter usage. ```javascript var myTemplate = "{{it.bio | replace('apples', 'watermelons') }}"; Sqrl.filters.define("replace", function(str, search, replace) { return str.replace(search, replace); }); Sqrl.render(myTemplate, { bio: "I like to eat apples" }); // I like to eat watermelons ``` -------------------------------- ### Squirrelly Console Logging for Debugging Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/cheatsheet.md Demonstrates how to output values directly to the browser's console from within a Squirrelly template using `console.log`. It's important to include a semicolon (`;`) at the end of the statement to ensure successful template compilation. ```SQRL {{!console.log("The value of it.num is: " + it.num);}} ``` -------------------------------- ### Squirrelly each Helper for Array Iteration Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/builtin-helpers.md Illustrates iterating over an array using the built-in `each` helper in Squirrelly templates. It provides access to the current array element (`val`) and its index (`index`), both of which are optional and can be renamed for flexibility. ```sqrl {{@each(it.somearray) => val, index}} Display this The current array element is {{val}} The current index is {{index}} {{/each}} ``` -------------------------------- ### Define Squirrelly.js 'extends' helper Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/helpers.md This helper defines the `extends` functionality in Squirrelly.js, allowing templates to inherit from a base layout. It processes blocks and injects compiled content into the parent template, throwing an error if the base template is not found. ```js Sqrl.helpers.define("extends", function(content, blocks, config) { var data = content.params[1] || {}; data.content = content.exec(); // Loop through each block for (var i = 0; i < blocks.length; i++) { var currentBlock = blocks[i]; // set data[blockName] to the compiled value of the current block data[currentBlock.name] = currentBlock.exec(); } var template = config.storage.templates.get(content.params[0]); if (!template) { throw SqrlErr('Could not fetch template "' + content.params[0] + '"'); } return template(data, config); }); ``` -------------------------------- ### Squirrelly Object Iteration with @foreach Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/cheatsheet.md Shows how to loop through the properties of an object using the `@foreach` helper in Squirrelly. This allows retrieval of both the key and value for each property in the object. ```SQRL {{@foreach(it.someObject) => key, val}} The current object key is {{key}}, and the value is {{val}} {{/foreach}} ``` -------------------------------- ### Squirrelly.js Storage Object Properties Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/api/configuration.md Details the properties of the `storage` object in Squirrelly.js, which manages caches for filters, helpers, native helpers, and templates. This object can be modified to create environments with different caches, allowing for custom caching strategies. ```APIDOC storage object: - filters: Filters cache - Type: Cacher - Default: Sqrl.filters - helpers: Helpers cache - Type: Cacher - Default: Sqrl.helpers - nativeHelpers: Native helpers cache - Type: Cacher - Default: Sqrl.nativeHelpers - templates: Templates cache - Type: Cacher - Default: Sqrl.templates ``` -------------------------------- ### Squirrelly foreach Helper for Object Iteration Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/builtin-helpers.md Shows how to iterate over the keys and values of an object using the built-in `foreach` helper in Squirrelly templates. This helper provides access to the current key (`key`) and value (`val`), which are optional and can be renamed. ```sqrl {{@foreach(it.someobject) => key, val}} This loops over each of an objects keys and values. The value of the current child is {{val}} The current key is {{key}} {{/foreach}} ``` -------------------------------- ### Define an Async Helper in Squirrelly Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/async.md Demonstrates the basic syntax for defining an asynchronous helper block in Squirrelly templates, allowing for asynchronous operations within the helper's scope. The `async` keyword is placed before the helper name. ```Squirrelly {{@ async helpername(parameters) => var1 }} {{/helpername}} ``` -------------------------------- ### Apply an Async Filter in Squirrelly Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/async.md Illustrates how to apply an asynchronous filter to a value within a Squirrelly template. The `async` keyword precedes the filter name, enabling the filter to perform asynchronous operations. ```Squirrelly {{val | filter1 | async filter2}} ``` -------------------------------- ### Squirrelly Array Iteration with @each Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/cheatsheet.md Illustrates how to iterate over an array using the `@each` helper in Squirrelly. This snippet shows how to access both the value and index of each element during iteration. ```SQRL {{@each(it.someArray) => val, index}} The current array item is {{val}}, the current index is {{index}} {{/each}} ``` -------------------------------- ### Define Basic Self-Closing Squirrelly Helper Syntax Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/helpers.md This snippet presents the simplified syntax for self-closing helpers in Squirrelly. Self-closing helpers are used when a helper does not encapsulate any content and is called solely for its side effects or parameter-based functionality. ```Squirrelly {{@helpername(parameters) /}} ``` -------------------------------- ### Disabling Autoescaping with the `safe` Flag in Squirrelly Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/filters.md Shows how to use the `safe` flag in Squirrelly templates to prevent content from being autoescaped, ensuring raw HTML or other sensitive content is rendered directly. ```sqrl {{myreference | safe}} ``` -------------------------------- ### Configure Global Auto-Escaping in Squirrelly.js Source: https://github.com/squirrellyjs/squirrelly-docs/blob/master/docs/syntax/auto-escaping.md This JavaScript snippet demonstrates how to globally enable or disable the auto-escaping feature in Squirrelly.js. By setting `Sqrl.defaultConfig.autoEscape` to `true` or `false`, developers can control whether all template references are automatically HTML-escaped for XSS protection. ```javascript Sqrl.defaultConfig.autoEscape = true // Turns autoEscaping on Sqrl.defaultConfig.autoEscape = false // Turns autoEscaping off // autoEscaping is on by default ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.