Skip to content

Instantly share code, notes, and snippets.

@lukewatts
Last active May 27, 2022 12:20
Show Gist options
  • Save lukewatts/6549a4000ba13d8743916fb8031baff1 to your computer and use it in GitHub Desktop.
Save lukewatts/6549a4000ba13d8743916fb8031baff1 to your computer and use it in GitHub Desktop.
AST example for Affinity4\Temple - Indented engine language for PHP
<?php
$input = <<<INPUT
use Affinity4\Temple\Temple
html(lang=en_IE)
head
css(path/to/style)
js(path/to/js defer)
body.app.theme__affinity4#app
h1: Page Title
Temple::component(component-filename, param1="value", param2=$variable_passed_to_render)
INPUT;
$ast = [
UseStatement(
path: 'Affinity4\Temple\Temple'
),
HtmlElement(
tagname: 'html',
attributes: [
HtmlAttribute(
attribute_name: TypeString(
value: 'lang'
),
attribute_value: TypeString(
value: 'en_IE'
)
)
],
children: [ // If array, (empty or not) then this element is not self closing /can have children
HtmlElement(
tagname: 'head',
attributes: [],
children: [
HtmlElement(
tag:name: 'link',
attributes: [
HtmlAttribute(
attribute_name: 'href',
attribute_value: TypeString(
value: 'path/to/style.css'
) // file extension automatically appended, if missing
),
HtmlAttribute(
attribute_name: 'type',
attribute_value: TypeString(
value: 'text/css'
)
),
HtmlAttribute(
attribute_name: 'rel',
attribute_value: TypeString(
value: 'stylesheet'
)
),
HtmlAttribute(
attribute_name: 'media',
attribute_value: TypeString(
value: 'all'
)
)
],
children: false // self closing. Cannot have child elements
),
HtmlElement(
tag:name: 'link',
attributes: [
HtmlAttribute(
attribute_name: 'src',
attribute_value: TypeString(
value: 'path/to/style.css'
)
),
HtmlAttribute(
attribute_name: 'type',
attribute_value: TypeString(
value: 'text/javascript'
)
),
HtmlAttribute(
attribute_name: 'defer',
attribute_value: null // null means this attribute can be generated without an ="" e.g. <script defer src="..." type="..." />
)
],
children: false // self closing. Cannot have child elements
),
]
),
// body
HtmlElement(
tagname: 'body',
attributes: [
HtmlAttribute(
attribute_name: 'class',
attribute_value: TypeString(
value: 'app theme__affinity4'
)
),
HtmlAttribute(
attribute_name: 'id',
attribute_value: TypeString(
value: 'app'
)
)
],
children: [
HtmlElement(
tagname: 'h1',
attributes: [
TextNode(
value: "Page Title"
),
TempleComponent(
arguments: [
Argument(
value: FileNameArgument(
file_name: 'component-filename',
file_path: './components/component-filename.php', // will loop through the componets folder to find the file and will populate this field when it find the file
)
)
Argument(
value: TypeString(
value: "value"
)
),
Argument(
value: TypeExpresion(
value: $variable_passed_to_render
)
)
]
)
]
)
]
)
]
)
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment