Template::set - jcobban/Genealogy GitHub Wiki
Up: class Template
This specifies the text that is to be substituted for the specified marker. The case of the label in the insertion point must match the case of the parameter to method set. For example $template->set('DATE', 'Sunday, 22 Oct 2017')
replaces each occurrence of '$DATE' with 'Sunday, 22 Oct 2017' in the generated page but does not match '$date'
or '$Date'
. It is recommended that the insertion points identified by method $template‑>set be all in upper case, while those identified by method $template‑>updateTag
or $tag‑>update
have identifiers with lower or mixed case. There can be multiple occurrences of a particular marker within the template. No warning is generated if a value provided by method set does not match any marker in the template, however a warning is generated if a marker is encountered in the template which does not correspond to a call to set and $template‑>debug
is set to true.
This method has three parameters:
parameter | description |
---|---|
$marker | the name of the insertion point. Note that this must match exactly as to case with the point defined in the template. For clarity it is recommended that these marker names be all upper case. |
$value | a string, or array of strings, or array of arrays of strings, or an object with members containing the text to insert in place of the marker. |
$doSubs | this is an optional parameter. Normally the text $value is inserted unaltered. However if you know that the text contains insertion points set this to true to request that those insertion points be resolved to previously defined values. In particular if you obtain a portion of the template from an instance of TemplateTag , which is likely to contain insertions, request that the substitutions are done before the value is saved. This is ignored if $value is not a string. |
Examples:
$array = ["foo" => "bar",
"bar" => "foo"];
class Person {
public $name;
public $age;
function __construct($p1, $p2) {
$this->name = $p1;
$this->age = $p2;
}
}
$person = new Person('John Smith', 25);
$text = 'A piece of text';
$template->set('NAME', $array);
$template->set('PERSON', $person);
$template->set('TEXT', $text);
with a template containing:
<p>value of $NAME[foo], value of $NAME[bar]</p>
<p>Person $PERSON->name is $PERSON->age years old.</p>
<p>Text is $TEXT.</p>
Note that the subscript indices are not enclosed in quotes when they are non-numeric, in conformance with the PHP implementation. This generates:
<p>value of bar, value of foo</p>
<p>Person John Smith is 25 years old.</p>
<p>Text is A piece of text.</p>
The function of the third parameter is shown in the following example:
$template->set('VALUE', 'value');
$text = 'A piece of text with a $VALUE substitution point in it.';
$template->set('NAME', $text, true);
with a template:
<p>$NAME</p>
generates:
<p>A piece of text with a value substitution point in it.</p>
The substitutions within $text
are made at the time of the call, therefore ensure that all of the insertions used by the string are defined by calls to $template->set
that precede this call.
Next: $template->get($marker)