Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git
pages/Template.php
if( $this->layout !== '' ) {
- $layoutFile = $this->layout;
+ $layout = $this->layout;
$this->layout = '';
$this->assign( 'content', $this->blocks['content'] ?? '' );
-
- $res = $this->evaluate( $layoutFile );
+ $res = $this->evaluate( $layout );
}
return $res;
}
private function evaluate( string $file ): string {
- $path = __DIR__ . '/' . $file;
- $code = \file_get_contents( $path );
- $php = $this->compile( $code );
- $tmp = \tempnam( \sys_get_temp_dir(), 'tpl_' );
- $res = '';
+ $php = $this->compile( \file_get_contents( __DIR__ . '/' . $file ) );
+ $tmp = \tempnam( \sys_get_temp_dir(), 'tpl_' );
\file_put_contents( $tmp, $php );
private function compile( string $code ): string {
- $c = \preg_replace_callback( '/\{([^\}]+)\}/', function( $m ) {
- $inner = \preg_replace_callback(
- '/(\$[a-zA-Z0-9_]+)((?:\.[a-zA-Z0-9_]+)+)/',
- function( $m2 ) {
- $parts = \explode( '.', \ltrim( $m2[2], '.' ) );
- $res = $m2[1];
-
- foreach( $parts as $p ) {
- $res .= "['$p']";
- }
+ $c = \preg_replace_callback(
+ '/\{strip\}(.*?)\{\/strip\}/s',
+ function( $m ) {
+ return \preg_replace( '/\s*\n\s*/', '', $m[1] );
+ },
+ $code
+ );
- return $res;
- },
- $m[1]
- );
+ $c = \preg_replace_callback(
+ '/\{([^\}]+)\}/',
+ function( $m ) {
+ return '{' . \preg_replace_callback(
+ '/(\$[a-zA-Z0-9_]+)((?:\.[a-zA-Z0-9_]+)+)/',
+ function( $m2 ) {
+ $parts = \explode( '.', \ltrim( $m2[2], '.' ) );
+ $res = $m2[1];
- return '{' . $inner . '}';
- }, $code );
+ foreach( $parts as $p ) {
+ $res .= "['$p']";
+ }
- $c = \preg_replace(
- '/\{extends\s+"([^"]+)"\}/',
- '<?php $this->layout = "$1"; ?>',
+ return $res;
+ },
+ $m[1]
+ ) . '}';
+ },
$c
);
- $c = \preg_replace(
+ $search = [
+ '/\{extends\s+"([^"]+)"\}/',
'/\{block\s+([a-zA-Z0-9_]+)\}/',
- '<?php \ob_start(); ?>',
- $c
- );
-
- $c = \preg_replace(
'/\{\/block\s+([a-zA-Z0-9_]+)\}/',
- '<?php $this->blocks["$1"] = \ob_get_clean(); ?>',
- $c
- );
-
- $c = \preg_replace(
'/\{yield\s+([a-zA-Z0-9_]+)\}/',
- '<?php echo $this->blocks["$1"] ?? ""; ?>',
- $c
- );
-
- $c = \preg_replace( '/\{if\s+([^}]+)\}/', '<?php if( $1 ) { ?>', $c );
- $c = \preg_replace(
+ '/\{if\s+([^}]+)\}/',
'/\{elseif\s+([^}]+)\}/',
- '<?php } elseif( $1 ) { ?>',
- $c
- );
-
- $c = \str_replace( '{else}', '<?php } else { ?>', $c );
- $c = \str_replace( '{/if}', '<?php } ?>', $c );
-
- $c = \preg_replace(
'/\{foreach\s+([^}]+)\}/',
- '<?php foreach( $1 ) { ?>',
- $c
- );
-
- $c = \str_replace( '{/foreach}', '<?php } ?>', $c );
-
- $c = \preg_replace( '/\{\!\$([^}]+)\}/', '<?php echo (\$$1 ?? ""); ?>', $c );
-
- $c = \preg_replace(
+ '/\{\!\$([^}]+)\}/',
'/\{\$([^}]+)\}/',
- '<?php echo \htmlspecialchars( (string)(\$$1 ?? "") ); ?>',
- $c
- );
+ '/\{else\}/',
+ '/\{\/if\}/',
+ '/\{\/foreach\}/'
+ ];
+ $replace = [
+ '<?php $this->layout = "$1"; ?>',
+ '<?php \ob_start(); ?>',
+ '<?php $this->blocks["$1"] = \ob_get_clean(); ?>',
+ '<?php echo $this->blocks["$1"] ?? ""; ?>',
+ '<?php if( $1 ) { ?>',
+ '<?php } elseif( $1 ) { ?>',
+ '<?php foreach( $1 ) { ?>',
+ '<?php echo \$$1 ?? ""; ?>',
+ '<?php echo \htmlspecialchars( (string)( \$$1 ?? "" ) ); ?>',
+ '<?php } else { ?>',
+ '<?php } ?>',
+ '<?php } ?>'
+ ];
- return $c;
+ return \preg_replace( $search, $replace, $c );
}
}
pages/commits.tpl
</div>
{/if}
-
{/if}
{/block content}
pages/diff.tpl
</tr>
{else}
+ {strip}
<tr class="{if $line.t === '+'}diff-add{elseif $line.t === '-'}diff-del{else}diff-ctx{/if}">
<td class="diff-num" data-num="{$line.no}"></td>
<td class="diff-num" data-num="{$line.nn}"></td>
<td class="diff-code">
<span class="diff-marker">{if $line.t === '+'}+{elseif $line.t === '-'}-{else} {/if}</span>{$line.l}
</td>
</tr>
+ {/strip}
{/if}
{/foreach}

Adds strip definition to templating

Author Dave Jarvis <email>
Date 2026-03-02 00:17:01 GMT-0800
Commit a20561a1c40c4686db9a8d64c3a40144fec1c480
Parent 43ee359
Delta 52 lines added, 66 lines removed, 14-line decrease