Oneseco Media GITHUB

Oneseco Media GITHUB

4110 bookmarks
Newest
Drafts User Guide: Mustache Templates
Drafts User Guide: Mustache Templates
Mustache Template Basics For a more detailed understanding of Mustache template syntax, it’s a good idea to read over the official documentation. This article covers enough basics to get you going, as well as discussing specific values and tags available in Drafts’ implementation. Like any template language, Mustache supports markup for substituting values and applying basic logic to strings. Let’s review basic tag constructs. Variable Tags {{value}} Renders a value from the context. Sections Tags & Inverted Section Tags Section: {{#section}}...{{/section}} Performs conditionals, loops and object scoping. Inverted Section: {{^section}}...{{/section}} Sister to section tags, and render when the other does not. When rendering, a section tag, if the value returned by the tag is “truthy”, the content inside the tag will be rendered. “Truthy” values are things like a true boolean, but also a not-empty string, a non-zero number, etc. The “inverted” section will render in the case where the same section tag will not, making it possible to have conditional content. CONDITIONAL EXAMPLE: {{#isFlagged}}FLAGGED{{/isFlagged}} {{^isFlagged}}NOT FLAGGED{{/isFlagged}} Section tags are also used to render repeating values, so if the value returned by the tag is a sequence, like an array of values, the section will be rendered once for each of the values in the array. REPEAT EXAMPLE: In this example, if the context has an array value with the key “letters” equal to ["a", "b", "c"], the template would render “abc”. {{#letters}}{{.}}{{/letters}} Partial Tags {{>partial}} Includes a template in another one. Partial Override Tags {{<layout}}...{{/layout}} Provides template inheretance. Comment Tags {{! Comment Text }} Provides comments in a template that are not rendered. Pragma Tags {{% CONTENT_TYPE:HTML }} By default, Drafts renders in text mode. If you need to default to HTML mode (automatically HTML escaping values), you can use the above pragma at the beginning of your template to switch modes. Drafts Template Context When a template is evaluated, values are provided in a context object. In most common uses in Drafts actions, the context is constructed for you with the values and filters discussed below. Note that you can pass additional context values to Mustache templates when triggering them via script (details below). Draft Value Tags When a draft is in context in a Mustache template, the below tags are available to render information about the draft. When running an action which uses Mustache templates, these values will be loaded in the root of the template context. A simple example using value tags to create a version of the draft with the title, then it’s assigned tags on the second line, the draft’s URL next, and the remaining body of the draft below might look like: Title: {{title}} Tags: {{tags}} URL: {{permalink}} {{body}} IDENTIFIERS {{uuid}} A unique identifier for the current draft. {{permalink}} A URL which can be used as a bookmark to open Drafts and select the current draft. CONTENT {{content}} The full text of the draft. {{title}} The first line of the draft only. {{safeTitle}} File name safe version of the first line with ASCII control characters and path separators that can interfere with file names removed (\/:*?<>|#). {{displayTitle}} A cleaned and trimmed version of the first line as would be displayed in the draft list. Removes whitespace and leading “#” characters. {{body}} The remainder of the draft text after the first line is removed. {{bodyPreview}} A trimmed and shortened snippet of the body, as would be displayed in the draft list. {{selection}} If text was selected within the draft before selecting an action, this tag will return only that selected text. If no text was selected, it will return the full text of the draft. {{selectionOnly}} Returns selected text, but unlike {{selection}} will not default to returning the entire draft if no selection was made - it will simply return a empty string. {{isArchived}} Boolean value indicating whether the draft is in the archive. Typically used with conditional sections to render content only true or false. {{isFlagged}} Boolean value indicating whether the draft is in the flagged. Typically used with conditional sections to render content only true or false. {{tags}} Comma-separated list of tags linked to the draft. {{hashtags}} Same as {{tags}}, but with # characters prepended before each tag. For use with actions that export to other systems which utilize hash tags in the text when tagging. {{line}} The text of the current line, based on the last cursor position in the document. If a selection which spanned multiple lines was present, extends the selection to the beginning and end of the lines selected. {{line(n)}} The text of a specific line number in the draft, where “n” is the line number. i.e. {{line(1)}}, {{line(2)}}.This tag also support negative indexes, which count back from the last line of the draft, e.g. {{
·docs.getdrafts.com·
Drafts User Guide: Mustache Templates