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

function with parameters issue... #7

Closed
scramatte opened this issue Sep 4, 2023 · 4 comments
Closed

function with parameters issue... #7

scramatte opened this issue Sep 4, 2023 · 4 comments

Comments

@scramatte
Copy link

Hello,

I'm using functions with your template renderer, but I've got an issue with parameters.
I'm unable to pass string or number as parameter directly

image

 $template = "F{{format.date('Y')}}-{{format.sequence(8)}}";
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

use PHPMicroTemplate\Render;
use Carbon\Carbon;

class TemplateFormat {

   public function sequence($digits=6) {
      return sprintf('%0'.$digits.'d', 7);
   }

   public function date($format='Y') {
      return Carbon::now()->format($format);
   }

}

class Template extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'template:render';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'replace variable into template';

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

        $render = new Render();
        $template = "F{{format.date('Y')}}-{{format.sequence(8)}}";

        $result = $render->renderTemplateString(
            $template,
            [
               'format' => new TemplateFormat,
            ]
        );

        echo  $result . PHP_EOL;
        return Command::SUCCESS;
   }   
}

Any idea what I'm doing wrong?

Regards

@wol-soft
Copy link
Owner

wol-soft commented Sep 4, 2023

Hi,

parsing parameter arguments like strings, integers, floats, arrays etc. is not supported by the library. Only template variables and function calls will be resolved (compare: https://github.com/wol-soft/php-micro-template#replacement-of-variables)

@scramatte
Copy link
Author

scramatte commented Sep 4, 2023

Does it possible to implement it?
We can sponsor the job.

@wol-soft
Copy link
Owner

wol-soft commented Sep 4, 2023

Although the library should stay minimalistic this feature with a limited scope seems like a reasonable addition.

I've implemented the feature in 93dc8de and tagged in version 1.8.0

The rendering now supports parsing of integer numbers, strings in single quotes and booleans (true, false)

@wol-soft wol-soft closed this as completed Sep 4, 2023
@scramatte
Copy link
Author

Thank you very much

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

No branches or pull requests

2 participants