Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gentle API for putting content into template #13

Open
hokoo opened this issue Aug 27, 2024 · 1 comment · May be fixed by #21
Open

Gentle API for putting content into template #13

hokoo opened this issue Aug 27, 2024 · 1 comment · May be fixed by #21

Comments

@hokoo
Copy link
Owner

hokoo commented Aug 27, 2024

Suppose, we've got a template:

$template = <<<TEMPLATE
<div class="list">
  %s
  [[list-item]]
    %s
  
    [[logo]]
    <div class="logo">
      %s
    </div>
    [[/logo]]

    [[text]]<div class="text">%s</div>[[/text]]

    [[img]]<img src="%s" alt=""/>[[/img]]
  [[/list-item]]
</div>
TEMPLATE;

Sometimes it's getting hard to maintain such expressions as

$data = [
  [
    [
      'tag'     => 'list-item',
      'content' => [
        [
          [
            'tag'     => 'logo',
            'content' => [
              [
                [ 'tag' => 'img', 'content' => 'localhost//image1.jpg' ],
              ]
            ]
          ],
          [ 'tag' => 'text', 'content' => 'TEXT1' ],
        ]
      ],
    ],
  ],
];
@hokoo
Copy link
Owner Author

hokoo commented Aug 27, 2024

Obviously, more readable approach would be like this:

$img = TemplaterClient::getElement( tag: 'img' );
$img->append( 'localhost//image1.jpg' );

$logo = TemplaterClient::getElement( tag: 'logo' );
$logo->append( $img );

$text = TemplaterClient::getElement( tag: 'text' );
$text->append( 'TEXT1' );

$element = TemplaterClient::getElement();
$element->append( $logo );
$element->append( $text );

/*
 * The elements' number added into $list_item, equals the modifiers' number in the list-item tag.
 */
$list_item = TemplaterClient::getElement( 'list-item' );
$list_item->append( $element );

/**
 * The elements' number inside of $elements equals the top level modifiers number in the template.
 */
$elements = TemplaterClient::getElement();
$elements->append( $list_item );

$templater = new Templater();
$result = $templater->render( $template, $elements );

@hokoo hokoo linked a pull request Sep 15, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant