Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding dynamic partials, discussed in #54 #134

Merged
merged 28 commits into from Aug 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
45650e7
Dynamic partials
Dec 4, 2021
7da3ef2
Dynamic partials (json)
Dec 4, 2021
b5ec88f
Update specs/dynamic.yml
Dec 6, 2021
2c585ca
Update dynamic.yml
Dec 6, 2021
a15b333
Renaming dynamic partials spec
Dec 6, 2021
22c3bf1
Dynamic partials update (squashed) (coauthored)
Jul 17, 2022
b32a4ae
Removing interpolation example and adding JSON spec
Jul 18, 2022
c1449c2
Dotted names and context stacking tests
Jul 20, 2022
7e356c5
Fixing typo on test result
Jul 20, 2022
d7edba3
Fixing test result and updating the spec (squashed) (coauthored)
Jul 20, 2022
69487d5
Editing tag description and fixing whitespaces in tests
Jul 20, 2022
956cfa4
Removing redundant paragraph
Jul 20, 2022
6e9b75d
adding rationale and a new test
Jul 21, 2022
dcc145e
Updating rationale (squashed) (coauthored)
Jul 22, 2022
736472c
Update specs/~dynamic-names.yml
Jul 25, 2022
2030fff
Adding two more tests, updating operator precedence desc + typo fix
Jul 25, 2022
80395e4
Updating rationale.
Jul 25, 2022
f297e36
Update specs/~dynamic-names.yml
Jul 25, 2022
f72af17
Rebuilding JSON.
Jul 25, 2022
3f33715
Adding examples to the rationale
Jul 26, 2022
d17d688
Adding pro to the if blocks approach
Jul 26, 2022
7a4d919
Specify block scope resolution (#125)
jgonggrijp Jul 5, 2021
4d6505f
Update JSON accordingly
jgonggrijp Jul 5, 2021
07137f0
Adding a test: comment content colliding with variable (squashed) (co…
s9105947 Mar 22, 2022
9dc776c
Removing leading hashtags and fixing spacing
Jul 27, 2022
f3ceea6
Adding blanklines and indenting rationale codes.
Jul 28, 2022
734537e
Updating spec, typo fix (squashed) (coauthored)
Jul 28, 2022
ee54b6a
Merge branch 'mustache:master' into master
Aug 8, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
312 changes: 312 additions & 0 deletions specs/~dynamic-names.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,312 @@
{
"overview": "Rationale: this special notation was introduced primarily to allow the dynamic\nloading of partials. The main advantage that this notation offers is to allow\ndynamic loading of partials, which is particularly useful in cases where\npolymorphic data needs to be rendered in different ways. Such cases would\notherwise be possible to render only with solutions that are convoluted,\ninefficient, or both.\nExample.\nLet's consider the following data:\n\n items: [\n { content: 'Hello, World!' },\n { url: 'http://example.com/foo.jpg' },\n { content: 'Some text' },\n { content: 'Some other text' },\n { url: 'http://example.com/bar.jpg' },\n { url: 'http://example.com/baz.jpg' },\n { content: 'Last text here' }\n ]\nThe goal is to render the different types of items in different ways. The\nitems having a key named `content` should be rendered with the template\n`text.mustache`:\n\n {{!text.mustache}}\n {{content}}\nAnd the items having a key named `url` should be rendered with the template\n`image.mustache`:\n\n {{!image.mustache}}\n <img src=\"{{url}}\"/>\nThere are already several ways to achieve this goal, here below are\nillustrated and discussed the most significant solutions to this problem.\nUsing Pre-Processing\nThe idea is to use a secondary templating mechanism to dynamically generate\nthe template that will be rendered.\nThe template that our secondary templating mechanism generates might look\nlike this:\n\n {{!template.mustache}}\n {{items.1.content}}\n <img src=\"{{items.2.url}}\"/>\n {{items.3.content}}\n {{items.4.content}}\n <img src=\"{{items.5.url}}\"/>\n <img src=\"{{items.6.url}}\"/>\n {{items.7.content}}\nThis solutions offers the advantages of having more control over the template\nand minimizing the template blocks to the essential ones.\nThe drawbacks are the rendering speed and the complexity that the secondary\ntemplating mechanism requires.\nUsing Lambdas\nThe idea is to inject functions into the data that will be later called from\nthe template.\nThis way the data will look like this:\n\n items: [\n {\n content: 'Hello, World!',\n html: function() { return '{{>text}}'; }\n },\n {\n url: 'http://example.com/foo.jpg',\n html: function() { return '{{>image}}'; }\n },\n {\n content: 'Some text',\n html: function() { return '{{>text}}'; }\n },\n {\n content: 'Some other text',\n html: function() { return '{{>text}}'; }\n },\n {\n url: 'http://example.com/bar.jpg',\n html: function() { return '{{>image}}'; }\n },\n {\n url: 'http://example.com/baz.jpg',\n html: function() { return '{{>image}}'; }\n },\n {\n content: 'Last text here',\n html: function() { return '{{>text}}'; }\n }\n ]\nAnd the template will look like this:\n\n {{!template.mustache}}\n {{#items}}\n {{{html}}}\n {{/items}}\nThe advantage this solution offers is to have a light main template.\nThe drawback is that the data needs to embed logic and template tags in\nit.\nUsing If-Else Blocks\nThe idea is to put some logic into the main template so it can select the\ntemplates at rendering time:\n\n {{!template.mustache}}\n {{#items}}\n {{#url}}\n {{>image}}\n {{/url}}\n {{#content}}\n {{>text}}\n {{/content}}\n {{/items}}\nThe main advantage of this solution is that it works without adding any\noverhead fields to the data. It also documents which external templates are\nappropriate for expansion in this position.\nThe drawback is that this solution isn't optimal for heterogeneous data sets\nas the main template grows linearly with the number of polymorphic variants.\nUsing Dynamic Names\nThis is the solution proposed by this spec.\nThe idea is to load partials dynamically.\nThis way the data items have to be tagged with the corresponding partial name:\n\n items: [\n { content: 'Hello, World!', dynamic: 'text' },\n { url: 'http://example.com/foo.jpg', dynamic: 'image' },\n { content: 'Some text', dynamic: 'text' },\n { content: 'Some other text', dynamic: 'text' },\n { url: 'http://example.com/bar.jpg', dynamic: 'image' },\n { url: 'http://example.com/baz.jpg', dynamic: 'image' },\n { content: 'Last text here', dynamic: 'text' }\n ]\nAnd the template would simple look like this:\n\n {{!template.mustache}}\n {{#items}}\n {{>*dynamic}}\n {{/items}}\nSummary:\n\n +----------------+---------------------+-----------------------------------+\n | Approach | Pros | Cons |\n +----------------+---------------------+-----------------------------------+\n | Pre-Processing | Essential template, | Secondary templating system |\n | | more control | needed, slower rendering |\n | Lambdas | Slim template | Data tagging, logic in data |\n | If Blocks | No data overhead, | Template linear growth |\n | | self-documenting | |\n | Dynamic Names | Slim template | Data tagging |\n +----------------+---------------------+-----------------------------------+\nDynamic Names are a special notation to dynamically determine a tag's content.\n\nDynamic Names MUST be a non-whitespace character sequence NOT containing\nthe current closing delimiter. A Dynamic Name consists of an asterisk,\nfollowed by a dotted name. The dotted name follows the same notation as in an\nInterpolation tag.\n\nThis tag's dotted name, which is the Dynamic Name excluding the\nleading asterisk, references a key in the context whose value will be used in\nplace of the Dynamic Name itself as content of the tag. The dotted name\nresolution produces the same value as an Interpolation tag and does not affect\nthe context for further processing.\n\nSet Delimiter tags MUST NOT affect the resolution of a Dynamic Name. The\nDynamic Names MUST be resolved against the context stack local to the tag.\nFailed resolution of the dynamic name SHOULD result in nothing being rendered.\n\nEngines that implement Dynamic Names MUST support their use in Partial tags.\nIn engines that also implement the optional inheritance spec, Dynamic Names\ninside Parent tags SHOULD be supported as well. Dynamic Names cannot be\nresolved more than once (Dynamic Names cannot be nested).\n",
"tests": [
{
"name": "Basic Behavior - Partial",
"desc": "The asterisk operator is used for dynamic partials.",
"data": {
"dynamic": "content"
},
"template": "\"{{>*dynamic}}\"",
"partials": {
"content": "Hello, world!"
},
"expected": "\"Hello, world!\""
},
{
"name": "Basic Behavior - Name Resolution",
"desc": "The asterisk is not part of the name that will be resolved in the context.\n",
"data": {
"dynamic": "content",
"*dynamic": "wrong"
},
"template": "\"{{>*dynamic}}\"",
"partials": {
"content": "Hello, world!",
"wrong": "Invisible"
},
"expected": "\"Hello, world!\""
},
{
"name": "Context Misses - Partial",
"desc": "Failed context lookups should be considered falsey.",
"data": {},
"template": "\"{{>*missing}}\"",
"partials": {
"missing": "Hello, world!"
},
"expected": "\"\""
},
{
"name": "Failed Lookup - Partial",
"desc": "The empty string should be used when the named partial is not found.",
"data": {
"dynamic": "content"
},
"template": "\"{{>*dynamic}}\"",
"partials": {
"foobar": "Hello, world!"
},
"expected": "\"\""
},
{
"name": "Context",
"desc": "The dynamic partial should operate within the current context.",
"data": {
"text": "Hello, world!",
"example": "partial"
},
"template": "\"{{>*example}}\"",
"partials": {
"partial": "*{{text}}*"
},
"expected": "\"*Hello, world!*\""
},
{
"name": "Dotted Names",
"desc": "The dynamic partial should operate within the current context.",
"data": {
"text": "Hello, world!",
"foo": {
"bar": {
"baz": "partial"
}
}
},
"template": "\"{{>*foo.bar.baz}}\"",
"partials": {
"partial": "*{{text}}*"
},
"expected": "\"*Hello, world!*\""
},
{
"name": "Dotted Names - Operator Precedence",
"desc": "The dotted name should be resolved entirely before being dereferenced.",
"data": {
"text": "Hello, world!",
"foo": "test",
"test": {
"bar": {
"baz": "partial"
}
}
},
"template": "\"{{>*foo.bar.baz}}\"",
"partials": {
"partial": "*{{text}}*"
},
"expected": "\"\""
},
{
"name": "Dotted Names - Failed Lookup",
"desc": "The dynamic partial should operate within the current context.",
"data": {
"foo": {
"text": "Hello, world!",
"bar": {
"baz": "partial"
}
}
},
"template": "\"{{>*foo.bar.baz}}\"",
"partials": {
"partial": "*{{text}}*"
},
"expected": "\"**\""
},
{
"name": "Dotted names - Context Stacking",
"desc": "Dotted names should not push a new frame on the context stack.",
"data": {
"section1": {
"value": "section1"
},
"section2": {
"dynamic": "partial",
"value": "section2"
}
},
"template": "{{#section1}}{{>*section2.dynamic}}{{/section1}}",
"partials": {
"partial": "\"{{value}}\""
},
"expected": "\"section1\""
},
{
"name": "Dotted names - Context Stacking Under Repetition",
"desc": "Dotted names should not push a new frame on the context stack.",
"data": {
"value": "test",
"section1": [
1,
2
],
"section2": {
"dynamic": "partial",
"value": "section2"
}
},
"template": "{{#section1}}{{>*section2.dynamic}}{{/section1}}",
"partials": {
"partial": "{{value}}"
},
"expected": "testtest"
},
{
"name": "Dotted names - Context Stacking Failed Lookup",
"desc": "Dotted names should resolve against the proper context stack.",
"data": {
"section1": [
1,
2
],
"section2": {
"dynamic": "partial",
"value": "section2"
}
},
"template": "{{#section1}}{{>*section2.dynamic}}{{/section1}}",
"partials": {
"partial": "\"{{value}}\""
},
"expected": "\"\"\"\""
},
{
"name": "Recursion",
"desc": "Dynamic partials should properly recurse.",
"data": {
"template": "node",
"content": "X",
"nodes": [
{
"content": "Y",
"nodes": []
}
]
},
"template": "{{>*template}}",
"partials": {
"node": "{{content}}<{{#nodes}}{{>*template}}{{/nodes}}>"
},
"expected": "X<Y<>>"
},
{
"name": "Dynamic Names - Double Dereferencing",
"desc": "Dynamic Names can't be dereferenced more than once.",
"data": {
"dynamic": "test",
"test": "content"
},
"template": "\"{{>**dynamic}}\"",
"partials": {
"content": "Hello, world!"
},
"expected": "\"\""
},
{
"name": "Dynamic Names - Composed Dereferencing",
"desc": "Dotted Names are resolved entirely before dereferencing begins.",
"data": {
"foo": "fizz",
"bar": "buzz",
"fizz": {
"buzz": {
"content": null
}
}
},
"template": "\"{{>*foo.*bar}}\"",
"partials": {
"content": "Hello, world!"
},
"expected": "\"\""
},
{
"name": "Surrounding Whitespace",
"desc": "A dynamic partial should not alter surrounding whitespace; any\nwhitespace preceding the tag should be treated as indentation while any\nwhitespace succeding the tag should be left untouched.\n",
"data": {
"partial": "foobar"
},
"template": "| {{>*partial}} |",
"partials": {
"foobar": "\t|\t"
},
"expected": "| \t|\t |"
},
{
"name": "Inline Indentation",
"desc": "Whitespace should be left untouched: whitespaces preceding the tag\nshould be treated as indentation.\n",
"data": {
"dynamic": "partial",
"data": "|"
},
"template": " {{data}} {{>*dynamic}}\n",
"partials": {
"partial": ">\n>"
},
"expected": " | >\n>\n"
},
{
"name": "Standalone Line Endings",
"desc": "\"\\r\\n\" should be considered a newline for standalone tags.",
"data": {
"dynamic": "partial"
},
"template": "|\r\n{{>*dynamic}}\r\n|",
"partials": {
"partial": ">"
},
"expected": "|\r\n>|"
},
{
"name": "Standalone Without Previous Line",
"desc": "Standalone tags should not require a newline to precede them.",
"data": {
"dynamic": "partial"
},
"template": " {{>*dynamic}}\n>",
"partials": {
"partial": ">\n>"
},
"expected": " >\n >>"
},
{
"name": "Standalone Without Newline",
"desc": "Standalone tags should not require a newline to follow them.",
"data": {
"dynamic": "partial"
},
"template": ">\n {{>*dynamic}}",
"partials": {
"partial": ">\n>"
},
"expected": ">\n >\n >"
},
{
"name": "Standalone Indentation",
"desc": "Each line of the partial should be indented before rendering.",
"data": {
"dynamic": "partial",
"content": "<\n->"
},
"template": "\\\n {{>*dynamic}}\n/\n",
"partials": {
"partial": "|\n{{{content}}}\n|\n"
},
"expected": "\\\n |\n <\n->\n |\n/\n"
},
{
"name": "Padding Whitespace",
"desc": "Superfluous in-tag whitespace should be ignored.",
"data": {
"dynamic": "partial",
"boolean": true
},
"template": "|{{> * dynamic }}|",
"partials": {
"partial": "[]"
},
"expected": "|[]|"
}
]
}
Loading