Skip to content

Commit

Permalink
Change: update syntax of template block class
Browse files Browse the repository at this point in the history
  • Loading branch information
satrun77 committed Nov 1, 2021
1 parent ee204d1 commit dbeb38a
Showing 1 changed file with 14 additions and 29 deletions.
43 changes: 14 additions & 29 deletions src/Template/Parser/TemplateBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,27 @@
namespace Moo\Template\Parser;

/**
* Class TemplateBlock
* Class TemplateBlock.
*/
class TemplateBlock
{
/**
* Collection of section arguments
*
* @var array
* Collection of section arguments.
*/
protected static $arguments = [];
protected static array $arguments = [];

/**
* Unique name for content argument
*
* @var string
* Unique name for content argument.
*/
protected static $contentName = '{{___CONTENT___}}';
protected static string $contentName = '{{___CONTENT___}}';

/**
* Provide a template tag in template. Its similar implementation to <% include %>
*
* @param array $res
* @return string
* Provide a template tag in template. Its similar implementation to <% include %>.
*/
public static function template($res)
public static function template(array $res): string
{
// Create unique name for content
static::$contentName = uniqid('__CONTENT__');
static::$contentName = uniqid('__CONTENT__', true);

// Name of content argument
$content = static::$contentName;
Expand All @@ -39,7 +32,7 @@ public static function template($res)
$arguments = '[';
// First the values from '<% arg %>'
foreach (static::$arguments as $name => $value) {
$arguments .= "'" . $name . "' => " . $value . ',';
$arguments .= "'".$name."' => ".$value.',';
}

// Construct 'Content' argument that would hold the content from the body of '<% template %>'
Expand All @@ -59,17 +52,15 @@ public static function template($res)
// Clear the values from the static storage
static::$arguments = [];

// Render template by lookup code
// Render template by lookup code, else argument is string, then render template by name
if ($res['Arguments'][0]['ArgumentMode'] === 'lookup') {
$template = str_replace('$$FINAL', 'XML_val', $res['Arguments'][0]['php']);
$php = <<<PHP
\$val .= \SilverStripe\View\SSViewer::execute_template(
{$template}, \$scope->getItem(), {$arguments}, \$scope
);
PHP;
}
// If argument is string, then render template by name
else {
} else {
$template = trim($res['Arguments'][0]['text'], "'");
$php = <<<PHP
\$val .= \SilverStripe\View\SSViewer::execute_template(
Expand All @@ -82,12 +73,9 @@ public static function template($res)
}

/**
* Set an argument value for the current template
*
* @param array $res
* @return string
* Set an argument value for the current template.
*/
public static function set(array $res)
public static function set(array $res): string
{
// Get the name of the argument
$name = $res['Arguments'][0]['text'];
Expand All @@ -109,10 +97,7 @@ public static function set(array $res)
}

/**
* Replaces content tag with placeholder code for rendering the template content
*
* @param array $res
* @return string
* Replaces content tag with placeholder code for rendering the template content.
*/
public static function content(array $res)
{
Expand Down

0 comments on commit dbeb38a

Please sign in to comment.