diff --git a/includes/class-Template.php b/includes/class-Template.php index f7dc39e..0c215d2 100644 --- a/includes/class-Template.php +++ b/includes/class-Template.php @@ -1,75 +1,71 @@ . namespace itwikidelbot; /** * Handle templates of text * * Template files are 'something.template' into the /template directory */ class Template { /** * Placeholder used to mark where the template content starts */ const START_PLACEHOLDER = "\n"; /** * Get the content of a template passing its arguments * * @param $name string Template name * @param $args array Template arguments * @return string Template content */ static function get( $name, $args = [] ) { // remove spaces $name = str_replace( ' ', '_', $name ); // ../templates/$name.template $path = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'templates' . DIRECTORY_SEPARATOR . $name . '.tpl'; if( ! file_exists( $path ) ) { throw new \InvalidArgumentException( "unexisting template $name" ); } // template content with also documentation $content = file_get_contents( $path ); // stripping documentation etc. $pos = strpos( $content, self::START_PLACEHOLDER ); if( false === $pos ) { throw new \Exception( 'missing header in template' ); } $pos += strlen( self::START_PLACEHOLDER ); $content = substr( $content, $pos ); // text-editors usually put an unuseful newline before the EOF $content = rtrim( $content ); // pass arguments $s = vsprintf( $content, $args ); if( false === $s ) { - - echo "CONTENT\n"; - echo $content . "\n"; - var_dump( "ASD", $args ); throw new \Exception( "wrong number of arguments for template $name" ); } return $s; } }