Hi
Completely out of shape on Monday. . . I went to watch the game at noon. I didn’t sleep. I don’t know if I can finish the smarty at night. Come on
1. jQuery
---Filter selector (2)---
--[attribute=value]Attribute selector
As an important feature of DOM elements, attributes can also be used in selectors. Starting from this section, we will introduce selectors for obtaining elements through element attributes. The function of [attribute=value]
attribute selectors is to obtain attribute names and attribute values. All elements are exactly the same, where [] is the bracket symbol used specifically for attribute selectors, the parameter attribute represents the attribute name, and the value parameter represents the attribute value.
--:text form text selector
:text
The form text selector can obtain all single-line text input box elements in the form. The single-line text input box is like a note tool without line breaks and is very widely used.
--:password form password selector
If you want to get the password input text box, you can use the :password
selector. Its function is to get all the password input text box elements in the form.
--:radio button selector
Radio buttons in a form are often used to select only one of multiple items of data, and using the :radio
selector can easily obtain the all radio button elements in the form.
--:checkbox checkbox selector
Check boxes in forms are often used to select multiple data. Use the :checkbox
selector to quickly locate and obtain the check box elements in the form.
--:submit button selector
Normally, only one submit button with a "type" attribute value of "submit" is allowed in a form. Use the :submit
selector to get the Submit button element in the form.
--:image image domain selector
When the "type" attribute value of an element is set to "image", the element is an image field. Use the :image
selector to quickly obtain all elements of this type.
--:button form button selector
The
form contains many types of buttons, and the :button
selector can be used to obtain and only obtain two types of ordinary button elements, and
--:checked selected state selector
Some elements have a selected state, such as check boxes and radio button elements. When selected, the "checked" attribute value is "checked". Calling: checked can get all elements in the selected state.
--:selected选中状态选择器
与:checked
选择器相比,:selected
选择器只能获取
------------------------------------------------- ------------------
2. Regular expression
The function of the engine template to be done here is to replace the variable output.
What is needed is the template file .class and the compiled source file .php
--template.class.php
/*
* Description: Imitation smarty template engine class file
*
*/
class template{
private $templateDir; //Used to store the directory where the source files are located
private $compileDir; //Used to store the compiled file directory
private $leftTag='{#' ; //The tag of the object to be replaced, smarty defaults to {
private $rightTag='#}';
private $currentTemp=''; //Used to store the name of the template file currently being compiled
private $outputHtml; //HTML code used to store currentTemp
private $varpool=array(); //Variable pool; variables are stored during template compilation
public function __construct($templateDir,$compileDir ,$leftTag=null,$rightTag=null){
$this->templateDir=$templateDir;
$this->compileDir=$compileDir;
if (!empty($leftTag)) $this->leftTag=$leftTag; //When passing left and right tags, you need to make a non-empty judgment
if (!empty($rightTag)) $this->rightTag=$rightTag;
}
//assign function - put the variables needed in the template into the address pool and give the tag
public function assign($tag,$var){
$ this->varpool[$tag]=$var;
}
//Corresponding to the assignment above, take out the variable from the address pool
public function getVar($tag){
return $this->varpool[$tag];
}
//getSourceTemplate: Get the compiled source file_You need to know the file name and full path (so you need an extension
public function getSourceTemplate($templateName,$ext='.html'){
$this->currentTemp=$templateName;
$sourceFilename=$this->templateDir.$this->currentTemp. $ext;
$this->outputHtml=file_get_contents($sourceFilename);
}
//compileTemplate: Compile method
public function compileTemplate($templateName=null,$ext= '.html'){
$templateName=empty($templateName)? $this->currentTemp:$templateName;
//Core code, regular compilation
//{#$(w )# }
$pattern='/{#$(w )#}/';
//Clearer writing $pattern='/'.preg_quote($this->leftTag).' *$( [a-zA-Z_]w*) *'.preg_quote($this->rightTag).'/';
//What the core code has to do is to find the thing in the tag, and then replace it with something that PHP can recognize Content
$this->outputHtml=preg_replace($pattern, 'getVar('$1')?>', $this->outputHtml);
//Note the usage of preg_replace here, $1 represents the matching sub-pattern
//Generate the target file below, which also requires the complete target path
$compiledFilename=$this->compileDir.md5( $templateName).$ext;
file_put_contents($compiledFilename,$this->outputHtml);
}
public function display($templateName = null, $ext = '.html') {
$templateName = empty($templateName) ? $this->currentTemp : $templateName;
include_once $this->compileDir.md5($templateName).$ext;
}
}
--index.php
/*
* Copycat template engine test file
*/
//Include files
require_once 'template.class.php';
//Get the path, basedir root directory
$baseDir=str_replace('\', '/',dirname(__FILE__) );
$temp=new template($baseDir.'/source/', $baseDir.'/compliled/');
//Variable pool
$temp->assign('pagetitle', 'Copycat version smarty');
$temp->assign('test','imooc goddess');
//
$temp->getSourceTemplate('index');
$temp->compileTemplate();
$temp->display();
--A few things to note are
Follow the attribute method and compile like a pipeline
Then, some services need to be opened, otherwise like me, you still need to debug. . .
Good luck to everyone.