Jump to content

simple template engine function


valan
 Share

Recommended Posts

Quick Q: anybody knows PW core/module "process_html_template" function that does smth like below? It is quite simple and can be done in few lines of code. But if there is oppty not to invent bicycles, its better not to invent.)

===================================================================

$text = "This page has id {id} and its parent title is {title}";

$array = ["id" => $page->id, "title" => $page->parent->title];

$rendered_text = some_func($text, $array);

OR even better)

$text = "This page has id {page.id} and its parent title is {page.parent.title}";

$rendered_text = some_func($text, $page);

Link to comment
Share on other sites

you mean something like passing a variable to a template the way Views are rendered in MVC frameworks 

try the TemplateFile Class

  <?php
// this is your template file, or a controller if you will:
$view = new TemplateFile();
$view->some_param = "PW";
$view->filename = $config->paths->templates."views/{$page->template}.php";
echo $view->render();
  • Like 1
Link to comment
Share on other sites

This doesn't even need an instance of TemplateFile.

$text = wireRenderFile("views/text", array(
  "id" => $page->id,
  "title" => $page->parent->title
));

// /views/text.php
echo "This page has $id and its parent title is $title";
Edit:

If you're looking to provide the template as string there's this: https://github.com/ryancramerdesign/ProcessWire/blob/576a5d30153f045daa94a136a6ba981650632b26/wire/core/Functions.php#L855

  • Like 4
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...