コード例 #1
0
 /**
  * Validates a template file.
  * You can pass either a file name, or the file content. One of the parameters
  *  needs to be !== null.
  *
  * @param string $strFile    Template file name to check
  * @param string $strContent Template content to check
  *
  * @return mixed Boolean true if no errors have been found, array of
  *                errors otherwise. An error is an array with keys
  *                - 'short' (short error code, string)
  *                - 'message' (readable message)
  *                - 'line'    (line number)
  *                - 'code'    (code that caused the error)
  *                false if no file and content is given
  */
 public static function validate($strFile = null, $strContent = null)
 {
     $arLines = HTML_Template_PHPLIB_Helper::getLines($strFile, $strContent);
     if ($arLines === false) {
         return false;
     }
     $arErrors = HTML_Template_PHPLIB_Validator::checkBlockDefinitions($arLines);
     $arErrors = array_merge($arErrors, HTML_Template_PHPLIB_Validator::checkVariables($arLines));
     HTML_Template_PHPLIB_Validator::sortByLine($arErrors);
     return count($arErrors) == 0 ? true : $arErrors;
 }
コード例 #2
0
    /**
     */
    public function testGetLines()
    {
        //test content
        $strContent = <<<EOT
1
22
333
44
5
EOT;
        $this->assertEquals(array('1', '22', '333', '44', '5'), HTML_Template_PHPLIB_Helper::getLines(null, $strContent));
        //TODO: test file loading
    }
コード例 #3
0
 /**
  * Creates the code to use a given template file
  *
  * @param string $strFile    Template file
  * @param string $strTplName Template reference name
  * @param string $strPrefix  Prefix to prepend before the code
  *
  * @return string PHP code
  */
 function getCodeBlockDefinition($strFile, $strTplName = null, $strPrefix = '$tpl')
 {
     $arBlocks = HTML_Template_PHPLIB_Generator::getBlocks(HTML_Template_PHPLIB_Helper::getLines($strFile));
     if ($strTplName === null) {
         $strTplName = HTML_Template_PHPLIB_Generator::getTemplateNameFromFilename($strFile);
     }
     $nl = "\r\n";
     $code = '';
     $code .= $strPrefix . ' = new HTML_Template_PHPLIB();' . $nl;
     $code .= HTML_Template_PHPLIB_Generator::getCodeBlock($arBlocks, $strTplName, $strPrefix);
     $code .= $nl;
     $code .= '//TODO: do something with the code' . $nl;
     $code .= $nl;
     $code .= $strPrefix . '->finish(' . trim($strPrefix) . "->parse('TMP', '" . $strTplName . "'));" . $nl;
     return $code;
 }
コード例 #4
0
    /**
     * Test if wrongly defined variables are detected
     */
    public function testCheckVariables()
    {
        $arErrors = HTML_Template_PHPLIB_Validator::checkVariables(array());
        $this->assertEquals(array(), $arErrors);
        $cont = <<<EOT
{FULL}
{PARTIAL1
PARTIAL2}
<h1>{FULL2}{FULL3}</h2>
<p><strong>{PARTIAL3</strong>}</p>
<p>{<strong>PARTIAL3}</strong></p>
EOT;
        $arErrors = HTML_Template_PHPLIB_Validator::checkVariables(HTML_Template_PHPLIB_Helper::getLines(null, $cont));
        $arErrors = self::stripMessages($arErrors);
        $this->assertEquals(array(array('short' => 'CLOSING_BRACE_MISSING', 'line' => 2, 'code' => '{PARTIAL1'), array('short' => 'OPENING_BRACE_MISSING', 'line' => 3, 'code' => 'PARTIAL2}'), array('short' => 'CLOSING_BRACE_MISSING', 'line' => 5, 'code' => '{PARTIAL3<'), array('short' => 'OPENING_BRACE_MISSING', 'line' => 6, 'code' => '>PARTIAL3}')), $arErrors);
    }