@@ -295,6 +295,50 @@ public function dirFiles(string $name, string ...$files): self
295
295
});
296
296
}
297
297
298
+ /**
299
+ * Simple render template by replace template vars.
300
+ *
301
+ * - not support expression on template.
302
+ *
303
+ * @param string $tplFile
304
+ * @param array $tplVars
305
+ *
306
+ * @return $this
307
+ */
308
+ public function replaceVars (string $ tplFile , array $ tplVars = []): static
309
+ {
310
+ Assert::notBlank ($ tplFile );
311
+
312
+ $ dstFile = $ this ->getRealpath ($ tplFile );
313
+ if (!File::isAbsPath ($ tplFile )) {
314
+ $ tplFile = $ this ->tplDir . '/ ' . $ tplFile ;
315
+ }
316
+
317
+ $ this ->printMsgf ('replace vars: %s ' , $ tplFile );
318
+ $ this ->doReplace ($ tplFile , $ dstFile , $ tplVars );
319
+
320
+ return $ this ;
321
+ }
322
+
323
+ /**
324
+ * @param string $tplFile
325
+ * @param string $dstFile
326
+ * @param array $tplVars
327
+ *
328
+ * @return void
329
+ */
330
+ protected function doReplace (string $ tplFile , string $ dstFile , array $ tplVars = []): void
331
+ {
332
+ if (!$ this ->dryRun ) {
333
+ if ($ this ->tplVars ) {
334
+ $ tplVars = array_merge ($ this ->tplVars , $ tplVars );
335
+ }
336
+
337
+ $ content = Str::renderTemplate (File::readAll ($ tplFile ), $ tplVars );
338
+ File::putContents ($ dstFile , $ content );
339
+ }
340
+ }
341
+
298
342
/**
299
343
* Render template files by glob match.
300
344
*
@@ -360,15 +404,15 @@ public function tplFile(string $tplFile, string $dstFile = '', array $tplVars =
360
404
}
361
405
362
406
$ this ->printMsgf ('render file: %s ' , $ tplFile );
363
- if ($ this ->tplVars ) {
364
- $ tplVars = array_merge ($ this ->tplVars , $ tplVars );
365
- }
366
407
367
408
return $ this ->doRender ($ tplFile , $ dstFile , $ tplVars );
368
409
}
369
410
370
411
/**
371
- * Do render template file
412
+ * Do render template file with vars
413
+ *
414
+ * - should support expression on template.
415
+ * - TIP: recommended use package: phppkg/easytpl#EasyTemplate
372
416
*
373
417
* @param string $tplFile
374
418
* @param string $dstFile
@@ -378,11 +422,7 @@ public function tplFile(string $tplFile, string $dstFile = '', array $tplVars =
378
422
*/
379
423
protected function doRender (string $ tplFile , string $ dstFile , array $ tplVars = []): self
380
424
{
381
- if (!$ this ->dryRun ) {
382
- $ content = Str::renderTemplate (File::readAll ($ tplFile ), $ tplVars );
383
-
384
- File::putContents ($ dstFile , $ content );
385
- }
425
+ $ this ->doReplace ($ tplFile , $ dstFile , $ tplVars );
386
426
387
427
return $ this ;
388
428
}
@@ -421,6 +461,8 @@ public function backPrev(): self
421
461
}
422
462
423
463
/**
464
+ * get realpath relative the workdir
465
+ *
424
466
* @param string $path
425
467
*
426
468
* @return string
0 commit comments