Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1d55ae4

Browse files
committed
up: ftb add new method for quick replace tpl vars
1 parent 35dcca3 commit 1d55ae4

File tree

1 file changed

+51
-9
lines changed

1 file changed

+51
-9
lines changed

‎src/Extra/FileTreeBuilder.php‎

Lines changed: 51 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,50 @@ public function dirFiles(string $name, string ...$files): self
295295
});
296296
}
297297

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+
298342
/**
299343
* Render template files by glob match.
300344
*
@@ -360,15 +404,15 @@ public function tplFile(string $tplFile, string $dstFile = '', array $tplVars =
360404
}
361405

362406
$this->printMsgf('render file: %s', $tplFile);
363-
if ($this->tplVars) {
364-
$tplVars = array_merge($this->tplVars, $tplVars);
365-
}
366407

367408
return $this->doRender($tplFile, $dstFile, $tplVars);
368409
}
369410

370411
/**
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
372416
*
373417
* @param string $tplFile
374418
* @param string $dstFile
@@ -378,11 +422,7 @@ public function tplFile(string $tplFile, string $dstFile = '', array $tplVars =
378422
*/
379423
protected function doRender(string $tplFile, string $dstFile, array $tplVars = []): self
380424
{
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);
386426

387427
return $this;
388428
}
@@ -421,6 +461,8 @@ public function backPrev(): self
421461
}
422462

423463
/**
464+
* get realpath relative the workdir
465+
*
424466
* @param string $path
425467
*
426468
* @return string

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /