Created
March 15, 2018 22:31
-
-
Save csilverman/51c0a9fa763706b9052551c0b0eff5c0 to your computer and use it in GitHub Desktop.
Script to take line-delimited content and merge it with a template.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Item formatter | |
============== | |
This script takes data in a set of line-delimited items, as follows: | |
Arthur Arthurson | |
From: Phoenix, AZ | |
Major: Political Science | |
Bertha Berthason | |
From: Elmhurst, HI | |
Major: English | |
Casey Caseyton | |
From: Ringewald, NY | |
Major: Physics | |
It then plugs these values into a markup template that you specify. It can | |
also remove the labels preceding a line ("From:", "Major:", etc). | |
Setup | |
----- | |
1. Replace the $template markup with your own markup. Specify the parts to be | |
replaced as follows: {{NAME}}, {{FROM}}, etc. | |
2. In the $setup array, specify the tags in the order that their corresponding | |
lines appear in the data: | |
"NAME", | |
"FROM", | |
"MAJOR", | |
If the line contains a label ("From:") that you want removed, include that | |
as follows: | |
"FROM, From:", | |
*/ | |
$template = <<<TEMPLATE | |
<div class="item"><!-- item start --> | |
<h3 class="item__title">{{NAME}}</h3> | |
<div class="item__bio"> | |
<ul class="item__bioList"> | |
<li><strong>From:</strong> {{FROM}}</li> | |
<li><strong>Major/minor:</strong> {{MAJOR}}</li> | |
<li><strong>Extracurriculars:</strong> {{EXTRAS}}</li> | |
<li><a href="mailto:{{EMAIL}}">{{EMAIL}}</a></li> | |
</ul> | |
</div> | |
<div class="u-Video"> | |
<iframe allowfullscreen="" frameborder="0" height="360" mozallowfullscreen="" src="https://player.vimeo.com/video/257120941?title=0&byline=0&portrait=0" webkitallowfullscreen="" width="640"></iframe> | |
</div> | |
</div><!-- item end --> | |
TEMPLATE; | |
/* Now specify which lines of data correspond to tags in | |
the template. | |
*/ | |
$setup = array( | |
"NAME", | |
"FROM, From:", | |
"MAJOR, Major/minor:", | |
"EXTRAS, Extracurriculars:", | |
"EMAIL", | |
); | |
$data = <<<DATA | |
Arthur, class of 2017 | |
From: Amherst, MA | |
Major/minor: Political science and Hispanic studies | |
Extracurriculars: Theater, Ultimate Frisbee, intramural sports | |
arthur@vassar.edu | |
Bea, class of 2019 | |
From: Belfort, AZ | |
Major/minor: Philosophy | |
Extracurriculars: English Language Learners Outreach Program, student-run choir | |
bea@vassar.edu | |
Casey, class of 2019 | |
From: Cobb, GA | |
Major/minor: Political science, biology, and education | |
Extracurriculars: Transitions intern, R.E.A.L. Skills tutor, weight training | |
casey@vassar.edu | |
Dana, class of 2018 | |
From: Delacorte, NY | |
Major/minor: Psychology, education, and art history | |
Extracurriculars: Varsity rowing | |
dana@vassar.edu | |
Edgar, class of 2018 | |
From: Edgarton, MA | |
Major/minor: Urban studies | |
Extracurriculars: Varsity cross country and track, international studies research assistant | |
edgar@vassar.edu | |
Frank, class of 2018 | |
From: Frankenville, IL | |
Major/minor: Media studies | |
Extracurriculars: Student theater | |
frank@vassar.edu | |
DATA; | |
/* Don't edit anything below here | |
============================== */ | |
// First, divide all the data into individual items | |
$data_items = explode(PHP_EOL.PHP_EOL, $data); | |
/* This function takes a tag, a data item (name, email, etc), and a | |
chunk of markup (the template), wraps the tag in handlebars, and | |
replaces the tag in the markup with the actual data. | |
*/ | |
function apply_tag_to_template($tag, $data, $template) { | |
$formatted_tag = "{{".$tag."}}"; | |
return str_replace($formatted_tag, $data, $template); | |
} | |
/* This function takes an entire item (a collection of lines), splits it up, | |
and merges the data with the template markup. | |
*/ | |
function format_item($data) { | |
// Divide the item into its individual lines | |
$item_lines = explode(PHP_EOL, $data); | |
global $setup, $template; | |
$final_item_markup = $template; | |
// Run through each line: match it to a tag, and then plug it | |
// into the template markup. | |
for($j=0;$j<count($item_lines);$j++) { | |
$tag_to_replace = $setup[$j]; | |
$content_to_replace_it_with = $item_lines[$j]; | |
// If a line includes a label, remove the label | |
if(strpos($setup[$j], ", ")) { | |
$tag_divided = explode(", ", $setup[$j]); | |
$tag_to_replace = $tag_divided[0]; | |
$content_to_replace_it_with = str_replace($tag_divided[1], "", $content_to_replace_it_with); | |
} | |
// Remove any white space from the beginning of the content | |
$content_to_replace_it_with = trim($content_to_replace_it_with); | |
$final_item_markup = apply_tag_to_template($tag_to_replace, $content_to_replace_it_with, $final_item_markup); | |
} | |
return $final_item_markup; | |
} | |
// Run through all the individual items | |
$final_markup = ""; | |
for($i=0;$i<count($data_items);$i++) { | |
echo format_item($data_items[$i]); | |
} | |
// echo $final_markup; | |
?> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment