scriptbox

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 91c3b555a1c4f478497d7251811ce1b43ccc5444
parent 5d246ce9328e218e994274064d2ff11a3a029f64
Author: Jan Dankert <develop@jandankert.de>
Date:   Tue,  7 Jun 2022 23:12:47 +0200

Fetched from upstream and enhanced documentation.

Diffstat:
MREADME.md | 40++++++++++++++++++++++++++++++++++++++--
Adsl/DslTemplate.class.php | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 91 insertions(+), 2 deletions(-)

diff --git a/README.md b/README.md @@ -150,9 +150,45 @@ Example +## Template script + +Remember Smarty and Twig? Yes, but both have their strange syntax. Check out this template parser with a JSP-like syntax: + + <html> + <body> + <% age = 12; %> + Next year your age is <%= (age+1) %></br> + + +### Usage + + // Parse the template, this will create a plain script + $templateParser = new DslTemplate(); + $templateParser->parseTemplate($src); + + // That's all. Lets start the interpreter + $executor = new DslInterpreter(); + $executor->runCode($templateParser->script); + echo( $executor->getOutput() ); // get the output + + ## Unsupported - There is NO support for creating classes or objects. - Due to the request-based operation of the PHP interpreter there is no possibility for asynchronous methods like `async` or `await`. - No try/catch -- No `window`, `document` or `navigator` objects -\ No newline at end of file +- No `window`, `document` or `navigator` objects + +## FAQ + +_Does it generate PHP code?_ + +No. The Interpreter works in memory. There is no way to create PHP code, even if it would be possible to implement. + +_Is it slow?_ + +Yes, maybe, because there is no cache and no compilation to native PHP code. But this scriptbox targets content management systems which have their own caching. + +_Why did you do this?_ + +Because it was possible ;) And I needed a sandboxed DSL (domain specific language) for my CMS. +\ No newline at end of file diff --git a/dsl/DslTemplate.class.php b/dsl/DslTemplate.class.php @@ -0,0 +1,52 @@ +<?php + + +namespace dsl; + + +class DslTemplate +{ + public $tagsFound = 0; + public $script = null; + + public function parseTemplate( $source ) { + + $this->script = ''; + $this->tagsFound = 0; + + while( true ) { + + $tagOpen = strpos( $source,'<%' ); + + if ( $tagOpen !== false ) { + $this->tagsFound++; + $this->addWriteCommand(substr($source,0,$tagOpen),true); + $source = substr($source,$tagOpen+2); + $tagClose = strpos( $source,'%>' ); + if ( $tagClose === false ) + throw new DslParserException('Unclosed script tag'); + $code = substr($source,0,$tagClose); + if ( $code[0] == '=' ) + $this->addWriteCommand( substr($code,1) ); + else + $this->script .= $code."\n"; + + $source = substr($source,$tagClose+2); + } + else{ + $this->addWriteCommand($source,true); + break; + } + } + } + + protected function addWriteCommand( $code, $quote = false ) { + + if ( $quote ) + foreach ( explode("\n",$code) as $line) + $this->script .= 'write(\''.str_replace('\'','\\\'',$line).'\');'."\n"; + else + $this->script .= 'write('.$code.');'."\n"; + + } +} +\ No newline at end of file