Jump to content

Recommended Posts

Posted

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);

Posted

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
Posted

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
Posted

As far as I understand, both methods require file as input while we already have "view" in a variable (e.g. it is stored as textarea in some page).

Posted

PW has answers. As usual.) Super! Thank you guys!

And more generally, Functions.php is a good addition to PW knowledge - I'd recommend to look at it for all who works with API.

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
×
×
  • Create New...