Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Last active May 20, 2020 09:59
Show Gist options
  • Save jonathantneal/5889625 to your computer and use it in GitHub Desktop.
Save jonathantneal/5889625 to your computer and use it in GitHub Desktop.
Content

Content

Content is created by putting data into a template. In this case, the data is information, and the template is how that information is displayed. Each data and template are also given a title and tags to make them easier to manage and organize. A title makes something easy to identify, and tags make something easy to group.

In this system, the markup languages of JSON is used to describe the data and the template.

Here is an example of a JSON template file named template.json:

{
	"title": "Birthday Greeting",
	"tags": "birthday, text",
	"template": "Happy Birthday, %full_name%!"
}

In this example, percent symbols (%) wrap around a variable called full_name. This variable will be replaced with data.

Here is an example of a JSON data file named data.json:

{
	"title": "Charlie's Name",
	"tags": "charlie, name, text",
	"data": {
		"full_name": {
			"type": "text",
			"value": "Charlie Brown"
		}
	}
}

In this example, full_name is defined as Charlie Brown. This data can be used by the template.

Now, content can be created by combining the data and the template. There are many different ways to display content in many different languages, and here is an example from a PHP file named output.php:

<?php
 
// get the template from the JSON file
$TEMPLATE = json_decode(file_get_contents('template.json'));
 
// get the data from the JSON file
$DATA = json_decode(file_get_contents('data.json'));
 
// get the output of the template using the data
$OUTPUT = preg_replace_callback('/%([^%]+)%/', function ($matches) use ($DATA) {
	return $DATA->data->$matches[1]->value;
}, $TEMPLATE->template);
 
// print the output
print($OUTPUT);

The final output of this PHP would look like this:

Happy Birthday, Charlie Brown!
{
"title": "Charlie's Name",
"tags": "charlie, name, text",
"data": {
"full_name": {
"type": "text",
"value": "Charlie Brown"
}
}
}
<?php
// get the template from the JSON file
$TEMPLATE = json_decode(file_get_contents('template.json'));
// get the data from the JSON file
$DATA = json_decode(file_get_contents('data.json'));
// get the output of the template using the data
$OUTPUT = preg_replace_callback('/%([^%]+)%/', function ($matches) use ($DATA) {
return $DATA->data->$matches[1]->value;
}, $TEMPLATE->template);
// print the output
print($OUTPUT);
{
"title": "Birthday Greeting",
"tags": "birthday, text",
"template": "Happy Birthday, %full_name%!"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment