sigma.php
来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,759 行 · 第 1/5 页
PHP
1,759 行
$this->currentBlock = $block; return SIGMA_OK; } /** * Parses the current block * * @see parse(), setCurrentBlock() * @access public */ function parseCurrentBlock() { return $this->parse($this->currentBlock); } /** * Returns the current block name * * @return string block name * @access public */ function getCurrentBlock() { return $this->currentBlock; } /** * Preserves the block even if empty blocks should be removed. * * Sometimes you have blocks that should be preserved although they are * empty (no placeholder replaced). Think of a shopping basket. If it's * empty you have to show a message to the user. If it's filled you have * to show the contents of the shopping basket. Now where to place the * message that the basket is empty? It's not a good idea to place it * in you application as customers tend to like unecessary minor text * changes. Having another template file for an empty basket means that * one fine day the filled and empty basket templates will have different * layouts. * * So blocks that do not contain any placeholders but only messages like * "Your shopping basked is empty" are intoduced. Now if there is no * replacement done in such a block the block will be recognized as "empty" * and by default ($removeEmptyBlocks = true) be stripped off. To avoid this * you can call touchBlock() * * @param string block name * @return mixed SIGMA_OK on success, error object on failure * @throws PEAR_Error * @access public * @see $removeEmptyBlocks, $_touchedBlocks */ function touchBlock($block) { if (!isset($this->_blocks[$block])) { return $this->raiseError($this->errorMessage(SIGMA_BLOCK_NOT_FOUND, $block), SIGMA_BLOCK_NOT_FOUND); } if (isset($this->_hiddenBlocks[$block])) { unset($this->_hiddenBlocks[$block]); } $this->_touchedBlocks[$block] = true; return SIGMA_OK; } /** * Hides the block even if it is not "empty". * * Is somewhat an opposite to touchBlock(). * * Consider a block (a 'edit' link for example) that should be visible to * registered/"special" users only, but its visibility is triggered by * some little 'id' field passed in a large array into setVariable(). You * can either carefully juggle your variables to prevent the block from * appearing (a fragile solution) or simply call hideBlock() * * @param string block name * @return mixed SIGMA_OK on success, error object on failure * @throws PEAR_Error * @access public */ function hideBlock($block) { if (!isset($this->_blocks[$block])) { return $this->raiseError($this->errorMessage(SIGMA_BLOCK_NOT_FOUND, $block), SIGMA_BLOCK_NOT_FOUND); } if (isset($this->_touchedBlocks[$block])) { unset($this->_touchedBlocks[$block]); } $this->_hiddenBlocks[$block] = true; return SIGMA_OK; } /** * Sets the template. * * You can either load a template file from disk with LoadTemplatefile() or set the * template manually using this function. * * @access public * @param string template content * @param boolean remove unknown/unused variables? * @param boolean remove empty blocks? * @return mixed SIGMA_OK on success, error object on failure * @see loadTemplatefile() */ function setTemplate($template, $removeUnknownVariables = true, $removeEmptyBlocks = true) { $this->_resetTemplate($removeUnknownVariables, $removeEmptyBlocks); $list = $this->_buildBlocks('<!-- BEGIN __global__ -->'.$template.'<!-- END __global__ -->'); if (PEAR::isError($list)) { return $list; } return $this->_buildBlockVariables(); } /** * Loads a template file. * * If caching is on, then it checks whether a "prepared" template exists. * If it does, it gets loaded instead of the original, if it does not, then * the original gets loaded and prepared and then the prepared version is saved. * addBlockfile() and replaceBlockfile() implement quite the same logic. * * @param string filename * @param boolean remove unknown/unused variables? * @param boolean remove empty blocks? * @access public * @return mixed SIGMA_OK on success, error object on failure * @see setTemplate(), $removeUnknownVariables, $removeEmptyBlocks */ function loadTemplateFile($filename, $removeUnknownVariables = true, $removeEmptyBlocks = true) { if ($this->_isCached($filename)) { $this->_resetTemplate($removeUnknownVariables, $removeEmptyBlocks); return $this->_getCached($filename); } $template = $this->_getFile($this->_sourceName($filename)); if (PEAR::isError($template)) { return $template; } $this->_triggers = array(); $template = preg_replace($this->includeRegExp, "\$this->_makeTrigger('\\1', '__global__')", $template); if (SIGMA_OK !== ($res = $this->setTemplate($template, $removeUnknownVariables, $removeEmptyBlocks))) { return $res; } else { return $this->_writeCache($filename, '__global__'); } } /** * Adds a block to the template changing a variable placeholder to a block placeholder. * * This means that a new block will be integrated into the template in * place of a variable placeholder. The variable placeholder will be * removed and the new block will behave in the same way as if it was * inside the original template. * * The block content must not start with <!-- BEGIN blockname --> and end with * <!-- END blockname -->, if it does the error will be thrown. * * @param string name of the variable placeholder, the name must be unique within the template. * @param string name of the block to be added * @param string content of the block * @return mixed SIGMA_OK on success, error object on failure * @throws PEAR_Error * @see addBlockfile() * @access public */ function addBlock($placeholder, $block, $template) { if (isset($this->_blocks[$block])) { return $this->raiseError($this->errorMessage(SIGMA_BLOCK_EXISTS, $block), SIGMA_BLOCK_EXISTS); } $parents = $this->_findParentBlocks($placeholder); if (0 == count($parents)) { return $this->raiseError($this->errorMessage(SIGMA_PLACEHOLDER_NOT_FOUND, $placeholder), SIGMA_PLACEHOLDER_NOT_FOUND); } elseif (count($parents) > 1) { return $this->raiseError($this->errorMessage(SIGMA_PLACEHOLDER_DUPLICATE, $placeholder), SIGMA_PLACEHOLDER_DUPLICATE); } $template = "<!-- BEGIN $block -->" . $template . "<!-- END $block -->"; $list = $this->_buildBlocks($template); if (PEAR::isError($list)) { return $list; } $this->_replacePlaceholder($parents[0], $placeholder, $block); return $this->_buildBlockVariables($block); } /** * Adds a block taken from a file to the template, changing a variable placeholder * to a block placeholder. * * @param string name of the variable placeholder * @param string name of the block to be added * @param string template file that contains the block * @return mixed SIGMA_OK on success, error object on failure * @throws PEAR_Error * @see addBlock() * @access public */ function addBlockfile($placeholder, $block, $filename) { if ($this->_isCached($filename)) { return $this->_getCached($filename, $block, $placeholder); } $template = $this->_getFile($this->_sourceName($filename)); if (PEAR::isError($template)) { return $template; } $template = preg_replace($this->includeRegExp, "\$this->_makeTrigger('\\1', '{$block}')", $template); if (SIGMA_OK !== ($res = $this->addBlock($placeholder, $block, $template))) { return $res; } else { return $this->_writeCache($filename, $block); } } /** * Replaces an existing block with new content. * * This function will replace a block of the template and all blocks * contained in it and add a new block instead. This means you can * dynamically change your template. * * Sigma analyses the way you've nested blocks and knows which block * belongs into another block. This nesting information helps to make the * API short and simple. Replacing blocks does not only mean that Sigma * has to update the nesting information (relatively time consuming task) * but you have to make sure that you do not get confused due to the * template change yourself. * * @param string name of a block to replace * @param string new content * @param boolean true if the parsed contents of the block should be kept * @access public * @see replaceBlockfile(), addBlock() * @return mixed SIGMA_OK on success, error object on failure * @throws PEAR_Error */ function replaceBlock($block, $template, $keepContent = false) { if (!isset($this->_blocks[$block])) { return $this->raiseError($this->errorMessage(SIGMA_BLOCK_NOT_FOUND, $block), SIGMA_BLOCK_NOT_FOUND); } // should not throw a error as we already checked for block existance $this->_removeBlockData($block, $keepContent); $template = "<!-- BEGIN $block -->" . $template . "<!-- END $block -->"; $list = $this->_buildBlocks($template); if (PEAR::isError($list)) { return $list; } // renew the variables list return $this->_buildBlockVariables($block); } /** * Replaces an existing block with new content from a file. * * @access public * @param string name of a block to replace * @param string template file that contains the block * @param boolean true if the parsed contents of the block should be kept * @return mixed SIGMA_OK on success, error object on failure * @throws PEAR_Error * @see replaceBlock(), addBlockfile() */ function replaceBlockfile($block, $filename, $keepContent = false) { if ($this->_isCached($filename)) { if (PEAR::isError($res = $this->_removeBlockData($block, $keepContent))) { return $res; } else { return $this->_getCached($filename, $block); } } $template = $this->_getFile($this->_sourceName($filename)); if (PEAR::isError($template)) { return $template; } $template = preg_replace($this->includeRegExp, "\$this->_makeTrigger('\\1', '{$block}')", $template); if (SIGMA_OK !== ($res = $this->replaceBlock($block, $template, $keepContent))) { return $res; } else { return $this->_writeCache($filename, $block); } } /** * Checks if the block exists in the template * * @param string block name * @return bool * @access public */ function blockExists($block) { return isset($this->_blocks[$block]); } /** * Returns the name of the (first) block that contains the specified placeholder. * * @param string Name of the placeholder you're searching * @param string Name of the block to scan. If left out (default) all blocks are scanned. * @return string Name of the (first) block that contains the specified placeholder. * If the placeholder was not found an empty string is returned. * @access public * @throws PEAR_Error */ function placeholderExists($placeholder, $block = '') { if ('' != $block && !isset($this->_blocks[$block])) { return $this->raiseError($this->errorMessage(SIGMA_BLOCK_NOT_FOUND, $block), SIGMA_BLOCK_NOT_FOUND); } if ('' != $block) { // if we search in the specific block, we should just check the array return isset($this->_blockVariables[$block][$placeholder])? $block: ''; } else { // _findParentBlocks returns an array, we need only the first element $parents = $this->_findParentBlocks($placeholder); return empty($parents)? '': $parents[0]; } } // end func placeholderExists /** * Sets a callback function. * * Sigma templates can contain simple function calls. This means that the * author of the template can add a special placeholder to it: * func_h1("embedded in h1") * Sigma will parse the template for these placeholders and will allow * you to define a callback function for them. Callback will be called * automatically when the block containing such function call is parse()'d. * * Please note that arguments to these template functions can contain * variable placeholders: func_translate('Hello, {username}'), but not * blocks or other function calls. *
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?