Skip to content

Instantly share code, notes, and snippets.

@k-kawaa
Created November 3, 2021 08:09
Show Gist options
  • Select an option

  • Save k-kawaa/219d0489f9590660a0279e643a75dc1b to your computer and use it in GitHub Desktop.

Select an option

Save k-kawaa/219d0489f9590660a0279e643a75dc1b to your computer and use it in GitHub Desktop.
php view class example (Half-finished)
<?php
class view{
private string $sauce;
private array $variable;
private string $tmp_name;
public function __construct(string $tmp_name,string $sauce,array $variable){
$this->tmp_name = $tmp_name;
$this->sauce = $sauce;
$this->variable = $variable;
}
/**
* show html page.
* @return void
*/
public function show():void{
$page = $this->insert_variable($this->variable,$this->sauce);
echo $page;
}
public function extract_foreach_tag(string $sauce,$variables){
$pattern = '/\{{foreach,.+?\}}.+?\{{endforeach}}';
preg_match_all($pattern,$sauce,$matches);
if(empty($matches)){
return null;
}
return $matches;
}
public function insert_variable(array $variables,string $sauce):string{
foreach ($variables as $key => $variable) {
$sauce = str_replace("{{" . $key . "}}", htmlspecialchars($variable, ENT_QUOTES), $sauce);
}
return $sauce;
}
public function insert_foreach_variable(string $sauce,array $foreach_tags,array $variables){
foreach($foreach_tags as $foreach_tag){
$patten = '/\{{foreach,(?P<iterable_expression>\d,(?P<value>\d)}}(P<contents>\d+\{{endforeach}}/';
}
}
/**
* @link https://stackoverflow.com/questions/834303/startswith-and-endswith-functions-in-php
* @param $haystack
* @param $needle
* @return bool
*/
public function startsWith($haystack, $needle):bool{
$length = strlen($needle);
return (substr($haystack, 0, $length) === $needle);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment