Last active
April 17, 2020 07:01
-
-
Save appsdevpk/c00433076cac0d44ef9df46ed35d0e85 to your computer and use it in GitHub Desktop.
JQuery and Html code generator in php
This file contains hidden or 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 | |
class jQuery{ | |
private $selector; | |
private $methodsList = array(); | |
public function __construct($selector){ | |
$this->selector = $selector; | |
} | |
public function output(){ | |
$output = "jQuery(".$this->selector.")"; | |
foreach($this->methodsList as $currMethod){ | |
$parameters = $currMethod[1]; | |
$paramsList = array(); | |
foreach($parameters as $param){ | |
if(is_callable($param)){ | |
$paramFunc = 'function(){'; | |
$paramFunc .= $param(); | |
$paramFunc .= '}'; | |
$paramsList[] = $paramFunc; | |
}else{ | |
$paramsList[] = "'".$param."'"; | |
} | |
} | |
$params = implode(",",$paramsList); | |
$output .= '.'.$currMethod[0].'('.$params.')'; | |
} | |
$output .= ';'; | |
return $output; | |
} | |
public function __call($name, $arguments){ | |
$this->methodsList[] = array($name,$arguments); | |
return $this; | |
} | |
} | |
class HtmlCode{ | |
private $elementsList = array(); | |
private $stylePos = 'footer'; | |
public function __construct($stylePos='footer'){ | |
$this->stylePos = $stylePos; | |
} | |
public function output(){ | |
$stylesList = array(); | |
$output = "<!DOCTYPE HTML><html>"; | |
$haveHeader = false; | |
foreach($this->elementsList as $currElement){ | |
$parameters = isset($currElement[1][0]) ? $currElement[1][0] : ''; | |
$paramsList = array(); | |
$params = ""; | |
$currStyle = ''; | |
$currSelector = ''; | |
if(is_array($parameters)){ | |
foreach($parameters as $key=>$val){ | |
if($key=='styleSelector'){ | |
$currSelector .= $val; | |
}elseif($key=='style'){ | |
$styleVals = array(); | |
foreach($val as $k=>$v){ | |
$styleVals[] = $k.':'.$v; | |
} | |
$currStyle = implode(';',$styleVals); | |
}elseif($key=='optimize'){ | |
//code to be done | |
}else{ | |
$paramsList[] = $key.'="'.$val.'"'; | |
} | |
} | |
$params = " ".implode(" ",$paramsList); | |
} | |
$nameParts = explode("_",$currElement[0]); | |
if($currSelector=='' && $currStyle!=''){ | |
$params .= ' style="'.$currStyle.'"'; | |
}elseif($currSelector!='' && $currStyle!=''){ | |
$stylesList[$currSelector] = $currStyle; | |
} | |
if(count($nameParts) > 1){ | |
if($nameParts[1]=='start'){ | |
$output .= '<'.$nameParts[0].$params.'>'; | |
if($nameParts[0]=='head'){ | |
$haveHeader = true; | |
} | |
}elseif($nameParts[1]=='end'){ | |
if($nameParts[0]=='head'){ | |
$output .= '[adpcsscontentHead]'; | |
} | |
if($nameParts[0]=='body'){ | |
$output .= '[adpcsscontentFooter]'; | |
} | |
$output .= '</'.$nameParts[0].'>'; | |
} | |
}else{ | |
if($nameParts[0]=='contents'){ | |
$output .= $currElement[1][0]; | |
}else{ | |
$output .= '<'.$nameParts[0].$params.' />'; | |
} | |
} | |
} | |
$cssCode = '<style>'; | |
if(count($stylesList) > 0){ | |
foreach($stylesList as $sKey=>$sVal){ | |
if($sKey=='' || $sVal==''){ | |
continue; | |
} | |
$cssCode .= $sKey.'{ '.$sVal.' }'; | |
} | |
} | |
$cssCode .= '</style>'; | |
if($haveHeader && $this->stylePos=='head'){ | |
$output = str_ireplace('[adpcsscontentHead]',$cssCode,$output);//replace with css code | |
$output = str_ireplace('[adpcsscontentFooter]','',$output); | |
}else{ | |
$output = str_ireplace('[adpcsscontentHead]','',$output); | |
$output = str_ireplace('[adpcsscontentFooter]',$cssCode,$output);//replace with css code | |
} | |
$output .= '</html>'; | |
return $output; | |
} | |
public function __call($name, $arguments){ | |
$this->elementsList[] = array($name,$arguments); | |
return $this; | |
} | |
} |
This file contains hidden or 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 | |
$jq = new jQuery("document"); | |
$htm = new HtmlCode(); | |
$htm->head_start(); | |
$htm->title_start()->contents('Web Page Title')->title_end(); | |
$htm->script_start(array('src'=>'https://code.jquery.com/jquery-3.4.1.min.js'))->script_end(); | |
$htm->head_end(); | |
$htm->body_start(); | |
$htm->div_start(array('class'=>'testDivClass','id'=>'testDiv','styleSelector'=>'.testDivClass','style'=>array( | |
'width'=>'500px', | |
'padding'=>'20px', | |
'border'=>'1px solid #000', | |
'margin'=>'50px auto' | |
))); | |
$htm->p_start(); | |
$htm->contents('Some paragraph content here'); | |
$htm->p_end(); | |
$htm->div_end(); | |
$documentReady = function(){ | |
$jq1 = new jQuery("'#testDiv'"); | |
$jq1->find('p'); | |
$jq1->css('color','blue'); | |
$clickHandle = function(){ | |
$jq2 = new jQuery("this"); | |
return $jq2->css('color','red')->output(); | |
}; | |
$jq1->on('click',$clickHandle); | |
return $jq1->output(); | |
}; | |
$scriptOutput = $jq->ready($documentReady)->output(); | |
$htm->script_start()->contents($scriptOutput)->script_end(); | |
$htm->body_end(); | |
echo $htm->output(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment