Element Selection (pick)
The pick() method selects elements for modification.
It is the core of data binding.
1. Basic Usage
$tpl->pick('title')->content('Hello');
---
2. Supported Selectors
- id →
<div id="title"> - name →
<input name="title"> - tag →
<title>
3. Content Binding
$tpl->pick('title')->content('Welcome');
---
4. Replace Element
$tpl->pick('title')->replace('<h1>New</h1>');
---
5. Attribute Binding
$tpl->pick('btn')
->attribute('class', 'btn btn-primary')
->attribute('disabled', true);
---
6. Conditional Attribute
$tpl->pick('option')->selectedIf(true);
---
7. Multiple Matches (Important)
Behavior depends on implementation:
---
- May affect first match
- May throw error (recommended)
8. Common Mistakes
❌ Duplicate IDs
❌ Selector not found
❌ Using generic tag selectors unintentionally
❌ Conflicting selectors across templates
---
❌ Selector not found
❌ Using generic tag selectors unintentionally
❌ Conflicting selectors across templates
9. Best Practices
- Prefer ID-based selection
- Keep selectors unique
- Avoid relying on tag names
10. Debug Tip
If binding fails, inspect rendered HTML for missing IDs.
---
pick() is the core binding engine — treat it carefully.
Low-Level API (Advanced)
These methods are used internally by the engine. Use only if you need direct control.
---assign(string $key, mixed $value)
Replaces entire element.
$tpl->assign('title', '<h1>Hello</h1>');
---
assignContent(string $key, mixed $value)
Sets inner content.
$tpl->assignContent('title', 'Hello');
---
assignAttribute(string $key, string $name, mixed $value)
Sets attribute.
$tpl->assignAttribute('btn', 'class', 'btn btn-primary');
---
Prefer using
pick() for better readability and maintainability.