Skip to content

Instantly share code, notes, and snippets.

@YavorK
Last active June 1, 2023 17:21
Show Gist options
  • Save YavorK/818f503ce925960809eaf6e0b2217927 to your computer and use it in GitHub Desktop.
Save YavorK/818f503ce925960809eaf6e0b2217927 to your computer and use it in GitHub Desktop.
Template engine with fragments.
<?php
/**
* Use
* <?= fragment_start('this_is_my_fragment') ?>
* Fragment content <?= $with_variables_and_stuff ?>
* <?= fragment_end('this_is_my_fragment') ?>
*/
/**
* Parses a php view to a HTML string
* @param $path - relative path to the view
* @param $data array ['name' => 'Peter']
* @param null $fragmentName If you specify a fragment name this function will return only the fragment. Will throw error if the fragment is not found.
* @return string the parsed HTML
* @throws Exception
*/
function parseView($path, $data, $fragmentName = null) {
if($fragmentName){
global $__nrj__fragment__name__;
$__nrj__fragment__name__ = 'this_is_my_fragment';
$fragmentStartString = fragment_start($fragmentName);
$fragmentEndString = fragment_end($fragmentName);
}
ob_start();
extract($data);
include ($path);
$result = ob_get_clean();
// if user wants a fragment try to "cut out" the fragment out of the
// whole view
if($fragmentName) {
// position of the first letter of the Start Fragment / (or End Fragment)
// "abc x{fragmentStart:this_is_my_fragment}x " position will be 4 "abc " are 4 symbols
$fragment_start_position = stripos($result, $fragmentStartString );
$fragment_end_position = stripos($result, $fragmentEndString);
if($fragment_start_position === false){
throw new Exception(
"Start for fragment: '$fragmentName' was not defined in the template: ".$path);
}
if($fragment_end_position === false){
throw new Exception(
"End for fragment: '$fragmentName' was not defined in the template: ".$path);
}
$end_of_start_string = $fragment_start_position + mb_strlen($fragmentStartString);
$fragment_substring = substr($result, $end_of_start_string,
$fragment_end_position - $end_of_start_string);
if(!$fragment_substring) {
throw new Exception("Failed to extract fragment");
}
$result = $fragment_substring;
}
return $result;
}
/**
* generate a fragment name tag if there is a fragment
* @param $name
* @return string
*/
function fragment_start($name) {
global $__nrj__fragment__name__;
if(($__nrj__fragment__name__)) {
return 'xxx{fragmentStart:'.$name.'}xxx';
}
return '';
}
function fragment_end($name) {
global $__nrj__fragment__name__;
if(isset($__nrj__fragment__name__)) {
return 'xxx{fragmentEnd:' . $name . '}xxx';
}
return '';
}
echo parseView('./view1.php',
['name' => 'World'],
'this_is_my_fragment');
<html>
<head><title></title></head>
<body>
abc <?= "Hello there, dear $name!"; ?>
<?= fragment_start('this_is_my_fragment') ?>
This is a fragment, dear <?= $name ?>!
<?= fragment_end('this_is_my_fragment') ?>
</body>
</html>
@YavorK
Copy link
Author

YavorK commented Mar 8, 2023

without fragments:
image

with fragments:
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment