I have a question regarding a HTML template.. I have create a template function which replace markers in a HTML template with content from a database..
/***********************************************************
* TemplateGenerator will match markers in a defined html
* template and replace them with corresponding data
* i.e. ###MARKER_1### in html template will be replaced
* with data in the ###MARKER_1### array key defined
* in GetMarkers function.
*
* @param string | The html template
* The template can contain all the html the
* layout needs - matched markers will be replaced.
************************************************************/
public static function TemplateGenerator($template) {
/* Get content from the html template */
$data = file_get_contents($template);
if(isset(self::$newmarkers)) {
/**************************************************
* Match each key in $this->markers array
* and replace with the correct value
***************************************************/
foreach(self::$newmarkers as $key => $value) {
if(preg_match("/". preg_quote($key) ."/", $data, $matches)) {
$data = str_replace($key, $value, $data);
} else {
$data = $data;
}
}
return $data;
} else {
if(!empty($data)) {
echo $data;
} else {
die("Der er sket en fejl med genereringen af siden");
}
}
}
I am using this function in a php newsletter application.. This works almost perfectly, but when I created the application I didn’t taking into account the possibility of editing the letter (why, I don’t know).
I have a HTML template (standard.html) with input fields to create a newsletter.
<h1 style="background-image: url(gfx/icons/new.png); background-position: left; background-repeat: no-repeat;">Opret nyhedsbrev - ###THETMP###</h1>
<form action="index.php?page=create" method="post" enctype="multipart/form-data">
<label for="letter_headline">Overskrift:</label> <input type="text" name="letter_headline" id="letter_headline" value="" /><br />
<label>Modtagere: </label> <select name="recievers[]" multiple="multiple" size="5">###RECS###</select><br />
<label>Billede</label> <input type="file" name="letter_image[0]" id="letter_image" value="" /><br />
<label for="letter_content">Indhold</label> <textarea name="letter_content" id="letter_content" cols="50" rows="15"></textarea><br />
<label for="letter_link">Link</label> <input type="text" name="letter_link" id="letter_link" value="" /><br />
<label for="letter_link_txt">Linktekst</label> <input type="text" name="letter_link_txt" id="letter_link_txt" value="" /><br />
<input type="hidden" name="template_to_use" value="standard.html" />
<input type="submit" value="Opret nyhedsbrev" name="create" />
</form>
When I select that I want to create a standard newsletter, the TemplateGenerator function will output this template with i.e. a recievers list in the marker ###RECS###.
I have a table called newsletter_fields that have the following columns:
field_uid | field_name | field_content
where field_name will contain the name of the input field, i.e. letter_headline and field_content will have the content that was written into the input field when the letter was created, i.e. “This is a headline”.
My question is if I can use that same HTML file in a editletter function that will replace only the value="" in the input fields with i.e. value="This is a headline" in the letter_headline input.
You are taking it from the wrong side.
If you have your values ALWAYS filled with markers, you can use the same template both for adding and editing, filling markers with empty strings for the latter case.
However, the whole idea of such a template is wrong.
A template engine should be able to implement basic programming logic, like loops, conditions and includes.
Otherwise there will be no good from the template at all and you’ll end up with HTML in your code (like you have already in the ###RECS### marker).
PHP based template is going to be way more convenient and I’d advise to strat using it instead of this one